Skip to content

Commit

Permalink
scriptlet to rewite src attributes of youtube-nocookie iframes, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
pes10k committed Sep 13, 2024
1 parent 3ad3f02 commit b2fb87a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions resources/fix-youtube-nocookie-embeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function () {
const ytNoCookiePattern = /(https|http)?:\/\/(www\.)?youtube-nocookie/
const fixYouTubeSrc = (elm) => {
if (!elm.src || !elm.src.match(ytNoCookiePattern)) {
return
}
elm.src.replace('youtube-nocookie.com', 'youtube.com')
}

const mutationHandler = (mutations) => {
for (const aMutation of mutations) {
const { target, attributeName } = aMutation
if (attributeName !== 'src' || target.tagName !== 'IFRAME') {
continue
}
fixYouTubeSrc(target)
}
}

const start = () => {
document
.querySelectorAll('iframe[src*="youtube-nocookie.com"]')
.forEach(fixYouTubeSrc)
const observer = new MutationObserver(mutationHandler)
observer.observe(document, {
attributes: true,
attributeFilter: ['src'],
childList: true,
subtree: true
})
}

self.addEventListener('DOMContentLoaded', start, { once: true })
}());

0 comments on commit b2fb87a

Please sign in to comment.