Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/chrome/content/code/ApplicableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ ApplicableList.prototype = {

},

count_applied: function() {
var counter = 0;
for (var x in this.active) {
if (!(x in this.breaking)) {
++counter;
}
}
for (var x in this.moot) {
if (!(x in this.active)) {
++counter;
}
}
return counter;
},

count_all: function() {
return Object.keys(this.all).length;
},

prepend_child: function(node) {
this.menupopup.insertBefore(node, this.menupopup.firstChild);
},
Expand Down
154 changes: 121 additions & 33 deletions src/chrome/content/toolbar_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ if (!httpsEverywhere) { var httpsEverywhere = {}; }
httpsEverywhere.toolbarButton = {

/**
* Name of preference for determining whether to show ruleset counter.
* Names of preferences for determining whether to show ruleset counters.
*/
COUNTER_PREF: "extensions.https_everywhere.show_counter",
COUNTER_TOTAL_PREF: "extensions.https_everywhere.show_counter_total",

/**
* Name of preference for whether HTTP Nowhere is on.
Expand All @@ -62,24 +63,8 @@ httpsEverywhere.toolbarButton = {

var tb = httpsEverywhere.toolbarButton;

// make sure the checkbox for showing counter is properly set
var showCounter = tb.shouldShowCounter();
var counterItem = document.getElementById('https-everywhere-counter-item');
if (counterItem) {
counterItem.setAttribute('checked', showCounter ? 'true' : 'false');
}

// make sure UI for HTTP Nowhere mode is properly set
var httpNowhereItem = document.getElementById('http-nowhere-item');
var showHttpNowhere = tb.shouldShowHttpNowhere();
var toolbarbutton = document.getElementById('https-everywhere-button');
if (httpNowhereItem) {
httpNowhereItem.setAttribute('checked', showHttpNowhere ? 'true' : 'false');
}
if (toolbarbutton) {
toolbarbutton.setAttribute('http_nowhere',
showHttpNowhere ? 'true' : 'false');
}
// make sure the checkbox for showing counters are properly set
tb.updateMenuItems();

// make sure UI is set depending on whether HTTPS-E is enabled
toggleEnabledUI();
Expand Down Expand Up @@ -163,14 +148,81 @@ httpsEverywhere.toolbarButton = {
return window.BrowserApp.selectedBrowser;
}
},

/**
* Update the toolbar button image and menuitem checkboxes to current settings.
* This is used when the UI is initialized.
*/
updateMenuItems: function() {
HTTPSEverywhere.log(INFO, 'Updating toolbar button image and menu item checkboxes.');
var tb = httpsEverywhere.toolbarButton;

var counterItem = document.getElementById('https-everywhere-counter-item');

var uiFound = false;

if (counterItem) { // If we can find the counterItem like this, we can find everything in the UI.
uiFound = true;
var counterTotalItem = document.getElementById('https-everywhere-counter-total-item');
var httpNowhereItem = document.getElementById('http-nowhere-item');
var toolbarbutton = document.getElementById('https-everywhere-button');
} else {
// If the toolbar button is hidden away in the Firefox hamburger menu,
// we won't find it by the above, and need to look somewhere else
var toolbox = document.getElementById("navigator-toolbox");
if (toolbox) {
let palette = toolbox.palette;
counterItem = palette.querySelector("#https-everywhere-counter-item");
if (counterItem) {
HTTPSEverywhere.log(DBUG, 'The toolbar button was trying to hide');
uiFound = true;
counterItem.setAttribute('checked', showCounter);
var counterTotalItem = palette.querySelector('#https-everywhere-counter-total-item');
var httpNowhereItem = palette.querySelector('#http-nowhere-item');
var toolbarbutton = palette.querySelector('#https-everywhere-button');
}
}
}

if (uiFound) {
// make sure the checkbox for showing counters are properly set
var showCounter = tb.shouldShowCounter();
HTTPSEverywhere.log(DBUG, 'Setting ruleset counter checkbox to '+showCounter);
counterItem.setAttribute('checked', showCounter);

var showCounterTotal = tb.shouldShowCounterTotal();
HTTPSEverywhere.log(DBUG, 'Setting total ruleset counter checkbox to '+showCounterTotal);
counterTotalItem.setAttribute('checked', showCounterTotal);
// No reason to be able to change the setting for showing the total
// count of applicable rulesets if we do not even show the active ones.
counterTotalItem.setAttribute('disabled', !showCounter);

// make sure UI for HTTP Nowhere mode is properly set
var showHttpNowhere = tb.shouldShowHttpNowhere();
HTTPSEverywhere.log(DBUG, 'Setting HTTP Nowhere checkbox to '+showHttpNowhere);
httpNowhereItem.setAttribute('checked', showHttpNowhere);
toolbarbutton.setAttribute('http_nowhere', showHttpNowhere);
} else {
HTTPSEverywhere.log(WARN, 'UI not found.');
}
},

/**
* Update the rulesets applied counter for the current tab.
*/
updateRulesetsApplied: function() {
var toolbarbutton = document.getElementById('https-everywhere-button');
if (!toolbarbutton) {
return;
// This becomes relevant for instance when the toolbar button is
// hidden away in the Firefox hamburger menu.
var toolbox = document.getElementById("navigator-toolbox");
if (toolbox) {
palette = toolbox.palette;
var toolbarbutton = palette.querySelector('#https-everywhere-button');
if (!toolbarbutton) {
return;
}
}
}

var enabled = HTTPSEverywhere.prefs.getBoolPref("globalEnabled");
Expand All @@ -191,16 +243,19 @@ httpsEverywhere.toolbarButton = {
}
// Make sure the list is up to date
alist.populate_list();

var counter = 0;
for (var x in alist.active) {
if (!(x in alist.breaking)) {
++counter;
}
var rulesetsApplied = alist.count_applied();
var rulesetsTotal = alist.count_all();

if (!rulesetsTotal) {
toolbarbutton.setAttribute('rulesetsApplied', 0);
return;
}

toolbarbutton.setAttribute('rulesetsApplied', counter);
HTTPSEverywhere.log(INFO, 'Setting icon counter to: ' + counter);
var includeTotals = httpsEverywhere.toolbarButton.shouldShowCounterTotal();
var counterLabel = includeTotals ? rulesetsApplied + '/' + rulesetsTotal : rulesetsApplied;

toolbarbutton.setAttribute('rulesetsApplied', counterLabel);
HTTPSEverywhere.log(INFO, 'Setting icon counter to: ' + counterLabel);
},

/**
Expand All @@ -219,6 +274,21 @@ httpsEverywhere.toolbarButton = {
return !prefExists || sp.getBoolPref(tb.COUNTER_PREF);
},

/**
* Gets whether to show the total ruleset count of applicable rulesets.
*
* @return {boolean}
*/
shouldShowCounterTotal: function() {
var tb = httpsEverywhere.toolbarButton;
var sp = Services.prefs;

var prefExists = sp.getPrefType(tb.COUNTER_TOTAL_PREF);

// This time around, the default behavior is to not show the counter
return prefExists && sp.getBoolPref(tb.COUNTER_TOTAL_PREF);
},

/**
* Gets whether to show HTTP Nowhere UI.
*
Expand All @@ -231,19 +301,30 @@ httpsEverywhere.toolbarButton = {
},

/**
* Toggles the user's preference for displaying the rulesets applied counter
* Toggles the user's preference for displaying the rulesets applied counters
* and updates the UI.
*/
toggleShowCounter: function() {
var tb = httpsEverywhere.toolbarButton;
var sp = Services.prefs;

var showCounter = tb.shouldShowCounter();
sp.setBoolPref(tb.COUNTER_PREF, !showCounter);
var showCounter = !tb.shouldShowCounter();
sp.setBoolPref(tb.COUNTER_PREF, showCounter);

var counterTotalItem = document.getElementById('https-everywhere-counter-total-item');
counterTotalItem.setAttribute('disabled', !showCounter);

tb.updateRulesetsApplied();
},
toggleShowCounterTotal: function() {
var tb = httpsEverywhere.toolbarButton;
var sp = Services.prefs;

var showCounterTotal = !tb.shouldShowCounterTotal();
sp.setBoolPref(tb.COUNTER_TOTAL_PREF, showCounterTotal);
tb.updateRulesetsApplied();
},

/**
* Toggles whether HTTP Nowhere mode is active, updates the toolbar icon.
*/
Expand All @@ -254,8 +335,7 @@ httpsEverywhere.toolbarButton = {

// Change icon color to red if HTTP nowhere is enabled
var toolbarbutton = document.getElementById('https-everywhere-button');
toolbarbutton.setAttribute('http_nowhere',
showHttpNowhere ? 'true' : 'false');
toolbarbutton.setAttribute('http_nowhere', showHttpNowhere);
reload_window();
},

Expand Down Expand Up @@ -375,6 +455,14 @@ function toggleEnabledUI() {

// Change icon depending on enabled state
var toolbarbutton = document.getElementById('https-everywhere-button');
if (!toolbarbutton) {
// If we did not find the button, look for it in the hamburger menu.
var toolbox = document.getElementById("navigator-toolbox");
if (toolbox) {
var palette = toolbox.palette;
var toolbarbutton = palette.querySelector('#https-everywhere-button');
}
}
if (toolbarbutton) {
toolbarbutton.setAttribute('status', enabled ? 'enabled' : 'disabled');
}
Expand Down
2 changes: 2 additions & 0 deletions src/chrome/content/toolbar_button.xul
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<menuseparator class="hide-on-disable"/>
<menuitem type="checkbox" id="https-everywhere-counter-item" label="&https-everywhere.menu.showCounter;"
oncommand="httpsEverywhere.toolbarButton.toggleShowCounter()" class="hide-on-disable"/>
<menuitem type="checkbox" id="https-everywhere-counter-total-item" label="&https-everywhere.menu.showCounterTotal;"
oncommand="httpsEverywhere.toolbarButton.toggleShowCounterTotal()" class="hide-on-disable"/>
<menuseparator />
<menuitem label="&https-everywhere.menu.observatory;" command="https-everywhere-menuitem-observatory" />
<menuitem label="&https-everywhere.menu.about;" command="https-everywhere-menuitem-about" />
Expand Down
6 changes: 5 additions & 1 deletion src/chrome/content/toolbar_button_binding.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0"?>

<!DOCTYPE overlay SYSTEM "chrome://https-everywhere/locale/https-everywhere.dtd">

<!--
Toolbar button needs to be extended to show a counter for the number of
rulesets applied, and this can be done using XBL.
Expand All @@ -9,11 +12,12 @@ See: https://developer.mozilla.org/en-US/docs/XBL
xmlns:xbl="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">


<binding id="https-everywhere-binding">
<content>
<!-- ruleset counter (rscounter) and rulesets applied (rsapplied) -->
<xul:stack id="rscounter">
<xul:label id="rsapplied" xbl:inherits="value=rulesetsApplied" />
<xul:label id="rsapplied" tooltiptext="&https-everywhere.menu.counterTooltip;" xbl:inherits="value=rulesetsApplied" />
</xul:stack>

<!--
Expand Down
2 changes: 2 additions & 0 deletions src/chrome/locale/en/https-everywhere.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<!ENTITY https-everywhere.menu.globalDisable "Disable HTTPS Everywhere">
<!ENTITY https-everywhere.menu.blockHttpRequests "Block all HTTP requests">
<!ENTITY https-everywhere.menu.showCounter "Show Counter">
<!ENTITY https-everywhere.menu.showCounterTotal "Include total ruleset count">
<!ENTITY https-everywhere.menu.counterTooltip "Number of HTTPS Everywhere rulesets applying to this page">
<!ENTITY https-everywhere.menu.viewAllRules "View All Rules">

<!ENTITY https-everywhere.prefs.title "HTTPS Everywhere Preferences">
Expand Down