Skip to content

Commit

Permalink
Fix manual skip, cross platform
Browse files Browse the repository at this point in the history
  • Loading branch information
cornzz committed Nov 17, 2023
1 parent b20788d commit 8198963
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"manifest_version": 3,
"permissions": [
"storage",
"activeTab"
"activeTab",
"scripting"
],
"content_scripts": [
{
Expand Down
56 changes: 29 additions & 27 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
function setOption(option) {
return (event) => {
settings[option] = event.target.checked
updateStorage()
}
}

function updateStorage() {
console.log('update storage:', settings)
browser.storage.sync.set({ skipButtonSettings: settings })
}
const autoSkipCheck = document.getElementById('autoSkip')
const hideSponsoredCheck = document.getElementById('hideSponsored')
const manualSkipBtn = document.getElementById('manualSkip')

function sendManualSkip() {
browser.tabs.query({
active: true,
currentWindow: true
}).then(tabs => tabs.forEach(tab => {
browser.tabs.executeScript(tab.id, { code: 'manualSkip()' })
}))
}
autoSkipCheck.addEventListener('input', setOption('autoSkip'))
hideSponsoredCheck.addEventListener('input', setOption('hideSponsored'))
manualSkipBtn.addEventListener('click', sendManualSkip)

// Default settings
let settings = {
autoSkip: false,
hideSponsored: true
}

const autoSkipCheck = document.getElementById('autoSkip')
const hideSponsoredCheck = document.getElementById('hideSponsored')
const manualSkipBtn = document.getElementById('manualSkip')

// Load current settings and set popup values
browser.storage.sync.get('skipButtonSettings').then(({ skipButtonSettings }) => {
if (skipButtonSettings) {
console.log('settings loaded:', skipButtonSettings)
settings = skipButtonSettings
} else {
updateStorage()
Expand All @@ -40,6 +23,25 @@ browser.storage.sync.get('skipButtonSettings').then(({ skipButtonSettings }) =>
hideSponsoredCheck.checked = settings.hideSponsored
})

autoSkipCheck.addEventListener('input', setOption('autoSkip'))
hideSponsoredCheck.addEventListener('input', setOption('hideSponsored'))
manualSkipBtn.addEventListener('click', sendManualSkip)
function setOption(option) {
return (event) => {
settings[option] = event.target.checked
updateStorage()
}
}

function updateStorage() {
browser.storage.sync.set({ skipButtonSettings: settings })
}

function sendManualSkip() {
browser.tabs.query({
active: true,
currentWindow: true
}).then(tabs => tabs.forEach(tab => {
browser.scripting.executeScript({
target: { allFrames: true, tabId: tab.id },
func: () => typeof manualSkip !== 'undefined' && manualSkip()
})
}))
}
14 changes: 1 addition & 13 deletions skipButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ function check(mutations) {
const sponsored = addedNodes.filter(node =>
['YTD-AD-SLOT-RENDERER', 'YTD-PLAYER-LEGACY-DESKTOP-WATCH-ADS-RENDERER'].includes(node.tagName)
)
if (adStarted) log('@@@ STARTED')
if (adStopped) log('@@@ STOPPED')
if (adEndScreen) log('@@@ ENDSCREEN')

if (adStarted) handleAd()
if (adStopped && skipButton) {
Expand All @@ -81,8 +78,8 @@ function check(mutations) {
if (sponsored.length) handleSponsored(sponsored)

if (!initialCheck) {
// Check if ad was already playing before observer started
log('initial check...')
// Check if ad already playing
handleAd(true)
initialCheck = true
}
Expand All @@ -100,7 +97,6 @@ function handleAd(needToCheck) {
skipButton.onclick = () => skip(videoNode)
const target = videoNode.parentElement.parentElement
target.appendChild(skipButton)
console.log(videoNode, target, skipButton)
}
}
}
Expand Down Expand Up @@ -154,13 +150,6 @@ async function init() {
// Manually skip any running video
function manualSkip() {
document.querySelectorAll('video').forEach(video => skip(video))
// Propagate to all child iframes
document.querySelectorAll('iframe').forEach(iframe => {
const iframeWindow = iframe.contentWindow
if (iframeWindow) {
iframeWindow.postMessage('manualSkip', '*')
}
})
}

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

0 comments on commit 8198963

Please sign in to comment.