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 {port} = require("messaging"); | |
23 const info = require("info"); | |
24 | |
25 const optionsUrl = "options.html"; | |
26 | |
27 function findOptionsTab(callback) | |
28 { | |
29 chrome.tabs.query({}, tabs => | |
30 { | |
31 // We find a tab ourselves because Edge has a bug when quering tabs with | |
32 // extension URL protocol: | |
33 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8094 141/ | |
34 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8604 703/ | |
35 // Firefox won't let us query for moz-extension:// pages either, though | |
36 // starting with Firefox 56 an extension can query for its own URLs: | |
37 // https://bugzilla.mozilla.org/show_bug.cgi?id=1271354 | |
38 let fullOptionsUrl = ext.getURL(optionsUrl); | |
39 callback(tabs.find(element => element.url == fullOptionsUrl)); | |
40 }); | |
41 } | |
42 | |
43 function returnShowOptionsCall(optionsTab, callback) | |
44 { | |
45 if (!callback) | |
46 return; | |
47 | |
48 if (optionsTab) | |
49 { | |
50 callback(new ext.Page(optionsTab)); | |
51 } | |
52 else | |
53 { | |
54 // If we don't already have an options page, it means we've just opened | |
55 // one, in which case we must find the tab, wait for it to be ready, and | |
56 // then return the call. | |
57 findOptionsTab(tab => | |
58 { | |
59 function onMessage(message, sender) | |
60 { | |
61 if (message.type == "app.listen" && | |
62 sender.page && sender.page.id == page.id) | |
63 { | |
64 port.off("app.listen"); | |
Sebastian Noack
2017/09/30 02:38:44
It seems you forgot to specify the listener functi
Manish Jethani
2017/09/30 13:49:52
Done.
| |
65 callback(page); | |
66 } | |
67 } | |
68 | |
69 let page = new ext.Page(tab); | |
Sebastian Noack
2017/09/30 02:38:45
You could get rid of this temporary variable (with
Manish Jethani
2017/09/30 13:49:53
Done.
| |
70 port.on("app.listen", onMessage); | |
71 }); | |
72 } | |
73 } | |
74 | |
75 /** | |
76 * Opens the options page. | |
77 * | |
78 * @param {function} callback | |
79 * @static | |
Sebastian Noack
2017/09/30 02:38:45
Is marking this function as @static necessary? It
Manish Jethani
2017/09/30 13:49:53
Done.
| |
80 */ | |
81 exports.showOptions = callback => | |
82 { | |
83 findOptionsTab(optionsTab => | |
84 { | |
85 // Edge does not yet support runtime.openOptionsPage (tested version 38) | |
86 if ("openOptionsPage" in chrome.runtime && | |
87 // Some versions of Firefox for Android before version 57 do have a | |
88 // runtime.openOptionsPage but it doesn't do anything. | |
89 // https://bugzilla.mozilla.org/show_bug.cgi?id=1364945 | |
90 (info.application != "fennec" || | |
91 parseInt(info.applicationVersion, 10) >= 57)) | |
92 { | |
93 chrome.runtime.openOptionsPage(() => | |
94 { | |
95 if (chrome.runtime.lastError) | |
96 return; | |
97 | |
98 returnShowOptionsCall(optionsTab, callback); | |
99 }); | |
100 } | |
101 else if (optionsTab) | |
Sebastian Noack
2017/09/30 02:38:44
Shouldn't this branch go first?
Manish Jethani
2017/09/30 13:49:53
I guess that depends. Do we want to skip calling r
Sebastian Noack
2017/10/02 01:47:06
I think you are right, it is better to rely on run
| |
102 { | |
103 // Firefox for Android before version 57 does not support | |
104 // runtime.openOptionsPage, nor does it support the windows API. | |
105 // Since there is effectively only one window on the mobile browser, | |
106 // there's no need to bring it into focus. | |
107 if ("windows" in chrome) | |
108 chrome.windows.update(optionsTab.windowId, {focused: true}); | |
109 | |
110 chrome.tabs.update(optionsTab.id, {active: true}); | |
111 | |
112 returnShowOptionsCall(optionsTab, callback); | |
113 } | |
114 else | |
115 { | |
116 // We use a relative URL here because of this Edge issue: | |
117 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10 276332 | |
118 chrome.tabs.create({url: optionsUrl}, () => | |
119 { | |
120 returnShowOptionsCall(optionsTab, callback); | |
121 }); | |
122 } | |
123 }); | |
124 }; | |
OLD | NEW |