在 2022 年左右,我曾经能够使用 javascript 关闭 YouTube 窗口
var openedWindow;
function openWindow() {
openedWindow = window.open('https://www.youtube.com/watch?
v=TC1Vfoq3PvU&ab_channel=Historyboy87','_blank', 'location=yes,height=400,width=400,scrollbars=yes,status=yes');
}
function closeOpenedWindow() {
openedWindow.close();
}
这不再有效。已在 Chrome、Firefox 和 Edge 中尝试过。它将关闭其他网站,但不会关闭 YouTube。
您只能在 iframe 中嵌入 Youtube 视频,并且只有在它们不是很受欢迎的情况下才有效(即:大多数具有大量观看次数的音乐视频都会被屏蔽)。
这是我的做法,但是......它基于 data:text/html 动态创建一个“可打开”页面,我不确定这是否仍然适用于最新的浏览器版本!
它可能因为“安全”原因(读作愚蠢的原因)而被放弃,就像许多其他美妙的事情一样;以及我使用带有旧引擎的自定义浏览器的原因。 :)
无论如何,如果它确实在最新的商业浏览器中工作,请按以下步骤操作...
let YT='https://www.youtube-nocookie.com/embed/vWq-mIxvH3k'; // change the last part after the slash to play another video
let Url='data:text/html;charset=utf-8,<html><head><title>Embedded video</title></head><body>'+
'<iframe width="0" height="0" src="'+YT+'" frameborder="0" style="position:absolute;left:0;top:0;width:100%;height:100%;background:black;" allowfullscreen>'+
'</iframe></body></html>';
let a=document.createElement('A');
a.target='_blank';
a.href=Url;
a.click();