Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pause all hotkey #44

Merged
merged 14 commits into from
Feb 1, 2024
19 changes: 0 additions & 19 deletions ContentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
case 'Rewind':
Rewind();
break
case 'togglePlayback':
togglePlayback();
break
case 'allowplayback':
resume(false);
break
Expand Down Expand Up @@ -78,22 +75,6 @@
break
}
});

function togglePlayback() {
Elements.forEach((data, e) => {
if (e.paused) return;
if (data.togglePause) {
data.togglePause = false;
e.playbackRate = data.wasPlaybackRate;
} else {
data.togglePause = true;
data.wasPlaying = false;
data.wasPlaybackRate = e.playbackRate;
e.playbackRate = 0;
}
Elements.set(e, data);
});
}

function isPlaying() {
checkShadow();
Expand Down
22 changes: 18 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* global chrome */
var state = {};

const setItems = ['media','backgroundaudio', 'otherTabs', 'mutedTabs', 'ignoredTabs', 'mutedMedia'];
const resumelimit = 5;
const setItems = ['media','backgroundaudio', 'otherTabs', 'mutedTabs', 'ignoredTabs', 'mutedMedia', 'legacyMedia'];

async function save() {
let temp = Object.assign({}, state);
Expand Down Expand Up @@ -33,6 +34,7 @@ state.otherTabs = new Set(); // Tab IDs of media with no permission to access.
state.mutedTabs = new Set(); // Tab IDs of all muted media.
state.ignoredTabs = new Set();
state.mutedMedia = new Set(); // Tab IDs of resumable muted media.
state.legacyMedia = new Set(); // Tab IDs of old media.
state.autoPauseWindow = null;
state.denyPlayback = false;

Expand Down Expand Up @@ -140,10 +142,14 @@ function onPlay(tab, id = '') {
if (tab.id == state.activeTab)
state.lastPlaying = null;
if (state.media.has(tab.id)) {
state.legacyMedia.delete(tab.id);
state.mutedTabs.delete(tab.id);
// Make tab top priority.
state.media.delete(tab.id);
state.media.add(tab.id);
if (hasProperty(options, 'resumelimit') && state.media.size > resumelimit) {
state.legacyMedia.add([...state.media][state.media.size-1-resumelimit]);
}
}
// Pause all other media.
if (tab.audible)
Expand Down Expand Up @@ -174,7 +180,7 @@ async function tabChange(tab) {
// Pause all except active, last playing, backgroundaudio tab
pauseOther(tab.id, true, true);
}

if (state.media.has(tab.id) || state.mutedTabs.has(tab.id)) {
play(tab.id);
} else if (state.otherTabs.has(tab.id)) {
Expand All @@ -192,6 +198,7 @@ function getResumeTab(exclude) {
}

const resumableMedia = Array.from(tabs).filter(id => id !== exclude);

if (resumableMedia.length > 0) {
return resumableMedia.pop();
}
Expand Down Expand Up @@ -258,8 +265,14 @@ chrome.commands.onCommand.addListener(async command => {
case 'togglePlayback':
var result = getResumeTab();
if (result !== false) {
pauseOther(result);
if (state.otherTabs.size === 0) send(result, 'togglePlayback');
// Ignore pause event due to this hotkey
if (state.mediaPlaying === null) {
state.mediaPlaying = result;
play(result);
} else {
state.mediaPlaying = null;
pauseOther(false, false);
}
}
break
case 'next':
Expand Down Expand Up @@ -359,6 +372,7 @@ function remove(tabId) {
state.otherTabs.delete(tabId);
state.backgroundaudio.delete(tabId);
state.mutedTabs.delete(tabId);
state.legacyMedia.delete(tabId);
onPause(tabId);
}

Expand Down
2 changes: 1 addition & 1 deletion manifest-chrome.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AutoPause",
"version": "2.8.3",
"version": "2.8.4",
"description": "Pause other audio and video sources if audio is playing on active tab with automatic resume",
"content_security_policy": {
"extension_pages": "default-src 'none'; script-src 'self'; frame-ancestors 'none'; form-action 'none'; upgrade-insecure-requests; block-all-mixed-content"
Expand Down
2 changes: 1 addition & 1 deletion manifest-firefox.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AutoPause",
"version": "2.8.3",
"version": "2.8.4",
"description": "Pause other audio and video sources if audio is playing on active tab with automatic resume",
"content_security_policy": {
"extension_pages": "default-src 'none'; script-src 'self'; frame-ancestors 'none'; form-action 'none'; upgrade-insecure-requests; block-all-mixed-content"
Expand Down
1 change: 1 addition & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>After changes press enter.</h1>
<p>This permission is needed to pause, resume and fast forward media its not needed to detect audio.</p><br>
<div><label for="nopermission"> No permission mode, discards tabs when needed.</label><input type="checkbox" id="nopermission"></div>
<div><label for="disableresume"> Disable automatic resume</label><input type="checkbox" id="disableresume"></div>
<div><label for="resumelimit"> Dont resume old media</label><input type="checkbox" id="resumelimit"></div>
<div><label for="pauseoninactive"> Pause media on tab change and limit resume to the active and marked tabs</label><input type="checkbox" id="pauseoninactive"></div>
<div><label for="ignoretabchange"> Ignore tab changes</label><input type="checkbox" id="ignoretabchange"></div>
<div><label for="muteonpause"> Mute tab when its paused (unmuting a tab will resume it, no tab switching needed)</label><input type="checkbox" id="muteonpause"></div>
Expand Down
2 changes: 1 addition & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var permissions = [];
var options = {};

// ID for each checkbox
const supported = ['disableresume', 'pauseoninactive', 'multipletabs', 'ignoretabchange', 'muteonpause', 'ignoreother', 'nopermission', 'permediapause', 'checkidle'];
const supported = ['disableresume', 'pauseoninactive', 'multipletabs', 'ignoretabchange', 'muteonpause', 'ignoreother', 'nopermission', 'permediapause', 'checkidle','resumelimit'];

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

Expand Down
Loading