From 16fab20d08bf1cc5f83de9e3bdf22d163fc7f543 Mon Sep 17 00:00:00 2001 From: Peter Snyder Date: Fri, 13 Sep 2024 18:24:21 -0500 Subject: [PATCH] scriptlet to rewite src attributes of youtube-nocookie iframes, fixes brave/adblock-resources#212 --- resources/fix-youtube-nocookie-embeds.js | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 resources/fix-youtube-nocookie-embeds.js diff --git a/resources/fix-youtube-nocookie-embeds.js b/resources/fix-youtube-nocookie-embeds.js new file mode 100644 index 0000000..a6243b1 --- /dev/null +++ b/resources/fix-youtube-nocookie-embeds.js @@ -0,0 +1,34 @@ +(() => { + 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 }) +})()