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
73 changes: 55 additions & 18 deletions chromium/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,31 @@ var switchPlannerInfo = {};
// Is HTTPSe enabled, or has it been manually disabled by the user?
var isExtensionEnabled = true;

// Load prefs about whether http nowhere is on. Structure is:
// { httpNowhere: true/false }
var httpNowhereOn = false;
storage.get({httpNowhere: false}, function(item) {
httpNowhereOn = item.httpNowhere;
// Load preferences. Structure is:
// { httpNowhere: Boolean, showAppliedCount: Boolean }

let httpNowhere = false;
let showAppliedCount = true;

storage.get({ httpNowhere: false, showAppliedCount: true }, item => {
httpNowhere = item.httpNowhere
showAppliedCount = item.showAppliedCount;
updateState();
});
chrome.storage.onChanged.addListener(function(changes, areaName) {

chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'sync' || areaName === 'local') {
for (var key in changes) {
if (key === 'httpNowhere') {
httpNowhereOn = changes[key].newValue;
updateState();
}
if ('httpNowhere' in changes) {
httpNowhere = changes.httpNowhere.newValue;
updateState();
}
if ('showAppliedCount' in changes) {
showAppliedCount = changes.showAppliedCount.newValue;
updateState();
}
}
});

if (chrome.tabs) {
chrome.tabs.onActivated.addListener(function() {
updateState();
Expand Down Expand Up @@ -110,6 +118,25 @@ var loadStoredUserRules = function() {

loadStoredUserRules();

function getActiveRulesetCount(id) {
const applied = activeRulesets.getRulesets(id);

if (!applied)
{
return 0;
}

let activeCount = 0;

for (const key in applied) {
if (applied[key].active) {
activeCount++;
}
}

return activeCount;
}

/**
* Set the icon color correctly
* inactive: extension is enabled, but no rules were triggered on this page.
Expand All @@ -121,17 +148,19 @@ var updateState = function() {
if (!chrome.tabs) {
return;
}
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
if (!tabs || tabs.length === 0) {
return;
}
var applied = activeRulesets.getRulesets(tabs[0].id)
var iconState = "inactive";

const activeCount = getActiveRulesetCount(tabs[0].id);
let iconState = "inactive";

if (!isExtensionEnabled) {
iconState = "disabled";
} else if (httpNowhereOn) {
} else if (httpNowhere) {
iconState = "blocking";
} else if (applied) {
} else if (activeCount > 0) {
iconState = "active";
}
chrome.browserAction.setIcon({
Expand All @@ -142,6 +171,12 @@ var updateState = function() {
chrome.browserAction.setTitle({
title: "HTTPS Everywhere (" + iconState + ")"
});

chrome.browserAction.setBadgeBackgroundColor({color: "#00cc00"});

const showBadge = activeCount > 0 && isExtensionEnabled && showAppliedCount;

chrome.browserAction.setBadgeText({text: showBadge ? "" + activeCount : ""});
});
}

Expand Down Expand Up @@ -245,7 +280,7 @@ function onBeforeRequest(details) {

// Should the request be canceled?
var shouldCancel = (
httpNowhereOn &&
httpNowhere &&
uri.protocol === 'http:' &&
!/\.onion$/.test(uri.hostname) &&
!/^localhost$/.test(uri.hostname) &&
Expand Down Expand Up @@ -305,6 +340,8 @@ function onBeforeRequest(details) {
}
}

updateState();

if (newuristr && using_credentials_in_url) {
// re-insert userpass info which was stripped temporarily
const uri_with_credentials = new URL(newuristr);
Expand All @@ -323,7 +360,7 @@ function onBeforeRequest(details) {
newuristr);
}

if (httpNowhereOn) {
if (httpNowhere) {
// If loading a main frame, try the HTTPS version as an alternative to
// failing.
if (shouldCancel) {
Expand Down
8 changes: 4 additions & 4 deletions chromium/options.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
.section-header{
.section-header {
margin-bottom: 10px;
}

.section-header-span{
.section-header-span {
border-bottom: 1px solid #ccc;
font-size: 15px;
}

#import{
#import {
margin-bottom: 10px;
float: right;
}

#import-confirmed{
#import-confirmed {
display: none;
}
37 changes: 24 additions & 13 deletions chromium/options.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="options.css" rel="stylesheet" type="text/css"/>
<script src="options.js"></script>
<script src="translation.js"></script>
<script src="send-message.js"></script>
<link rel="stylesheet" href="options.css">
</head>
<body>
<div id='import-confirmed' i18n="options_imported"></div>
<div>
<input id="show-applied-count" type="checkbox" checked>

<!-- TODO: Internationalize this -->
<label for="show-applied-count">Show applied ruleset count</label>
</div>

<div id="import-confirmed" data-i18n="options_imported"></div>

<form>
<div class="section-header"><span class="section-header-span" i18n="options_importSettings"></span></div>
<section id="ImportSettings" class="options">
<input id="import-settings" type="file" accept="application/json" value="importSettings" />
</section>
<button type="submit" id="import" i18n="options_import" disabled></button>
</form>
<div class="section-header">
<span class="section-header-span" data-i18n="options_importSettings"></span>
</div>

<section id="ImportSettings" class="options">
<input id="import-settings" type="file" accept="application/json">
</section>

<button id="import" data-i18n="options_import" disabled></button>
<button id="rulesetSettingsExport">Export user rule settings</button>
<button id="rulesetSettingsImport">Import user rule settings</button>

<script src="options.js"></script>
<script src="translation.js"></script>
<script src="send-message.js"></script>
</body>
</html>
91 changes: 63 additions & 28 deletions chromium/options.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
document.addEventListener("DOMContentLoaded", () => {

let json_data;
let import_button = document.querySelector("#import");

function import_json(e) {
e.preventDefault();

let settings = JSON.parse(json_data);
sendMessage("import_settings", settings, resp => {
document.querySelector("#import-confirmed").style.display = "block";
document.querySelector("form").style.display = "none";
});
}

document.querySelector("#import-settings").addEventListener("change", event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = event => {
json_data = event.target.result;
import_button.disabled = false;
};

reader.readAsText(file);
});

document.querySelector("form").addEventListener("submit", import_json);
});
'use strict'

function readFile (file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()

reader.addEventListener('error', event => {
reject(new Error('FileReader error: ' + event.target.error.name))
})

reader.addEventListener('load', event => {
resolve(event.target.result)
})

reader.readAsText(file)
})
}

function saveFile (blob, fileName) {
const blobUrl = URL.createObjectURL(blob)

const anchor = document.createElement('a')
anchor.href = blobUrl
anchor.download = fileName
anchor.click()
}

document.addEventListener('DOMContentLoaded', () => {
const showAppliedCountCheckbox = document.getElementById('show-applied-count')

sendMessage('get_option', { showAppliedCount: true }).then(item => {
showAppliedCountCheckbox.checked = item.showAppliedCount
showAppliedCountCheckbox.addEventListener('change', event => {
sendMessage('set_option', { showAppliedCount: showAppliedCountCheckbox.checked })
})
})

const importButton = document.getElementById('import')
const importSettings = document.getElementById('import-settings')

importButton.addEventListener('click', event => {
const file = importSettings.files[0]

readFile(file).then(data => {
const settings = JSON.parse(fileContents)
sendMessage('import_settings', settings)
})
})

document.getElementById('import-settings').addEventListener('change', event => {
importButton.disabled = (event.target.files.length === 0)
})

document.getElementById('rulesetSettingsExport').addEventListener('click', () => {
const json = JSON.stringify(localStorage)
const blob = new Blob([json], { type: 'application/json' })
saveFile(blob, 'settings.json')
})

document.getElementById('rulesetSettingsImport').addEventListener('click', () => {

})
})
3 changes: 1 addition & 2 deletions chromium/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ h1, h2, h3, .rule {

/* Initially hide section (until rules get added). */
section.rules {
position: fixed;
visibility: hidden;
display: none;
}

.rule{
Expand Down
47 changes: 24 additions & 23 deletions chromium/popup.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>
<html>
<head>
<title i18n="about_ext_name"></title>
<title data-i18n="about_ext_name"></title>

<meta charset="utf-8"/>

Expand All @@ -16,58 +17,58 @@

<body>
<header>
<h1 i18n="about_ext_name"></h1>
<h1 data-i18n="about_ext_name"></h1>
</header>

<section id="disableButton" class="options">
<div class="onoffswitch">
<input id="onoffswitch" type="checkbox" checked>
<label i18n="menu_globalEnable" for="onoffswitch"></label>
<label data-i18n="menu_globalEnable" for="onoffswitch"></label>
</div>
</section>
<section id="HttpNowhere" class="options">
<input id="http-nowhere-checkbox" type="checkbox" value="httpNowhere"/>
<label i18n="menu_blockUnencryptedRequests" for="http-nowhere-checkbox"></label>
<label data-i18n="menu_blockUnencryptedRequests" for="http-nowhere-checkbox"></label>
</section>

<div id="RuleManagement">
<section>
<a href="#" id="add-rule-link" i18n="chrome_add_rule"></a>
<a href="#" id="add-rule-link" data-i18n="chrome_add_rule"></a>
<div id="add-new-rule-div" style="display:none">
<h3 i18n="about_add_new_rule"></h3>
<p i18n="chrome_always_https_for_host"></p>
<label for="new-rule-host" i18n="chrome_host"></label> <br><input size="50" id="new-rule-host" type="text" disabled><br>
<h3 data-i18n="about_add_new_rule"></h3>
<p data-i18n="chrome_always_https_for_host"></p>
<label for="new-rule-host" data-i18n="chrome_host"></label> <br><input size="50" id="new-rule-host" type="text" disabled><br>

<div id="new-rule-regular-text">
<p><a href="#" id="new-rule-show-advanced-link" i18n="chrome_show_advanced"></a>
<p><a href="#" id="new-rule-show-advanced-link" data-i18n="chrome_show_advanced"></a>
</div>
<div id="new-rule-advanced" style="display:none;">
<p><a href="#" id="new-rule-hide-advanced-link" i18n="chrome_hide_advanced"></a>
<p><a href="#" id="new-rule-hide-advanced-link" data-i18n="chrome_hide_advanced"></a>
<p>
<label for="new-rule-name" i18n="chrome_rule_name"></label><br><input size="50" id="new-rule-name" type="text"><br>
<label for="new-rule-regex" i18n="chrome_regex"></label> <br><input size="50" id="new-rule-regex" type="text"><br>
<label for="new-rule-redirect" i18n="chrome_redirect_to"></label> <br><input size="50" id="new-rule-redirect" type="text"><br>
<label for="new-rule-name" data-i18n="chrome_rule_name"></label><br><input size="50" id="new-rule-name" type="text"><br>
<label for="new-rule-regex" data-i18n="chrome_regex"></label> <br><input size="50" id="new-rule-regex" type="text"><br>
<label for="new-rule-redirect" data-i18n="chrome_redirect_to"></label> <br><input size="50" id="new-rule-redirect" type="text"><br>
</div>

<button id="add-new-rule-button" i18n="chrome_add_new_rule"></button><br>
<button id="cancel-new-rule" i18n="chrome_status_cancel_button"></button>
<button id="add-new-rule-button" data-i18n="chrome_add_new_rule"></button><br>
<button id="cancel-new-rule" data-i18n="chrome_status_cancel_button"></button>
</div>
</section>
<section id="StableRules" class="rules">
<h3 i18n="chrome_stable_rules"></h3>
<p class="description" i18n="chrome_stable_rules_description"></p>
<h3 data-i18n="chrome_stable_rules"></h3>
<p class="description" data-i18n="chrome_stable_rules_description"></p>
</section>
<section id="UnstableRules" class="rules">
<h3 i18n="chrome_experimental_rules"></h3>
<p class="description" i18n="chrome_experimental_rules_description"></p>
<h3 data-i18n="chrome_experimental_rules"></h3>
<p class="description" data-i18n="chrome_experimental_rules_description"></p>
</section>
</div>

<footer>
<a id="viewAllRules" href="https://www.eff.org/https-everywhere/atlas" target="_blank" i18n="menu_viewAllRules"></a><br><br>
<a id="aboutTitle" href="https://www.eff.org/https-everywhere" target="_blank" i18n="about_title"></a><br><br>
<a id="donateEFF" href="https://supporters.eff.org/donate/support-https-everywhere" target="_blank" i18n="menu_donate_eff_imperative"></a>
<p><small>(<span i18n="about_version">Version</span>: <span id="current-version"></span>)</small></p>
<a id="viewAllRules" href="https://www.eff.org/https-everywhere/atlas" target="_blank" data-i18n="menu_viewAllRules"></a><br><br>
<a id="aboutTitle" href="https://www.eff.org/https-everywhere" target="_blank" data-i18n="about_title"></a><br><br>
<a id="donateEFF" href="https://supporters.eff.org/donate/support-https-everywhere" target="_blank" data-i18n="menu_donate_eff_imperative"></a>
<p><small>(<span data-i18n="about_version">Version</span>: <span id="current-version"></span>)</small></p>
</footer>

</body>
Expand Down
Loading