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 return; |
| 63 |
| 64 function onMessage(message, sender) |
| 65 { |
| 66 if (message.type == "app.listen" && |
| 67 sender.page && sender.page.id == tab.id) |
| 68 { |
| 69 port.off("app.listen", onMessage); |
| 70 callback(new ext.Page(tab)); |
| 71 } |
| 72 } |
| 73 |
| 74 port.on("app.listen", onMessage); |
| 75 }); |
| 76 } |
| 77 } |
| 78 |
| 79 let showOptions = |
| 80 /** |
| 81 * Opens the options page. |
| 82 * |
| 83 * @param {function} callback |
| 84 */ |
| 85 exports.showOptions = callback => |
| 86 { |
| 87 findOptionsTab(optionsTab => |
| 88 { |
| 89 // Edge does not yet support runtime.openOptionsPage (tested version 38) |
| 90 if ("openOptionsPage" in chrome.runtime && |
| 91 // Some versions of Firefox for Android before version 57 do have a |
| 92 // runtime.openOptionsPage but it doesn't do anything. |
| 93 // https://bugzilla.mozilla.org/show_bug.cgi?id=1364945 |
| 94 (info.application != "fennec" || |
| 95 parseInt(info.applicationVersion, 10) >= 57)) |
| 96 { |
| 97 chrome.runtime.openOptionsPage(() => |
| 98 { |
| 99 returnShowOptionsCall(optionsTab, callback); |
| 100 }); |
| 101 } |
| 102 else if (optionsTab) |
| 103 { |
| 104 // Firefox for Android before version 57 does not support |
| 105 // runtime.openOptionsPage, nor does it support the windows API. |
| 106 // Since there is effectively only one window on the mobile browser, |
| 107 // there's no need to bring it into focus. |
| 108 if ("windows" in chrome) |
| 109 chrome.windows.update(optionsTab.windowId, {focused: true}); |
| 110 |
| 111 chrome.tabs.update(optionsTab.id, {active: true}); |
| 112 |
| 113 returnShowOptionsCall(optionsTab, callback); |
| 114 } |
| 115 else |
| 116 { |
| 117 // We use a relative URL here because of this Edge issue: |
| 118 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10
276332 |
| 119 chrome.tabs.create({url: optionsUrl}, () => |
| 120 { |
| 121 returnShowOptionsCall(optionsTab, callback); |
| 122 }); |
| 123 } |
| 124 }); |
| 125 }; |
| 126 |
| 127 // On Firefox for Android, open the options page directly when the browser |
| 128 // action is clicked. |
| 129 chrome.browserAction.onClicked.addListener(() => |
| 130 { |
| 131 ext.pages.query({active: true, lastFocusedWindow: true}, pages => |
| 132 { |
| 133 let currentPage = pages[0]; |
| 134 |
| 135 showOptions(optionsPage => |
| 136 { |
| 137 if (!/^https?:$/.test(currentPage.url.protocol)) |
| 138 return; |
| 139 |
| 140 optionsPage.sendMessage({ |
| 141 type: "app.respond", |
| 142 action: "showPageOptions", |
| 143 args: [ |
| 144 { |
| 145 host: getDecodedHostname(currentPage.url).replace(/^www\./, ""), |
| 146 whitelisted: !!checkWhitelisted(currentPage) |
| 147 } |
| 148 ] |
| 149 }); |
| 150 }); |
| 151 }); |
| 152 }); |
OLD | NEW |