summaryrefslogtreecommitdiffstats
path: root/src/js/firefoxandroid.js
blob: 07eeb6a55f5c27a7b3d9dc69f8fe06cb49d020a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * This file is part of Privacy Badger <https://www.eff.org/privacybadger>
 * Copyright (C) 2014 Electronic Frontier Foundation
 *
 * Privacy Badger 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.
 *
 * Privacy Badger 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 Privacy Badger.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Temporary polyfill for firefox android,
 * while it doesn't support the full browserAction API
 * Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1330159
 */

require.scopes.firefoxandroid = (function() {
var hasPopupSupport = !!(
  chrome.browserAction.setPopup &&
  chrome.browserAction.getPopup
);
var hasBadgeSupport = !!chrome.browserAction.setBadgeText;

// keeps track of popup id while one is open
var openPopupId = false;
var popup_url = chrome.runtime.getManifest().browser_action.default_popup;

// fakes a popup
function openPopup() {
  chrome.tabs.query({active: true, lastFocusedWindow: true}, (tabs) => {
    var url = popup_url + "?tabId=" + tabs[0].id;
    chrome.tabs.create({url, index: tabs[0].index + 1}, (tab) => {
      openPopupId = tab.id;
    });
  });
}

// remove the 'popup' when another tab is activated
function onActivated(activeInfo) {
  if (openPopupId != false && openPopupId != activeInfo.tabId) {
    chrome.tabs.remove(openPopupId, () => {
      openPopupId = false;
    });
  }
}

// forgets the popup when the url is overwritten by the user
function onUpdated(tabId, changeInfo, tab) {
  if (tab.url && openPopupId == tabId) {
    var new_url = new URL(tab.url);

    if (new_url.origin + new_url.pathname != popup_url) {
      openPopupId = false;
    }
  }
}

// Subscribe to events needed to fake a popup
function startListeners() {
  if (!hasPopupSupport) {
    chrome.browserAction.onClicked.addListener(openPopup);
    chrome.tabs.onActivated.addListener(onActivated);
    chrome.tabs.onUpdated.addListener(onUpdated);
  }
}

// Used in popup.js, figures out which tab opened the 'fake' popup
function getParentOfPopup(callback) {
  chrome.tabs.query({active: true, currentWindow: true}, function(focusedTab) {
    var parentId = parseInt(new URL(focusedTab[0].url).searchParams.get('tabId'));
    chrome.tabs.get(parentId, callback);
  });
}

/************************************** exports */
var exports = {};
exports.startListeners = startListeners;
exports.hasPopupSupport = hasPopupSupport;
exports.hasBadgeSupport = hasBadgeSupport;
exports.getParentOfPopup = getParentOfPopup;
return exports;
/************************************** exports */
})();