| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 /** @module options */ | |
| 19 | |
| 20 "use strict"; | |
| 21 | |
| 22 const {getDecodedHostname} = require("url"); | |
| 23 const {checkWhitelisted} = require("whitelisting"); | |
| 24 const {port} = require("messaging"); | |
| 25 const info = require("info"); | |
| 26 | |
| 27 const optionsUrl = "options.html"; | |
| 28 | |
| 29 function findOptionsTab(callback) | |
| 30 { | |
| 31 chrome.tabs.query({}, tabs => | |
| 32 { | |
| 33 // We find a tab ourselves because Edge has a bug when quering tabs with | |
| 34 // extension URL protocol: | |
| 35 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8094 141/ | |
| 36 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8604 703/ | |
| 37 // Firefox won't let us query for moz-extension:// pages either, though | |
| 38 // starting with Firefox 56 an extension can query for its own URLs: | |
| 39 // https://bugzilla.mozilla.org/show_bug.cgi?id=1271354 | |
| 40 let fullOptionsUrl = ext.getURL(optionsUrl); | |
| 41 callback(tabs.find(element => element.url == fullOptionsUrl)); | |
| 42 }); | |
| 43 } | |
| 44 | |
| 45 function returnShowOptionsCall(optionsTab, callback) | |
| 46 { | |
| 47 if (!callback) | |
| 48 return; | |
| 49 | |
| 50 if (optionsTab) | |
| 51 { | |
| 52 callback(new ext.Page(optionsTab)); | |
| 53 } | |
| 54 else | |
| 55 { | |
| 56 // If we don't already have an options page, it means we've just opened | |
| 57 // one, in which case we must find the tab, wait for it to be ready, and | |
| 58 // then return the call. | |
| 59 findOptionsTab(tab => | |
| 60 { | |
| 61 if (!tab) | |
| 62 { | |
| 63 // This likely means that there was an error. | |
|
Sebastian Noack
2017/10/04 05:33:15
I think this comment should rather be:
This mea
Manish Jethani
2017/10/04 13:02:46
OK, I'd rather just lose that comment.
Done.
| |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 function onMessage(message, sender) | |
| 68 { | |
| 69 if (message.type == "app.listen" && | |
| 70 sender.page && sender.page.id == tab.id) | |
| 71 { | |
| 72 port.off("app.listen", onMessage); | |
| 73 callback(new ext.Page(tab)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 port.on("app.listen", onMessage); | |
| 78 }); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 let showOptions = | |
| 83 /** | |
| 84 * Opens the options page. | |
| 85 * | |
| 86 * @param {function} callback | |
| 87 */ | |
| 88 exports.showOptions = callback => | |
| 89 { | |
| 90 findOptionsTab(optionsTab => | |
| 91 { | |
| 92 // Edge does not yet support runtime.openOptionsPage (tested version 38) | |
| 93 if ("openOptionsPage" in chrome.runtime && | |
| 94 // Some versions of Firefox for Android before version 57 do have a | |
| 95 // runtime.openOptionsPage but it doesn't do anything. | |
| 96 // https://bugzilla.mozilla.org/show_bug.cgi?id=1364945 | |
| 97 (info.application != "fennec" || | |
| 98 parseInt(info.applicationVersion, 10) >= 57)) | |
| 99 { | |
| 100 chrome.runtime.openOptionsPage(() => | |
| 101 { | |
| 102 returnShowOptionsCall(optionsTab, callback); | |
| 103 }); | |
| 104 } | |
| 105 else if (optionsTab) | |
| 106 { | |
| 107 // Firefox for Android before version 57 does not support | |
| 108 // runtime.openOptionsPage, nor does it support the windows API. | |
| 109 // Since there is effectively only one window on the mobile browser, | |
| 110 // there's no need to bring it into focus. | |
| 111 if ("windows" in chrome) | |
| 112 chrome.windows.update(optionsTab.windowId, {focused: true}); | |
| 113 | |
| 114 chrome.tabs.update(optionsTab.id, {active: true}); | |
| 115 | |
| 116 returnShowOptionsCall(optionsTab, callback); | |
| 117 } | |
| 118 else | |
| 119 { | |
| 120 // We use a relative URL here because of this Edge issue: | |
| 121 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10 276332 | |
| 122 chrome.tabs.create({url: optionsUrl}, () => | |
| 123 { | |
| 124 returnShowOptionsCall(optionsTab, callback); | |
| 125 }); | |
| 126 } | |
| 127 }); | |
| 128 }; | |
| 129 | |
| 130 // On Firefox for Android, open the options page directly when the browser | |
| 131 // action is clicked. | |
| 132 chrome.browserAction.onClicked.addListener(() => | |
| 133 { | |
| 134 ext.pages.query({active: true, lastFocusedWindow: true}, pages => | |
| 135 { | |
| 136 let currentPage = pages[0]; | |
| 137 | |
| 138 showOptions(optionsPage => | |
| 139 { | |
| 140 if (!/^https?:$/.test(currentPage.url.protocol)) | |
| 141 return; | |
| 142 | |
| 143 optionsPage.sendMessage({ | |
| 144 type: "app.respond", | |
| 145 action: "showPageOptions", | |
| 146 args: [ | |
| 147 { | |
| 148 host: getDecodedHostname(currentPage.url).replace(/^www\./, ""), | |
| 149 whitelisted: !!checkWhitelisted(currentPage) | |
| 150 } | |
| 151 ] | |
| 152 }); | |
| 153 }); | |
| 154 }); | |
| 155 }); | |
| OLD | NEW |