【发布时间】: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,我必须使用 <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/…
-
嗨达林,我不是专家。我已经尽力回答你提到的问题。希望对您有所帮助。