Skip to content

Commit

Permalink
feat: enable extension on all sites
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Jan 9, 2025
1 parent 4e02210 commit 0bf9017
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 145 deletions.
36 changes: 2 additions & 34 deletions manifests/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@
"name": "Video Auto Pause",
"version": "1.10.2",
"description": "Stops multiple video services on tab unfocus and continues on focus",
"permissions": [
"tabs",
"storage",
"activeTab",
"scripting",
"idle"
],
"host_permissions": [
"https://*.youtube.com/*",
"https://*.vimeo.com/*",
"https://*.netflix.com/watch/*",
"https://*.youtubekids.com/*",
"https://*.primevideo.com/*",
"https://*.max.com/*",
"https://*.disneyplus.com/*",
"https://*.twitch.tv/*",
"https://*.udacity.com/*"
],
"permissions": ["tabs", "storage", "activeTab", "scripting", "idle"],
"host_permissions": ["<all_urls>"],
"homepage_url": "https://github.com/drodil/video_auto_pause",
"options_ui": {
"page": "options.html",
Expand All @@ -43,22 +27,6 @@
"background": {
"service_worker": "vap_bs.js"
},
"externally_connectable": {
"ids": [
"*"
],
"matches": [
"https://*.youtube.com/*",
"https://*.youtubekids.com/*",
"https://*.vimeo.com/*",
"https://*.netflix.com/watch/*",
"https://*.primevideo.com/*",
"https://*.max.com/*",
"https://*.disneyplus.com/*",
"https://*.twitch.tv/*",
"https://*.udacity.com/*"
]
},
"commands": {
"toggle-extension": {
"suggested_key": {
Expand Down
24 changes: 3 additions & 21 deletions manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@
"name": "Video Auto Pause",
"version": "1.10.2",
"description": "Stops various video services on tab unfocus and continues on focus",
"permissions": [
"tabs",
"storage",
"activeTab",
"scripting",
"idle"
],
"permissions": ["tabs", "storage", "activeTab", "scripting", "idle"],
"browser_specific_settings": {
"gecko": {
"id": "drodil@youtube_auto_pause",
"strict_min_version": "127.0"
}
},
"host_permissions": [
"https://*.youtube.com/*",
"https://*.vimeo.com/*",
"https://*.netflix.com/watch/*",
"https://*.youtubekids.com/*",
"https://*.primevideo.com/*",
"https://*.max.com/*",
"https://*.disneyplus.com/*",
"https://*.twitch.tv/*",
"https://*.udacity.com/*"
],
"host_permissions": ["<all_urls>"],
"homepage_url": "https://github.com/drodil/video_auto_pause",
"options_ui": {
"page": "options.html",
Expand All @@ -47,9 +31,7 @@
}
},
"background": {
"scripts": [
"vap_bs.js"
]
"scripts": ["vap_bs.js"]
},
"commands": {
"toggle-extension": {
Expand Down
13 changes: 5 additions & 8 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ <h4>Settings</h4>
<label><input type="checkbox" id="autoresume"><span class="label-text">Enable auto resume on tab focus</span></label>
<label><input type="checkbox" id="focuspause"><span class="label-text">Enable auto pause on window unfocus</span></label>
<label><input type="checkbox" id="focusresume"><span class="label-text">Enable auto resume on window focus</span></label>
<label><input type="checkbox" id="lockpause"><span class="label-text">Enable auto pause on computer lock</span></label>
<label><input type="checkbox" id="lockresume"><span class="label-text">Enable auto resume on computer unlock</span></label>
<label><input type="checkbox" id="scrollpause"><span class="label-text">Enable auto pause when out of viewport</span></label>
<label><input type="checkbox" id="cursorTracking"><span class="label-text">Enable cursor on window tracking</span></label>
<label><input type="checkbox" id="manualPause"><span class="label-text">Manual pause disables auto resume</span></label>
<label><input type="checkbox" id="disabledTabs"><span class="label-text">Disable on this tab</span></label>
<label><input type="checkbox" id="disableOnFullscreen"><span class="label-text">Disable on full screen</span></label>
<label><input type="checkbox" id="disabled"><span class="label-text">Disable extension</span></label>
Expand All @@ -110,10 +105,12 @@ <h4>Settings</h4>
<div class="container">
<button class="collapsible_button">Advanced settings</button>
<div class="collapsible_content">
<div id="hosts">
<h4>Enabled sites</h4>
</div>
<h4>Other settings</h4>
<label><input type="checkbox" id="lockpause"><span class="label-text">Enable auto pause on computer lock</span></label>
<label><input type="checkbox" id="lockresume"><span class="label-text">Enable auto resume on computer unlock</span></label>
<label><input type="checkbox" id="scrollpause"><span class="label-text">Enable auto pause when out of viewport</span></label>
<label><input type="checkbox" id="cursorTracking"><span class="label-text">Enable cursor on window tracking</span></label>
<label><input type="checkbox" id="manualPause"><span class="label-text">Manual pause disables auto resume</span></label>
<label><input type="checkbox" id="debugMode"><span class="label-text">Enable debug mode</span></label>
</div>
</div>
Expand Down
24 changes: 0 additions & 24 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const options = {
disableOnFullscreen: false,
};

const hosts = env.runtime.getManifest().host_permissions;
for (const host of hosts) {
options[host] = true;
}

// Saves options to chrome storage
async function save_options() {
const storage = {};
Expand Down Expand Up @@ -86,25 +81,6 @@ env.commands.getAll(function (commands) {
}
});

function formatHostName(hostname) {
return hostname.replace("https://", "").split("/")[0].replaceAll("*.", "");
}

const hostsDiv = document.getElementById("hosts");
for (host of hosts) {
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = host;
label.appendChild(checkbox);
const span = document.createElement("span");
span.className = "label-text";
span.textContent = formatHostName(host);
label.appendChild(span);
hostsDiv.appendChild(label);
checkbox.addEventListener("change", save_options);
}

// Show version in the options window
const version = document.getElementById("version");
version.textContent = "v" + env.runtime.getManifest().version;
Expand Down
65 changes: 10 additions & 55 deletions src/vap_bs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const env = chrome.runtime ? chrome : browser;
// Previous tab and window numbers
let previous_tab = -1;
let previous_window = env.windows.WINDOW_ID_NONE;
let executedTabs = [];
let disabledTabs = [];

// Computer state
Expand All @@ -24,11 +23,6 @@ let options = {
disableOnFullscreen: false,
};

const hosts = env.runtime.getManifest().host_permissions;
for (const host of hosts) {
options[host] = true;
}

function debugLog(message) {
if (options.debugMode) {
console.log(`Video auto pause: ${message}`);
Expand All @@ -37,6 +31,14 @@ function debugLog(message) {

// Initialize settings from storage
refresh_settings();
env.scripting.registerContentScripts([
{
id: "video_auto_pause",
matches: ["<all_urls>"],
js: ["video_auto_pause.js"],
runAt: "document_start",
},
]);

async function refresh_settings() {
const result = await env.storage.sync.get(Object.keys(options));
Expand All @@ -53,9 +55,6 @@ async function refresh_settings() {
options.cursorTracking = false;
options.debugMode = false;
options.disableOnFullscreen = true;
for (const host of hosts) {
options[host] = false;
}
}

disabledTabs =
Expand All @@ -69,46 +68,11 @@ async function save_settings() {
}

function isEnabledForTab(tab) {
if (!tab || !tab.url || options.disabled) {
return false;
}

if (disabledTabs.includes(tab.id)) {
if (!tab || options.disabled) {
return false;
}

const optionKey = Object.keys(options).find((option) => {
if (!option.startsWith("http")) {
return false;
}
const reg = option
.replace(/[.+?^${}()|/[\]\\]/g, "\\$&")
.replace("*", ".*");
return new RegExp(reg).test(tab.url);
});

if (optionKey) {
return !!options[optionKey];
}

return false;
}

async function injectScript(tab) {
if (executedTabs.includes(tab.id) || !isEnabledForTab(tab)) {
return;
}

debugLog(`Injecting script into tab ${tab.id} with url ${tab.url}`);
try {
await env.scripting.executeScript({
target: { tabId: tab.id },
files: ["video_auto_pause.js"],
});
executedTabs.push(tab.id);
} catch (e) {
debugLog(e);
}
return !disabledTabs.includes(tab.id);
}

// Functionality to send messages to tabs
Expand Down Expand Up @@ -204,8 +168,6 @@ env.tabs.onActivated.addListener(async function (info) {
return;
}

await injectScript(tab);

if (options.autopause && previous_tab !== -1) {
debugLog(`Tab changed, stopping video from tab ${previous_tab}`);
const prev = await env.tabs.get(previous_tab);
Expand Down Expand Up @@ -234,7 +196,6 @@ env.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
if (tab.active) {
changeIcon(false);
}
await injectScript(tab);

if (
"status" in changeInfo &&
Expand All @@ -249,10 +210,6 @@ env.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
});

env.tabs.onRemoved.addListener(function (tabId) {
if (executedTabs.includes(tabId)) {
debugLog(`Tab removed, removing tab ${tabId} from executed tabs`);
executedTabs = executedTabs.filter((e) => e !== tabId);
}
if (disabledTabs.includes(tabId)) {
disabledTabs = disabledTabs.filter((tab) => tab !== tabId);
save_settings();
Expand Down Expand Up @@ -418,8 +375,6 @@ env.windows.onCreated.addListener(async function (window) {
continue;
}

await injectScript(tabs[i]);

if (tabs[i].active && options.autoresume) {
resume(tabs[i]);
} else if (options.autopause) {
Expand Down
3 changes: 0 additions & 3 deletions src/video_auto_pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ if (window.autoPauseInjected !== true) {
options.cursorTracking = false;
options.debugMode = false;
options.disableOnFullscreen = true;
for (var host of hosts) {
options[host] = false;
}
}
});
}
Expand Down

0 comments on commit 0bf9017

Please sign in to comment.