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
27 changes: 22 additions & 5 deletions ContentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,24 @@ function onPlay(e) {
let data = Elements.get(e);
if (isMuted(e)) {
send('playMuted');
} else {
send('play', data.id);
return;
}
// If duration is unknown, wait for metadata before reporting to background.
// This lets the background correctly identify short media (e.g. notification sounds).
if (isNaN(e.duration)) {
let sent = false;
const sendOnce = () => {
if (sent) return;
sent = true;
if (!isPaused(e) && !isMuted(e)) {
send('play', data.id, e.duration);
}
};
e.addEventListener('durationchange', sendOnce, {once: true});
setTimeout(sendOnce, 500);
return;
}
send('play', data.id, e.duration);
}

function validMedia(e) {
Expand Down Expand Up @@ -336,12 +351,14 @@ function checkDOM() {
}
}

function send(message, body = '') {
chrome.runtime.sendMessage({
function send(message, body = '', duration) {
const msg = {
type: message,
body: body,
userActivation: navigator.userActivation.isActive
});
};
if (duration !== undefined) msg.duration = duration;
chrome.runtime.sendMessage(msg);
}

window.addEventListener(
Expand Down
37 changes: 36 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var state = {};

const resumelimit = 5;
const shortMediaDuration = 3; // seconds
const shortMediaDebounce = 5000; // ms, for tabs without content script access
const pendingAudible = new Map();
const setItems = [
'media',
'backgroundaudio',
Expand Down Expand Up @@ -127,6 +130,14 @@ chrome.runtime.onMessage.addListener(async (message, sender) => {
state.mutedMedia.add(sender.tab.id);
onMute(sender.tab.id);
} else {
// Ignore short media (e.g. notification sounds) when option is enabled.
if (
hasProperty(options, 'ignoreshort') &&
isFinite(message.duration) &&
message.duration > 0 &&
message.duration < shortMediaDuration
)
break;
state.mutedMedia.delete(sender.tab.id);
state.media.add(sender.tab.id);
onPlay(sender.tab, message.body, message.userActivation);
Expand Down Expand Up @@ -449,6 +460,10 @@ function remove(tabId) {
state.backgroundaudio.delete(tabId);
state.mutedTabs.delete(tabId);
state.legacyMedia.delete(tabId);
if (pendingAudible.has(tabId)) {
clearTimeout(pendingAudible.get(tabId));
pendingAudible.delete(tabId);
}
onPause(tabId);
}

Expand Down Expand Up @@ -487,8 +502,28 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (hasProperty(options, 'ask'))
chrome.permissions.addHostAccessRequest({tabId: tabId});
}
onPlay(tab);
// Debounce otherTabs when ignoreshort is enabled to filter notification sounds.
if (
hasProperty(options, 'ignoreshort') &&
state.otherTabs.has(tabId)
) {
if (pendingAudible.has(tabId)) clearTimeout(pendingAudible.get(tabId));
pendingAudible.set(
tabId,
setTimeout(() => {
pendingAudible.delete(tabId);
if (state.otherTabs.has(tabId)) onPlay(tab);
}, shortMediaDebounce)
);
} else {
onPlay(tab);
}
} else {
// Cancel pending debounce if tab stopped being audible.
if (pendingAudible.has(tabId)) {
clearTimeout(pendingAudible.get(tabId));
pendingAudible.delete(tabId);
}
state.otherTabs.delete(tabId);
onPause(tabId);
}
Expand Down
5 changes: 5 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ <h1>After changes press enter.</h1>
<label for="ignoreother"> Ignore media from other tabs</label
><input type="checkbox" id="ignoreother" />
</div>
<div>
<label for="ignoreshort">
Ignore short media under 3 seconds (e.g. notification sounds)</label
><input type="checkbox" id="ignoreshort" />
</div>
<div>
<label for="nopermission">
No permission mode, discards tabs when needed.</label
Expand Down
1 change: 1 addition & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const supported = [
'ignoretabchange',
'muteonpause',
'ignoreother',
'ignoreshort',
'nopermission',
'permediapause',
'checkidle',
Expand Down
Loading