在Chrome扩展程序中,内容脚本不会影响iframe内部?

Bet*_*tty 5 google-chrome-extension

在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
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

问题是,在iframe内部,它不会从内容脚本继承任何内容.如果我想使用jQuery,我必须使用<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
    }
]
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

更清楚的是,iframe(example.com)的内容是:

<!DOCTYPE HTML>

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

<body>

<div></div>

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

</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

将出现错误:$未定义

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

<!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>
Run Code Online (Sandbox Code Playgroud)

ser*_*erg 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
        }
    ]
Run Code Online (Sandbox Code Playgroud)