【问题标题】:Firefox & Chrome - different behavior for iframeFirefox 和 Chrome - iframe 的不同行为
【发布时间】:2016-02-09 15:31:50
【问题描述】:

我在我的网站上添加了一个 youtube 视频(例如 <iframe width="640" height="360" src="https://www.youtube.com/embed/QE5KOfjKLy0" frameborder="0" allowfullscreen></iframe>

使用 Firefox 的扩展程序,我可以获取 youtube 嵌入的网址 视频中键点击视频的标题或“youtube 按钮”(见下面的截图)。

我不知道为什么,但它可以工作,我可以在“浏览器控制台”中看到视频的 url(对于 Firefox,ctrl+shift+j)。

但是 Chromium-browsers 有不同的行为,它只适用于当前页面上的简单 url (<a href="...">...</a>),而不是来自 iframe 的 url。

如果 Chrome 也能做到这一点,那就太好了。

有人知道为什么 Firefox 和 Chrome 会有所不同吗?

此处的内容脚本(用于 Firefox 和 Chromium):

(function (undefined) {
    'use strict';
    var YoutubeExtActions = {
        linkClickListener : function(e) {
            if(e.button === 1) {
                var link = $(e.target).closest('a');    
                if(link.length && link.attr('href') !== '#') {
                    var url = link[0].href;
                    if(url.match(/youtube.com\/watch/i) || url.match(/youtube.com\/embed/i)) {
                        e.preventDefault();
                        e.stopPropagation();
                        console.log("you got it: " + url);
                    }
                }
            }
        }
    }
window.addEventListener("click",YoutubeExtActions.linkClickListener,false);
})();

Chrome 中的 JS 仅适用于常见链接(不适用于 iframe)

manifest.json:

{
   "content_scripts": [ {
      "js": [ "jquery-1.11.3.min.js", "content_script.js" ],
      "matches": [ "<all_urls>" ],
      "run_at": "document_idle"
   } ],
   "description": "chromium youtube iframe get url",
   "manifest_version": 2,
   "name": "chromium youtube iframe get url",
   "version": "1.0"
}

【问题讨论】:

    标签: javascript jquery iframe google-chrome-extension


    【解决方案1】:

    为了在 iframe 中自动插入内容脚本,请在 manifest.json 中添加 "all_frames": true

    "content_scripts": [ {
      "js": .................,
      "matches": .................,
      "all_frames": true
    } ],
    

    文档:

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2013-04-29
      • 1970-01-01
      • 2018-02-16
      • 2015-10-11
      • 2015-10-17
      • 2014-10-24
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多