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 function whenOnline(page) |
| 26 { |
| 27 return new Promise(resolve => |
| 28 { |
| 29 function onPing(message, sender) |
| 30 { |
| 31 if (sender.page && sender.page.id == page.id) |
| 32 { |
| 33 port.off("ping", onPing); |
| 34 resolve(); |
| 35 } |
| 36 } |
| 37 |
| 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 }); |
| 49 } |
| 50 |
| 51 /** |
| 52 * Opens the options page. |
| 53 * |
| 54 * @param {function} callback |
| 55 * @static |
| 56 */ |
| 57 exports.showOptions = callback => |
| 58 { |
| 59 if ("openOptionsPage" in chrome.runtime && |
| 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 { |
| 66 chrome.runtime.openOptionsPage(() => |
| 67 { |
| 68 if (!callback || chrome.runtime.lastError) |
| 69 return; |
| 70 |
| 71 ext.pages.query({active: true, lastFocusedWindow: true}, ([page]) => |
| 72 { |
| 73 whenOnline(page).then(() => callback(page)); |
| 74 }); |
| 75 }); |
| 76 } |
| 77 else |
| 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 }; |
OLD | NEW |