Skip to content
Merged
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
55 changes: 53 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function save() {
let result = await chrome.storage.session.set({state: temp});
}

var exclude = [];
async function restore() {
let result = await chrome.storage.session.get('state');
if (typeof result.state === 'object' && result.state !== null) {
Expand All @@ -49,9 +50,10 @@ async function restore() {
}
state = result.state;
}
let result2 = await chrome.storage.sync.get('options');
let result2 = await chrome.storage.sync.get(['options', 'exclude']);
if (typeof result2.options === 'object' && result2.options !== null)
options = result2.options;
if (Array.isArray(result2.exclude)) exclude = result2.exclude;
resolveInitialization();
}

Expand All @@ -61,6 +63,10 @@ restore();
chrome.storage.onChanged.addListener((result) => {
if (typeof result.options === 'object' && result.options !== null)
options = result.options.newValue;
if (typeof result.exclude === 'object' && result.exclude !== null) {
exclude = result.exclude.newValue;
updateContentScripts();
}
});

// On install display the options page so the user can give permissions.
Expand Down Expand Up @@ -580,6 +586,48 @@ function toggleOption(o) {
});
}

function matchPatternToRegExp(pattern) {
if (pattern === '<all_urls>') {
return /^(https?|file|ftp):\/\/.*/;
}
const match = /^(.*):\/\/([^/]+)(\/.*)$/.exec(pattern);
if (!match) {
console.error('Invalid pattern:', pattern);
return /^(?!)/; // Matches nothing
}
const [, scheme, host, path] = match;
const specialChars = /[\\[\]\(\)\{\}\^\$\+\.\?]/g;
let re = '^';
if (scheme === '*') {
re += '(https?|ftp)';
} else {
re += scheme.replace(specialChars, '\\$&');
}
re += ':\\/\\/';
if (host === '*') {
re += '[^/]+';
} else if (host.startsWith('*.')) {
re += '([^/]+\\.)?';
re += host.substring(2).replace(specialChars, '\\$&');
} else {
re += host.replace(specialChars, '\\$&');
}
re += path.replace(specialChars, '\\$&').replace(/\*/g, '.*');
re += '$';
return new RegExp(re);
}

function isUrlExcluded(url) {
return exclude.some((pattern) => {
try {
return matchPatternToRegExp(pattern).test(url);
} catch (e) {
console.error('Error matching pattern:', pattern, e);
return false;
}
});
}

async function updateContentScripts() {
await initializationCompletePromise;
await chrome.scripting.unregisterContentScripts();
Expand All @@ -590,6 +638,7 @@ async function updateContentScripts() {
id: 'ContentScript',
js: ['ContentScript.js'],
matches: p.origins,
excludeMatches: exclude,
allFrames: true,
matchOriginAsFallback: true,
runAt: 'document_start'
Expand All @@ -598,6 +647,7 @@ async function updateContentScripts() {
id: 'WindowScript',
js: ['WindowScript.js'],
matches: p.origins,
excludeMatches: exclude,
allFrames: true,
runAt: 'document_start',
world: 'MAIN'
Expand All @@ -611,8 +661,9 @@ async function updateExtensionScripts() {
await updateContentScripts();
const tabs = await chrome.tabs.query({});
tabs.forEach(async (tab) => {
if (!tab.url || !tab.id) return;
if (!tab.url || !tab.id || isUrlExcluded(tab.url)) return;
chrome.tabs.sendMessage(tab.id, {type: 'hi ya!'}).catch(async () => {
if (isUrlExcluded(tab.url)) return;
await chrome.scripting.executeScript({
target: {
tabId: tab.id,
Expand Down
6 changes: 6 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ <h1>After changes press enter.</h1>
size="500"
placeholder="Examples: youtube or <all_urls> or https://*.youtube.com/* https://soundcloud.com/* https://example.com/*"
/></label>
<label
>Exclude Matches (space separated): <br /><input
id="exclude"
size="500"
placeholder="Examples: https://*.youtube.com/*"
/></label>
<p>
This permission is needed to pause, resume and fast forward media its not
needed to detect audio.
Expand Down
9 changes: 8 additions & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const supported = [
];

var userinput = document.getElementById('userinput');
var exclude = document.getElementById('exclude');

// User presses enter
window.addEventListener('keyup', (event) => {
Expand All @@ -34,11 +35,14 @@ chrome.permissions.onAdded.addListener(getPermissions);
chrome.permissions.onRemoved.addListener(getPermissions);

// Security: chrome.storage.sync is not safe from website content scripts.
chrome.storage.sync.get('options', (result) => {
chrome.storage.sync.get(['options', 'exclude'], (result) => {
if (typeof result.options === 'object' && result.options !== null) {
options = result.options;
applyChanges();
}
if (Array.isArray(result.exclude)) {
exclude.value = result.exclude.join(' ');
}
});

chrome.storage.onChanged.addListener((result) => {
Expand Down Expand Up @@ -145,4 +149,7 @@ async function permissionUpdate() {
}
);
}
chrome.storage.sync.set({
exclude: exclude.value.split(' ').filter((domain) => domain)
});
}
Loading