Skip to content

Commit 8198963

Browse files
committed
Fix manual skip, cross platform
1 parent b20788d commit 8198963

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"manifest_version": 3,
66
"permissions": [
77
"storage",
8-
"activeTab"
8+
"activeTab",
9+
"scripting"
910
],
1011
"content_scripts": [
1112
{

popup.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
function setOption(option) {
2-
return (event) => {
3-
settings[option] = event.target.checked
4-
updateStorage()
5-
}
6-
}
7-
8-
function updateStorage() {
9-
console.log('update storage:', settings)
10-
browser.storage.sync.set({ skipButtonSettings: settings })
11-
}
1+
const autoSkipCheck = document.getElementById('autoSkip')
2+
const hideSponsoredCheck = document.getElementById('hideSponsored')
3+
const manualSkipBtn = document.getElementById('manualSkip')
124

13-
function sendManualSkip() {
14-
browser.tabs.query({
15-
active: true,
16-
currentWindow: true
17-
}).then(tabs => tabs.forEach(tab => {
18-
browser.tabs.executeScript(tab.id, { code: 'manualSkip()' })
19-
}))
20-
}
5+
autoSkipCheck.addEventListener('input', setOption('autoSkip'))
6+
hideSponsoredCheck.addEventListener('input', setOption('hideSponsored'))
7+
manualSkipBtn.addEventListener('click', sendManualSkip)
218

229
// Default settings
2310
let settings = {
2411
autoSkip: false,
2512
hideSponsored: true
2613
}
2714

28-
const autoSkipCheck = document.getElementById('autoSkip')
29-
const hideSponsoredCheck = document.getElementById('hideSponsored')
30-
const manualSkipBtn = document.getElementById('manualSkip')
31-
15+
// Load current settings and set popup values
3216
browser.storage.sync.get('skipButtonSettings').then(({ skipButtonSettings }) => {
3317
if (skipButtonSettings) {
34-
console.log('settings loaded:', skipButtonSettings)
3518
settings = skipButtonSettings
3619
} else {
3720
updateStorage()
@@ -40,6 +23,25 @@ browser.storage.sync.get('skipButtonSettings').then(({ skipButtonSettings }) =>
4023
hideSponsoredCheck.checked = settings.hideSponsored
4124
})
4225

43-
autoSkipCheck.addEventListener('input', setOption('autoSkip'))
44-
hideSponsoredCheck.addEventListener('input', setOption('hideSponsored'))
45-
manualSkipBtn.addEventListener('click', sendManualSkip)
26+
function setOption(option) {
27+
return (event) => {
28+
settings[option] = event.target.checked
29+
updateStorage()
30+
}
31+
}
32+
33+
function updateStorage() {
34+
browser.storage.sync.set({ skipButtonSettings: settings })
35+
}
36+
37+
function sendManualSkip() {
38+
browser.tabs.query({
39+
active: true,
40+
currentWindow: true
41+
}).then(tabs => tabs.forEach(tab => {
42+
browser.scripting.executeScript({
43+
target: { allFrames: true, tabId: tab.id },
44+
func: () => typeof manualSkip !== 'undefined' && manualSkip()
45+
})
46+
}))
47+
}

skipButton.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ function check(mutations) {
6868
const sponsored = addedNodes.filter(node =>
6969
['YTD-AD-SLOT-RENDERER', 'YTD-PLAYER-LEGACY-DESKTOP-WATCH-ADS-RENDERER'].includes(node.tagName)
7070
)
71-
if (adStarted) log('@@@ STARTED')
72-
if (adStopped) log('@@@ STOPPED')
73-
if (adEndScreen) log('@@@ ENDSCREEN')
7471

7572
if (adStarted) handleAd()
7673
if (adStopped && skipButton) {
@@ -81,8 +78,8 @@ function check(mutations) {
8178
if (sponsored.length) handleSponsored(sponsored)
8279

8380
if (!initialCheck) {
81+
// Check if ad was already playing before observer started
8482
log('initial check...')
85-
// Check if ad already playing
8683
handleAd(true)
8784
initialCheck = true
8885
}
@@ -100,7 +97,6 @@ function handleAd(needToCheck) {
10097
skipButton.onclick = () => skip(videoNode)
10198
const target = videoNode.parentElement.parentElement
10299
target.appendChild(skipButton)
103-
console.log(videoNode, target, skipButton)
104100
}
105101
}
106102
}
@@ -154,13 +150,6 @@ async function init() {
154150
// Manually skip any running video
155151
function manualSkip() {
156152
document.querySelectorAll('video').forEach(video => skip(video))
157-
// Propagate to all child iframes
158-
document.querySelectorAll('iframe').forEach(iframe => {
159-
const iframeWindow = iframe.contentWindow
160-
if (iframeWindow) {
161-
iframeWindow.postMessage('manualSkip', '*')
162-
}
163-
})
164153
}
165154

166155
console.log(`skipButton.js: loaded (host: ${window.location.host})`)
@@ -173,4 +162,3 @@ if (isYoutube) {
173162
browser.storage.onChanged.addListener(changes => {
174163
if (isYoutube && changes.skipButtonSettings) updateSettings(changes.skipButtonSettings.newValue)
175164
})
176-
window.addEventListener('message', message => { if (message.data === 'manualSkip') manualSkip() })

0 commit comments

Comments
 (0)