在Chrome扩展中,内容脚本无法影响iframe内部?

6
在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,我必须在 iframe 中再次使用<script src=... 来包含它。
我希望不要再次包含 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 设置为 true 的完整清单文件以及创建 iframe 的内容脚本代码吗? - serg
嘿,贝蒂,既然Serg已经解决了最初的问题,请看一下这个关于等待框架加载以访问它的问题:http://stackoverflow.com/questions/4669089/how-to-inject-an-iframe-panel-to-a-google-chrome-extension-and-add-click-event-in - Darin
嗨,Darin,我不是专家。我已经尽力回答你提出的问题了。希望能有所帮助。 - Betty
2个回答

5

您需要将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
@Betty,按照你想要的方式是行不通的。内容脚本是沙盒化的,常规页面无法访问作为内容脚本注入其中的jquery。如果example.com框架是您的扩展的一部分,并且您对其拥有完全控制权,则可以将该页面中的所有JavaScript移动到内容脚本中,并将其与jquery一起注入。 - serg
谢谢@serg。我的example.com框架的内容是动态生成的,所以我不能将其放在扩展中。我想这意味着我想要的无法实现。我将不得不使用<script src=...来在iframe内部包含jQuery。 - Betty

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接