【问题标题】:In Chrome extension, content scripts can't affect inside the iframe?在 Chrome 扩展中,内容脚本不能影响 iframe 内部?
【发布时间】:2011-07-10 14:42:49
【问题描述】:

在 manifest.json 文件中,我声明要注入一些脚本,如下所示:

{
    "name": "my extension",

    "version": "1.0",

    "background_page": "background.html",

    "permissions": ["contextMenus", "tabs", "http://*.example.com/*"],

    "content_scripts": [
        {
            "matches":
                [
                "http://*.taobao.com/*",
                "http://*.yintai.com/*"
                ],
            "run_at": "document_idle",
            "js": ["jquery-1.5.1.min.js","jquery-ui-1.8.13.custom.min.js", "contentscript.js"],
            "all_frames": true
        }
    ]
}

在内容脚本中,我创建了一个 iframe 等。到目前为止它工作正常。像这样:

$('<div id="my_notifier"></div>').appendTo($('body')).html('<iframe src="http://example.com"></iframe>');

问题是,在 iframe 内部,它没有从内容脚本继承任何内容。如果我想使用 jQuery,我必须使用 &lt;script src=... 将其再次包含在 iframe 中。

我不想再次包含 jQuery,因为我已经把它放在了扩展中。我不希望用户在需要运行扩展的每个页面上一次又一次地下载 jQuery。

我试过属性"all_frames": true,但是不行。

请指教。谢谢。

编辑:我将 example.com 添加到 matches 属性,如下所示:

"content_scripts": [
    {
        "matches":
            [
            "http://*.taobao.com/*",
            "http://*.yintai.com/*", 
            "http://*.example.com/*"
            ],
        "run_at": "document_idle",
        "js": ["jquery-1.5.1.min.js","jquery-ui-1.8.13.custom.min.js", "contentscript.js"],
        "all_frames": true
    }
]

但它不起作用。

为了更清楚,假设 iframe (example.com) 的内容是:

<!DOCTYPE HTML>

<html>
<head>
    <title>Untitled</title>
</head>

<body>

<div></div>

<script type="text/javascript">
    $('div').html('hi');  

</script>

</body>
</html>

会有错误:$未定义

为了让它工作,我必须使用:

<!DOCTYPE HTML>

<html>
<head>
    <title>Untitled</title>
</head>

<body>

<div></div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
    $('div').html('hi');  

</script>

</body>
</html>

【问题讨论】:

  • 您能否展示带有all_frames 集的完整清单以及创建 iframe 的内容脚本代码?
  • 好的,我已经编辑了问题。
  • 嘿 Betty,既然 Serg 已经解决了最初的问题,请在等待框架加载访问它时查看这个问题:stackoverflow.com/questions/4669089/…
  • 嗨达林,我不是专家。我已经尽力回答你提到的问题。希望对您有所帮助。

标签: google-chrome-extension


【解决方案1】:

您需要将 iframe 的 url http://example.com 添加到列表中并指定要注入的内容脚本:

 "content_scripts": [
        {
            "matches":
                [
                "http://*.taobao.com/*",
                "http://*.yintai.com/*"
                ],
            "run_at": "document_idle",
            "js": ["jquery-1.5.1.min.js","jquery-ui-1.8.13.custom.min.js", "contentscript.js"]
        },{
            "matches":["http://example.com/*"],
            "run_at": "document_idle",
            "js": ["jquery-1.5.1.min.js"],
            "all_frames": true
        }
    ]

【讨论】:

  • 我已经尝试过你的方法,另一种方法是简单地将 example.com 添加到 matches 属性,但没有运气。请参阅已编辑的问题。 PS:我按回车换了一行,惊讶地发现评论发送了。 >
  • @Betty 它不会按照你想要的方式工作。内容脚本是沙盒的,常规页面无法访问作为内容脚本注入的 jquery。如果 example.com 框架是您的扩展的一部分并且您可以完全控制它,您可以将该页面中的所有 javascript 移动到内容脚本中,并将其与 jquery 一起注入。
  • 谢谢@serg。我的 example.com 框架的内容是动态生成的,所以我不能把它放在扩展中。我认为这意味着我想要的无法完成。我将不得不使用
【解决方案2】:

答案很简单:

  • 检查新标签是否已更新
  • 是你的网址吗?
  • 执行你想要的脚本

dynamically deploying content scripts in chrome extensions

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 2011-11-02
    • 2012-07-04
    • 2011-04-25
    • 2018-06-21
    • 2011-09-17
    • 2013-05-20
    相关资源
    最近更新 更多