Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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 Apr 28, 2020

More info about this bookmarklet on this page

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