Skip to content

Commit

Permalink
Fixing up restoration on startup found during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wingman-jr-addon committed Apr 5, 2020
1 parent 6858447 commit a7bad3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
13 changes: 11 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ async function dnsBlockListener(details) {
}

function setDnsBlocking(onOrOff) {
console.log('DNS blocking set request: '+onOrOff);
let isCurrentlyOn = browser.webRequest.onBeforeRequest.hasListener(dnsBlockListener);
if(onOrOff != isCurrentlyOn) {
shouldUseDnsBlocking = onOrOff;
Expand All @@ -545,6 +546,8 @@ function setDnsBlocking(onOrOff) {
browser.webRequest.onBeforeRequest.removeListener(dnsBlockListener);
}
console.log('DNS blocking is now: '+onOrOff);
} else {
console.log('DNS blocking is already correctly set.');
}
}

Expand Down Expand Up @@ -737,6 +740,11 @@ browser.webRequest.onHeadersReceived.addListener(
);

////////////////////////Actual Startup//////////////////////////////
function updateFromSettings() {
browser.storage.local.get("is_dns_blocking").then(dnsResult=>
setDnsBlocking(dnsResult.is_dns_blocking == true));
}

function handleMessage(request, sender, sendResponse) {
if(request.type=='setZone')
{
Expand All @@ -756,10 +764,11 @@ function handleMessage(request, sender, sendResponse) {
}
else if(request.type=='setDnsBlocking')
{
setDnsBlocking(request.value);
updateFromSettings();
}
}
browser.runtime.onMessage.addListener(handleMessage);
setZone('neutral');
browser.browserAction.setIcon({path: "icons/wingman_icon_32.png"});
wingman_startup();
wingman_startup();
updateFromSettings();
38 changes: 19 additions & 19 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function saveOptions() {
async function saveOptions() {
let isDnsBlocking = document.querySelector('input[name="dns_blocking"]:checked').value == "dns_blocking_yes";
browser.storage.local.set({
is_dns_blocking: isDnsBlocking
await browser.storage.local.set({
is_dns_blocking: isDnsBlocking
});
browser.runtime.sendMessage({ type: 'setDnsBlocking', value: isDnsBlocking });
}
function restoreOptions() {
}

function restoreOptions() {
console.log('Restoring saved options');

function setCurrentChoice(rawResult) {
let result = rawResult.is_dns_blocking;
console.log('Setting DNS to '+result);
Expand All @@ -19,19 +19,19 @@ function saveOptions() {
}
browser.runtime.sendMessage({ type: 'setDnsBlocking', value: result });
}

function onError(error) {
console.log(`Error saving: ${error}`);
console.log(`Error saving: ${error}`);
}

let getting = browser.storage.local.get("is_dns_blocking");
getting.then(setCurrentChoice, onError);
}
document.addEventListener("DOMContentLoaded", restoreOptions);
var radios = document.forms[0].elements["dns_blocking"];
for(var i = 0, max = radios.length; i < max; i++) {
radios[i].onclick = function() {
saveOptions();
}
}
}

document.addEventListener("DOMContentLoaded", restoreOptions);
var radios = document.forms[0].elements["dns_blocking"];
for(var i = 0, max = radios.length; i < max; i++) {
radios[i].onclick = function() {
saveOptions();
}
}

0 comments on commit a7bad3c

Please sign in to comment.