Index: lib/options.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/options.js |
@@ -0,0 +1,121 @@ |
+/* |
+ * 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"); |
+ |
+function whenOnline(page) |
+{ |
+ return new Promise(resolve => |
+ { |
+ function onPing(message, sender) |
+ { |
+ if (sender.page && sender.page.id == page.id) |
+ { |
+ port.off("ping", onPing); |
+ resolve(); |
+ } |
+ } |
+ |
+ port.on("ping", onPing); |
+ |
+ page.sendMessage({type: "ping"}, pong => |
+ { |
+ if (pong) |
+ { |
+ port.off("ping", onPing); |
+ resolve(); |
+ } |
+ }); |
+ }); |
+} |
+ |
+/** |
+ * Opens the options page. |
+ * |
+ * @param {function} callback |
+ * @static |
+ */ |
+exports.showOptions = callback => |
+{ |
+ 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 (!callback || chrome.runtime.lastError) |
+ return; |
+ |
+ ext.pages.query({active: true, lastFocusedWindow: true}, ([page]) => |
+ { |
+ whenOnline(page).then(() => callback(page)); |
+ }); |
+ }); |
+ } |
+ else |
+ { |
+ // Edge does not yet support runtime.openOptionsPage (tested version 38) |
+ 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 optionsUrl = "options.html"; |
+ let fullOptionsUrl = ext.getURL(optionsUrl); |
+ let tab = tabs.find(element => element.url == fullOptionsUrl); |
+ if (tab) |
+ { |
+ // 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(tab.windowId, {focused: true}); |
+ |
+ chrome.tabs.update(tab.id, {active: true}); |
+ |
+ if (callback) |
+ { |
+ let page = new ext.Page(tab); |
+ whenOnline(page).then(() => callback(page)); |
+ } |
+ } |
+ else |
+ { |
+ // We don't use fullOptionsUrl here because of this Edge issue: |
+ // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10276332 |
+ ext.pages.open(optionsUrl, page => |
+ { |
+ if (callback) |
+ whenOnline(page).then(() => callback(page)); |
+ }); |
+ } |
+ }); |
+ } |
+}; |