Skip to content

Commit

Permalink
Allow disabling test extension per site (#475)
Browse files Browse the repository at this point in the history
* Allow disabling autoconsent per site (in the test extension)

* Depend on tlds-experimental in the test extension
  • Loading branch information
muodov authored Aug 1, 2024
1 parent 7f72d17 commit 59bc52b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
10 changes: 6 additions & 4 deletions addon/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { snippets } from "../lib/eval-snippets";
import { BackgroundMessage, ContentScriptMessage, DevtoolsMessage, ReportMessage } from "../lib/messages";
import { Config, RuleBundle } from "../lib/types";
import { manifestVersion, storageGet, storageRemove, storageSet } from "./mv-compat";
import { initConfig, showOptOutStatus } from "./utils";
import { initConfig, isEnabledForDomain, showOptOutStatus } from "./utils";

/**
* Mapping of tabIds to Port connections to open devtools panels.
Expand Down Expand Up @@ -72,11 +72,13 @@ chrome.runtime.onMessage.addListener(
async (msg: ContentScriptMessage, sender: any) => {
const tabId = sender.tab.id;
const frameId = sender.frameId;
const senderUrl = sender.origin || sender.url;
const senderDomain = (new URL(senderUrl)).hostname;
const autoconsentConfig: Config = await storageGet('config');
const logsConfig = autoconsentConfig.logs;
if (logsConfig.lifecycle) {
console.log('got config', autoconsentConfig);
console.groupCollapsed(`${msg.type} from ${sender.origin || sender.url}`);
console.groupCollapsed(`${msg.type} from ${senderUrl}`);
console.log(msg, sender);
console.groupEnd();
}
Expand All @@ -90,15 +92,15 @@ chrome.runtime.onMessage.addListener(
chrome.tabs.sendMessage(tabId, {
type: "initResp",
rules,
config: autoconsentConfig,
config: {...autoconsentConfig, enabled: await isEnabledForDomain(senderDomain)},
} as BackgroundMessage, {
frameId,
});
break;
case "eval":
evalInTab(tabId, frameId, msg.code, msg.snippetId).then(([result]) => {
if (logsConfig.evals) {
console.groupCollapsed(`eval result for ${sender.origin || sender.url}`);
console.groupCollapsed(`eval result for ${senderUrl}`);
console.log(msg.code, result.result);
console.groupEnd();
}
Expand Down
2 changes: 1 addition & 1 deletion addon/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<fieldset>
<div>
<input type="checkbox" id="enabled" name="enabled" checked>
<label for="enabled">Enable autoconsent</label>
<label for="enabled">Enable on <span id="current-site"></span></label>
</div>
</fieldset>

Expand Down
14 changes: 8 additions & 6 deletions addon/popup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BackgroundMessage } from "../lib/messages";
import { Config } from "../lib/types";
import { storageGet, storageRemove, storageSet } from "./mv-compat";
import { initConfig, showOptOutStatus } from "./utils";
import { initConfig, isEnabledForDomain, setIsEnabledForDomain, showOptOutStatus } from "./utils";

async function init() {
const autoconsentConfig: Config = await storageGet('config');
const enabledCheckbox = document.querySelector('input#enabled') as HTMLInputElement;
const currentSite = document.querySelector('span#current-site') as HTMLSpanElement;
const optOutRadio = document.querySelector('input#optout') as HTMLInputElement;
const optInRadio = document.querySelector('input#optin') as HTMLInputElement;
const promptRadio = document.querySelector('input#prompt') as HTMLInputElement;
Expand All @@ -25,6 +26,8 @@ async function init() {
// enable proceed button when necessary

const [currentTab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true });
const currentDomain = (new URL(currentTab.url)).hostname;
currentSite.textContent = currentDomain;
const tabId = currentTab.id;
const detectedKey = `detected${tabId}`;
console.log('detectedKey', detectedKey);
Expand All @@ -49,8 +52,8 @@ async function init() {
}

// set form initial values

enabledCheckbox.checked = autoconsentConfig.enabled;
const enabledForCurrentDomain = await isEnabledForDomain(currentDomain);
enabledCheckbox.checked = autoconsentConfig.enabled && enabledForCurrentDomain;
logsLifecycleCheckbox.checked = autoconsentConfig.logs.lifecycle;
logsRulestepsCheckbox.checked = autoconsentConfig.logs.rulesteps;
logsEvalsCheckbox.checked = autoconsentConfig.logs.evals;
Expand Down Expand Up @@ -79,9 +82,8 @@ async function init() {

// set form event listeners

enabledCheckbox.addEventListener('change', () => {
autoconsentConfig.enabled = enabledCheckbox.checked;
storageSet({ config: autoconsentConfig });
enabledCheckbox.addEventListener('change', async () => {
await setIsEnabledForDomain(currentDomain, enabledCheckbox.checked);
});

retriesInput.addEventListener('change', () => {
Expand Down
27 changes: 27 additions & 0 deletions addon/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse as tldtsParse } from 'tldts-experimental';
import { normalizeConfig } from "../lib/utils";
import { storageGet, storageSet } from "./mv-compat";

Expand Down Expand Up @@ -51,3 +52,29 @@ export async function initConfig() {
config: updatedConfig,
});
}

export async function isEnabledForDomain(domain: string) {
const storedExceptions = (await storageGet('exceptions')) || [];
console.log('stored exceptions', storedExceptions);
const parsed = tldtsParse(domain);
return !storedExceptions.includes(parsed.domain);
}

export async function setIsEnabledForDomain(domain: string, isEnabled: boolean) {
const parsed = tldtsParse(domain);
const storedExceptions = (await storageGet('exceptions')) || [];
const index = storedExceptions.indexOf(parsed.domain);
let changed = false;
if (isEnabled && index > -1) {
storedExceptions.splice(index, 1);
changed = true;
} else if (!isEnabled && index === -1) {
storedExceptions.push(parsed.domain);
changed = true;
}
if (changed) {
await storageSet({
exceptions: storedExceptions,
});
}
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@
"npm"
],
"onlyPublishWithReleaseLabel": true
},
"dependencies": {
"tldts-experimental": "^6.1.37"
}
}

0 comments on commit 59bc52b

Please sign in to comment.