Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gvenk/85d31c608e4a0ea57ac10d814514b002 to your computer and use it in GitHub Desktop.
Save gvenk/85d31c608e4a0ea57ac10d814514b002 to your computer and use it in GitHub Desktop.
Convert all youtube urls on a webpage to youtube-nocookie.com urls
javascript:void%20function(){const%20a=document.querySelectorAll(%22a%22);a.forEach(a=%3E{if(-1!==a.href.indexOf(%22youtube.com%22)){const%20b=new%20URL(a.href).searchParams,c=b.get(%22v%22);c%26%26(a.href=%22https://www.youtube-nocookie.com/embed/%22+c)}else-1!==a.href.indexOf(%22youtu.be%22)%26%26(a.href=a.href.replace(%22//youtu.be/%22,%22//www.youtube-nocookie.com/embed/%22))});const%20b=document.querySelectorAll(%22iframe%22);b.forEach(a=%3E{-1===a.src.indexOf(%22//www.youtube.com%22)%3F-1!==a.src.indexOf(%22//youtube.com%22)%26%26(a.src=a.src.replace(%22//youtube.com/%22,%22//www.youtube-nocookie.com/%22)):a.src=a.src.replace(%22//www.youtube.com/%22,%22//www.youtube-nocookie.com/%22)})}();
// check links
const allUrls = document.querySelectorAll("a");
allUrls.forEach((urlElement) => {
if (urlElement.href.indexOf("youtube.com") !== -1) {
const search = new URL(urlElement.href).searchParams;
const videoId = search.get("v");
if (videoId) {
urlElement.href = "https://www.youtube-nocookie.com/embed/" + videoId;
}
} else if (urlElement.href.indexOf("youtu.be") !== -1) {
urlElement.href = urlElement.href.replace(
"//youtu.be/",
"//www.youtube-nocookie.com/embed/"
);
}
});
// check iframes for embeds
const allEmbedUrls = document.querySelectorAll("iframe");
allEmbedUrls.forEach((urlElement) => {
if (urlElement.src.indexOf("//www.youtube.com") !== -1) {
urlElement.src = urlElement.src.replace(
"//www.youtube.com/",
"//www.youtube-nocookie.com/"
);
} else if (urlElement.src.indexOf("//youtube.com") !== -1) {
urlElement.src = urlElement.src.replace(
"//youtube.com/",
"//www.youtube-nocookie.com/"
);
}
});
@gvenk
Copy link
Author

gvenk commented May 16, 2024

Hello,

Just need to create a .js file ? And where do i need to add it on the webpage?

Thanks

@ianermasi It is meant to be used as a 'bookmarklet': https://en.wikipedia.org/wiki/Bookmarklet. You can use the link on this page and drag it to your bookmark folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment