gpt4 book ai didi

google-chrome-extension - 在 Chrome 扩展中,内容脚本不能影响 iframe 内部?

转载 作者:行者123 更新时间:2023-12-04 21:24:08 25 4
gpt4 key购买 nike

在 manifest.json 文件中,我声明我要注入(inject)一些脚本,如下所示:

{
"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>

最佳答案

您需要添加 iframe 的网址 http://example.com到列表并指定要注入(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
}
]

关于google-chrome-extension - 在 Chrome 扩展中,内容脚本不能影响 iframe 内部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6641729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com