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