Google chrome extension 在Chrome扩展中,内容脚本可以';不影响iframe内部?
在manifest.json文件中,我声明要注入一些脚本,如下所示:Google chrome extension 在Chrome扩展中,内容脚本可以';不影响iframe内部?,google-chrome-extension,Google Chrome Extension,在manifest.json文件中,我声明要注入一些脚本,如下所示: { "name": "my extension", "version": "1.0", "background_page": "background.html", "permissions": ["contextMenus", "tabs", "http://*.example.com/*"], "content_scripts": [ {
{
"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
}
]
}
"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。到目前为止效果还不错。像这样:
$('<div id="my_notifier"></div>').appendTo($('body')).html('<iframe src="http://example.com"></iframe>');
但它不起作用
更清楚地说,假设iframe(example.com)的内容是:
无标题
$('div').html('hi');
将出现错误:$未定义
要使其工作,我必须使用:
<!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>
无标题
$('div').html('hi');
您需要添加iframe的urlhttp://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
}
]
答案很简单:
- 检查新选项卡是否已更新
- 你的网址是什么
- 执行您想要的脚本
你能用
所有帧集和创建iframe的内容脚本代码显示你的完整清单吗?好的,我已经编辑了这个问题。嘿,贝蒂,既然Serg已经解决了最初的问题,那么在等待加载帧访问它时看看这个问题:嗨,达林,我不是专家。我已尽力回答你提到的问题。希望能有所帮助。我已经尝试过你的方法,还有一种方法是简单地将example.com添加到matches
属性中,但没有成功。请参见已编辑的问题。PS:我按enter键获得一个新行,我惊讶地发现评论被发送了。>@贝蒂,这不是你想要的方式。内容脚本是沙盒的,常规页面无法访问作为内容脚本注入的jquery。如果example.com frame是您的扩展的一部分,并且您可以完全控制它,那么您可以将该页面中的所有javascript移动到内容脚本中,并将其与jquery一起注入。谢谢@serg。我的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
}
]