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