Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.
< YouTube /embed/ forwarderについてのフィードバック
Ok,I've updated it - it should work now.
YouTube changed how embeds work - they now can't be accessed without a referer (or something like that, I'm not sure) - so I added a redirect spoof so it gets its referer.
If you're on a dark theme like I am and the tab flashes white for a moment, sorry. I can't figure out how to get rid of that.
Thank you for your fast reponse.
Also i just found out that you can embed a playlist like this:
Normal: https://www.youtube.com/watch?v=kPa7bsKwL-c&list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU&index=2
Embed: https://www.youtube.com/embed/kPa7bsKwL-c?list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU
Can you do that too?
Nvm, i figured that out.
(function () {
const embedBaseUrl = 'https://www.youtube.com/embed/';
function getIds(u) {
const videoMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(u);
const playlistMatch = /(?:[?&]list=)([^&?\/]+)/.exec(u);
if (videoMatch) {
if (playlistMatch) {
return { type: 'playlist', videoId: videoMatch[1], playlistId: playlistMatch[1] };
} else {
return { type: 'video', videoId: videoMatch[1] };
}
}
return null;
}
function createSpoofPage(embedUrl) {
const html = `
Redirecting to YouTube embed...
`;
const blob = new Blob([html], { type: 'text/html' });
return URL.createObjectURL(blob);
}
const url = window.location.href;
const ids = getIds(url);
if (ids) {
let embedUrl = '';
// embed URL with autoplay for both normal videos, playlist videos and shorts
if (ids.type === 'video') {
embedUrl = `${embedBaseUrl}${ids.videoId}?autoplay=1`;
} else if (ids.type === 'playlist') {
embedUrl = `${embedBaseUrl}${ids.videoId}?list=${ids.playlistId}&autoplay=1`;
}
if (embedUrl !== url) {
const spoofPage = createSpoofPage(embedUrl);
window.location.href = spoofPage;
}
}
})();
Alright @banon, I've added the playlist embedding, but also, improved the way YouTube's new referer requirement is handled. The userscript now no longer needs to create an interstitial blob: spoof page that it redirects from. Now everything should be as smooth as it used to be.
Thanks you. Just a heads up, it's not possible to have an embedded playlist shows more than 200 videos. It seems like youtube placed a hard restriction on this. If you click on the 201st video of a big playlist like this one https://www.youtube.com/playlist?list=PLrhpb4TQr-uKzxOB1C_9x-Ysrj2WRMZN_, here's the 201st video link https://www.youtube.com/watch?v=6CqXgs-7ico&list=PLrhpb4TQr-uKzxOB1C_9x-Ysrj2WRMZN_&index=201&pp=iAQB8AUB, the script will redirect you to the first video. Here's my workaround: (function () {
const EMBED_BASE = 'https://www.youtube.com/embed/';
const ID_REGEX = /(?:[?&]v=|\/(?:embed\/|v\/))([^&?/]+)|(?:[?&]list=)([^&?/]+)/g;
function parseYouTubeUrl(url) {
const parsed = new URL(url);
const params = parsed.searchParams;
let videoId = null;
let playlistId = null;
let match;
while ((match = ID_REGEX.exec(url)) !== null) {
if (match[1]) videoId = match[1];
if (match[2]) playlistId = match[2];
}
// Extract playlist index if present (used for >200 video workaround)
const index = params.has('index') ? parseInt(params.get('index'), 10) : null;
if (!videoId) return null;
return playlistId
? { type: 'playlist', videoId, playlistId, index }
: { type: 'video', videoId, index };
}
function ensureReferrerMeta() {
const head = document.head || document.documentElement;
if (!head.querySelector('meta[name="referrer"]')) {
const meta = document.createElement('meta');
meta.name = 'referrer';
meta.content = 'origin';
head.prepend(meta);
}
}
const currentUrl = window.location.href;
const ids = parseYouTubeUrl(currentUrl);
if (!ids) return;
let embedUrl;
// playlist video beyond 200th index (bypass youtube embed restriction)
if (ids.index > 200) {
embedUrl = `${EMBED_BASE}${ids.videoId}?autoplay=1`;
} else if (ids.type === 'video') {
embedUrl = `${EMBED_BASE}${ids.videoId}?autoplay=1`;
} else if (ids.type === 'playlist') {
embedUrl = `${EMBED_BASE}${ids.videoId}?list=${ids.playlistId}&autoplay=1`;
}
if (embedUrl && embedUrl !== currentUrl) {
ensureReferrerMeta();
window.location.replace(embedUrl);
}
})();
Can you update the script please?