在Chrome扩展中,内容脚本不会影响iframe内部? [英] In Chrome extension, content scripts can't affect inside the iframe?
问题描述
版本:1.0,
background_page:background.html,
权限:[contextMenus,tabs,http://*.example.com/*],
content_scripts:[
{
匹配:
[
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,我必须使用< script src = ...
将它再次包含在iframe中。
我不想再次包含jQuery,因为我已经将它放入扩展中。我不希望用户一次又一次地在扩展需要运行的每个页面上下载jQuery。
我已经试过属性all_frames:true
,但它不起作用。
请指教。感谢。
编辑:我将example.com添加到匹配
属性中,如下所示:
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>无标题< / title>
< / head>
< body>
< div>< / div>
< script type =text / javascript>
$('div')。html('hi');
< / script>
< / body>
< / html>
会有错误:$未定义
为了使它工作,我必须使用:
<!DOCTYPE HTML>
< html>
< head>
< title>无标题< / title>
< / head>
< body>
< div>< / div>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js><<< ; /脚本>
< script type =text / javascript>
$('div')。html('hi');
< / script>
< / body>
< / html>
您需要添加iframe的网址<$ c $
>content_scripts:[
{
匹配:
[
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
}
]
In the manifest.json file, I declare that I want to inject some scripts, like this:
{
"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
}
]
}
In the content script, I create an iframe, among other things. It works fine so far. Like this:
$('<div id="my_notifier"></div>').appendTo($('body')).html('<iframe src="http://example.com"></iframe>');
The problem is, inside the iframe, it does not inherit anything from the content scripts. If I want to use jQuery, I have to use <script src=...
to include it again inside the iframe.
I prefer not to include jQuery again because I already put it in the extension. I don't want the user to download jQuery again and again on every page that the extension needs to run on.
I've tried the attribute "all_frames": true
, but it doesn't work.
Please advise. Thanks.
Edit: I added example.com to the matches
attribute like this:
"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
}
]
But it does not work.
To be clearer, say the contents of the iframe (example.com) is:
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<div></div>
<script type="text/javascript">
$('div').html('hi');
</script>
</body>
</html>
There will be an error: $ is not defined
To make it work, I have to use:
<!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>
You need to add your iframe's url http://example.com
to the list and specify which content scripts to inject:
"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
}
]
这篇关于在Chrome扩展中,内容脚本不会影响iframe内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!