|
| 1 | +/* |
| 2 | + * This file is part of Privacy Badger <https://www.eff.org/privacybadger> |
| 3 | + * Copyright (C) 2014 Electronic Frontier Foundation |
| 4 | + * |
| 5 | + * Privacy Badger 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 | + * Privacy Badger 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 Privacy Badger. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | + /* |
| 19 | + * Temporary polyfill for firefox android, |
| 20 | + * while it doesn't support the full browserAction API |
| 21 | + * Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1330159 |
| 22 | + */ |
| 23 | + |
| 24 | +require.scopes.firefoxandroid = (function() { |
| 25 | +var isFirefoxAndroid = !( |
| 26 | + chrome.browserAction.setBadgeText && |
| 27 | + chrome.browserAction.setPopup && |
| 28 | + chrome.browserAction.getPopup |
| 29 | +); |
| 30 | + |
| 31 | +// keeps track of popup id while one is open |
| 32 | +var openPopupId = false; |
| 33 | +var popup_url = chrome.runtime.getManifest().browser_action.default_popup; |
| 34 | + |
| 35 | +// fakes a popup |
| 36 | +function openPopup() { |
| 37 | + chrome.tabs.query({active: true, lastFocusedWindow: true}, (tabs) => { |
| 38 | + var url = popup_url + "?tabId=" + tabs[0].id; |
| 39 | + chrome.tabs.create({url, index: tabs[0].index + 1}, (tab) => { |
| 40 | + openPopupId = tab.id; |
| 41 | + }); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +// remove the 'popup' when another tab is activated |
| 46 | +function onActivated(activeInfo) { |
| 47 | + if(openPopupId != false && openPopupId != activeInfo.tabId) { |
| 48 | + chrome.tabs.remove(openPopupId, () => { |
| 49 | + openPopupId = false; |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// forgets the popup when the url is overwritten by the user |
| 55 | +function onUpdated(tabId, changeInfo, tab) { |
| 56 | + if(tab.url && openPopupId == tabId){ |
| 57 | + var new_url = new URL(tab.url); |
| 58 | + |
| 59 | + if(new_url.origin + new_url.pathname != popup_url){ |
| 60 | + openPopupId = false; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Subscribe to events needed to fake a popup |
| 66 | +function startListeners() { |
| 67 | + if(isFirefoxAndroid){ |
| 68 | + chrome.browserAction.onClicked.addListener(openPopup); |
| 69 | + chrome.tabs.onActivated.addListener(onActivated); |
| 70 | + chrome.tabs.onUpdated.addListener(onUpdated); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Used in popup.js, figures out which tab opened the 'fake' popup |
| 75 | +function getParentOfPopup(callback){ |
| 76 | + chrome.tabs.query({active: true, lastFocusedWindow: true}, function(focusedTab) { |
| 77 | + var parentId = parseInt(new URL(focusedTab[0].url).searchParams.get('tabId')); |
| 78 | + chrome.tabs.get(parentId, callback); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +/************************************** exports */ |
| 83 | +var exports = {}; |
| 84 | +exports.startListeners = startListeners; |
| 85 | +exports.isUsed = isFirefoxAndroid; |
| 86 | +exports.getParentOfPopup = getParentOfPopup; |
| 87 | +return exports; |
| 88 | +/************************************** exports */ |
| 89 | +})(); |
0 commit comments