【发布时间】:2016-01-08 19:37:37
【问题描述】:
作者注
在受到cmets的启发后,我想我可以自己实现它。感谢您的光临。
原始问题
例如,我想在页面中弹出嵌入的 YouTube 视频
https://www.udacity.com/course/viewer#!/c-ud037/l-1649018590/m-1659588540
看起来像这样
youtube html5 播放器作为 iframe 嵌入
<iframe data-ng-controller="videoPlayer" class="ng-scope" frameborder="0" allowfullscreen="1" title="YouTube video player" width="930" height="523" src="https://www.youtube.com/embed/OATi2nCjEoE?autoplay=1&color=red&controls=1&rel=0&showinfo=0&fs=1&theme=light&wmode=opaque&html5=1&enablejsapi=1&origin=https%3A%2F%2Fwww.udacity.com" id="widget2"></iframe>
我希望播放器可以在带有 URL 的新窗口中弹出:
感谢 JaromandaX,我的临时脚本如下:
// ==UserScript==
// @name My Fancy New Userscript
// @namespace http://your.homepage/
// @version 0.1
// @description enter something useful
// @author You
// @include https://www.udacity.com/course/viewer*
// @grant none
// ==/UserScript==
$(document).ready(function()
{
setTimeout(function(){
var button = document.createElement('button');
button.type = 'button';
button.appendChild(document.createTextNode('Pop Youtube'));
button.setAttribute("onclick", "window.open(document.getElementById('widget2').src, 'my video');")
document.querySelectorAll("[class=row-gap-small]")[0].appendChild(button)
}, 2000);
});
我的问题是,如图所示通过按钮切换 youtube 播放器的内容后,iframe src 没有改变。因此,单击该按钮仍会将我带到上一课的 youtube 播放器。
【问题讨论】:
-
扩展 (firefox/chrome) 或用户脚本 (greasemonkey/tampermonkey) 将能够满足您的需求。不知道如何使用 IE 或 Edge 来做这件事 - 显然 Edge 会得到一些扩展的好处,但最后我听说,它是“即将到来”,而不是“这里”
-
@JaromandaX 我很想通过 Chrome 中的扩展程序或用户脚本来实现这一点
-
我建议从 tampermonkey 开始,然后,一旦您对代码感到满意,如果您愿意,可以将其转换为扩展。这就是我有时处理 Firefox 扩展的方式。但是,我不熟悉 chrome 扩展,因此,不确定将用户脚本移植到 chrome 扩展有多么容易(使用greasemonkey -> firefox 扩展非常简单)
-
@JaromandaX 我尝试使用 JavaScript 访问 iframe 以获取
src,但它说由于相同的来源政策,我不允许这样做 -
这根本不应该是一个问题,因为 iframe 是您文档的一个元素,我认为由于 src 属性只是一个字符串,所以根本没有域问题。也许你可以展示一些(最小的)代码,就像你说的那样
标签: javascript html youtube html5-fullscreen