Index: lib/options.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/options.js |
@@ -0,0 +1,124 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+/** @module options */ |
+ |
+"use strict"; |
+ |
+const {port} = require("messaging"); |
+const info = require("info"); |
+ |
+const optionsUrl = "options.html"; |
+ |
+function findOptionsTab(callback) |
+{ |
+ chrome.tabs.query({}, tabs => |
+ { |
+ // We find a tab ourselves because Edge has a bug when quering tabs with |
+ // extension URL protocol: |
+ // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8094141/ |
+ // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8604703/ |
+ // Firefox won't let us query for moz-extension:// pages either, though |
+ // starting with Firefox 56 an extension can query for its own URLs: |
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1271354 |
+ let fullOptionsUrl = ext.getURL(optionsUrl); |
+ callback(tabs.find(element => element.url == fullOptionsUrl)); |
+ }); |
+} |
+ |
+function returnShowOptionsCall(optionsTab, callback) |
+{ |
+ if (!callback) |
+ return; |
+ |
+ if (optionsTab) |
+ { |
+ callback(new ext.Page(optionsTab)); |
+ } |
+ else |
+ { |
+ // If we don't already have an options page, it means we've just opened |
+ // one, in which case we must find the tab, wait for it to be ready, and |
+ // then return the call. |
+ findOptionsTab(tab => |
+ { |
+ function onMessage(message, sender) |
+ { |
+ if (message.type == "app.listen" && |
+ sender.page && sender.page.id == page.id) |
+ { |
+ port.off("app.listen"); |
+ callback(page); |
+ } |
+ } |
+ |
+ let page = new ext.Page(tab); |
Manish Jethani
2017/09/27 12:50:13
Create a Page object and release the tab reference
|
+ port.on("app.listen", onMessage); |
+ }); |
+ } |
+} |
+ |
+/** |
+ * Opens the options page. |
+ * |
+ * @param {function} callback |
+ * @static |
+ */ |
+exports.showOptions = callback => |
+{ |
+ findOptionsTab(optionsTab => |
Manish Jethani
2017/09/27 12:50:14
Now we first try to find the options tab, since th
|
+ { |
+ // Edge does not yet support runtime.openOptionsPage (tested version 38) |
+ if ("openOptionsPage" in chrome.runtime && |
+ // Some versions of Firefox for Android before version 57 do have a |
+ // runtime.openOptionsPage but it doesn't do anything. |
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1364945 |
+ (info.application != "fennec" || |
+ parseInt(info.applicationVersion, 10) >= 57)) |
+ { |
+ chrome.runtime.openOptionsPage(() => |
+ { |
+ if (chrome.runtime.lastError) |
+ return; |
+ |
+ returnShowOptionsCall(optionsTab, callback); |
+ }); |
+ } |
+ else if (optionsTab) |
+ { |
+ // Firefox for Android before version 57 does not support |
+ // runtime.openOptionsPage, nor does it support the windows API. |
+ // Since there is effectively only one window on the mobile browser, |
+ // there's no need to bring it into focus. |
+ if ("windows" in chrome) |
+ chrome.windows.update(optionsTab.windowId, {focused: true}); |
+ |
+ chrome.tabs.update(optionsTab.id, {active: true}); |
+ |
+ returnShowOptionsCall(optionsTab, callback); |
+ } |
+ else |
+ { |
+ // We don't use fullOptionsUrl here because of this Edge issue: |
+ // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10276332 |
+ chrome.tabs.create({url: optionsUrl}, () => |
Manish Jethani
2017/09/27 12:50:13
I've changed this to chrome.tabs.create, from ext.
|
+ { |
+ returnShowOptionsCall(optionsTab, callback); |
+ }); |
+ } |
+ }); |
+}; |