Skip to content

Commit 35223a3

Browse files
authored
Merge pull request #1516 from lemnis/master
Added support for firefox on android
2 parents f67c5a2 + 1f230cf commit 35223a3

7 files changed

Lines changed: 126 additions & 18 deletions

File tree

‎src/js/background.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var constants = require("constants");
2525
var pbStorage = require("storage");
2626

2727
var HeuristicBlocking = require("heuristicblocking");
28+
var FirefoxAndroid = require("firefoxandroid");
2829
var webrequest = require("webrequest");
2930
var SocialWidgetLoader = require("socialwidgetloader");
3031
window.SocialWidgetList = SocialWidgetLoader.loadSocialWidgetsFromFile("data/socialwidgets.json");
@@ -55,11 +56,9 @@ function Badger() {
5556
}
5657

5758
// Show icon as page action for all tabs that already exist
58-
chrome.windows.getAll({populate: true}, function (windows) {
59-
for (var i = 0; i < windows.length; i++) {
60-
for (var j = 0; j < windows[i].tabs.length; j++) {
61-
badger.refreshIconAndContextMenu(windows[i].tabs[j]);
62-
}
59+
chrome.tabs.query({}, function (tabs) {
60+
for (var i = 0; i < tabs.length; i++) {
61+
badger.refreshIconAndContextMenu(tabs[i]);
6362
}
6463
});
6564

@@ -532,6 +531,10 @@ Badger.prototype = {
532531
* @param {Integer} tabId chrome tab id
533532
*/
534533
updateBadge: function(tabId){
534+
if(FirefoxAndroid.isUsed){
535+
return;
536+
}
537+
535538
if (!this.showCounter()){
536539
chrome.browserAction.setBadgeText({tabId: tabId, text: ""});
537540
return;
@@ -603,7 +606,7 @@ Badger.prototype = {
603606
}
604607
return true;
605608
},
606-
609+
607610
/**
608611
* Check if privacy badger is disabled, take an origin and
609612
* check against the disabledSites list
@@ -745,7 +748,7 @@ Badger.prototype = {
745748
* @param {Object} tab The tab to set the badger icon for
746749
*/
747750
refreshIconAndContextMenu: function (tab) {
748-
if (!tab) {
751+
if (!tab || FirefoxAndroid.isUsed) {
749752
return;
750753
}
751754

@@ -763,7 +766,6 @@ Badger.prototype = {
763766
}
764767

765768
chrome.browserAction.setIcon({tabId: tab.id, path: iconFilename});
766-
chrome.browserAction.setTitle({tabId: tab.id, title: "Privacy Badger"});
767769
},
768770

769771
};
@@ -777,7 +779,6 @@ function startBackgroundListeners() {
777779
}
778780
}, {urls: ["http://*/*", "https://*/*"]}, []);
779781

780-
781782
// Update icon if a tab changes location
782783
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
783784
if(changeInfo.status == "loading") {
@@ -827,6 +828,7 @@ var badger = window.badger = new Badger();
827828
incognito.startListeners();
828829
webrequest.startListeners();
829830
HeuristicBlocking.startListeners();
831+
FirefoxAndroid.startListeners();
830832
startBackgroundListeners();
831833

832834
// TODO move listeners and this message behind INITIALIZED

‎src/js/firefoxandroid.js‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
})();

‎src/js/popup.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var backgroundPage = chrome.extension.getBackgroundPage();
2222
var require = backgroundPage.require;
2323
var constants = backgroundPage.constants;
2424
var badger = backgroundPage.badger;
25+
var FirefoxAndroid = backgroundPage.FirefoxAndroid;
2526
var htmlUtils = require("htmlutils").htmlUtils;
2627

2728
var i18n = chrome.i18n;
@@ -512,6 +513,12 @@ function syncUISelections() {
512513
* seems to be that it is synchronous.
513514
*/
514515
function getTab(callback) {
516+
// Temporary fix for Firefox Android
517+
if(FirefoxAndroid.isUsed){
518+
FirefoxAndroid.getParentOfPopup(callback);
519+
return;
520+
}
521+
515522
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(t) { callback(t[0]); });
516523
}
517524

‎src/manifest.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"js/heuristicblocking.js",
3939
"js/socialwidgetloader.js",
4040
"js/migrations.js",
41+
"js/firefoxandroid.js",
4142
"js/background.js"
4243
]
4344
},

‎src/skin/options.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<head>
2222
<meta name="google" content="notranslate">
2323
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
24+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2425
<link type="text/css" href="/lib/vendor/jquery-ui-1.12.1.custom/jquery-ui.structure.min.css" rel="stylesheet" />
2526
<link type="text/css" href="/lib/vendor/jquery-ui-1.12.1.custom/jquery-ui.theme.min.css" rel="stylesheet" />
2627
<link type="text/css" media="screen" href="/skin/popup.css" rel="stylesheet" />

‎src/skin/popup.css‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
/*csslint ids:ignore*/
1919

2020
body {
21-
min-width: 200px;
22-
max-width: 400px;
21+
margin: 0;
22+
min-width: 430px; /* Chrome */
23+
max-width: 100%; /* FF android */
2324
font-size: 12px;
2425
background: #fefefe;
2526
color: #333;
2627
font-family: helvetica, arial, sans-serif;
27-
padding-left: 7px;
28-
padding-right: 7px;
29-
overflow-y: hidden;
30-
overflow-x: hidden;
31-
width: 400px;
28+
padding: 8px 15px;
29+
box-sizing: border-box;
30+
}
31+
@media screen and (min--moz-device-pixel-ratio:0) {
32+
body{
33+
min-width: 200px; /* FF android */
34+
width: 430px; /* FF desktop */
35+
}
3236
}
3337

3438
a { text-decoration: none }
@@ -218,11 +222,13 @@ font-size: 16px;
218222
max-height: 290px;
219223
overflow-y: auto;
220224
background-color: #E8E9EA;
225+
position: relative;
221226
}
222227
.key {
223-
position: fixed;
228+
position: absolute;
224229
height: 25px;
225-
width: 235px;
230+
left: 0;
231+
right: 0;
226232
z-index: 30;
227233
background: #fefefe;
228234
padding-top: 4px;
@@ -325,6 +331,7 @@ font-size: 16px;
325331
z-index: -1;
326332
opacity: 0;
327333
padding: 3%;
334+
box-sizing: border-box;
328335
color: #ccc;
329336
background: url("/skin/background.png");
330337
transition: opacity .1s ease;

‎src/tests/index.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<script src="../js/socialwidgetloader.js"></script>
5656
<script src="../js/htmlutils.js"></script>
5757
<script src="../js/migrations.js"></script>
58+
<script src="../js/firefoxandroid.js"></script>
5859
<script src="../js/background.js"></script>
5960

6061
<script src="tests/background.js"></script>

0 commit comments

Comments
 (0)