【发布时间】:2012-03-06 23:48:29
【问题描述】:
我正在构建我的第一个扩展程序,但在让后台页面加载脚本在内容脚本中工作时遇到问题,这让人怀疑后台页面上加载的任何脚本是否真的可用于所有内容脚本。
我有两个 javascript 文件,functions.js 和 content.js,它们都使用 jQuery。我在后台页面加载 jQuery 和函数脚本以减少每个站点匹配的解析。一次就够了,因为它们都是函数。
manifest.json 无效
{
"name": "extension name",
"background_page": "background.html",
"content_scripts": [ {
"js": [ "js/functions.js", "js/content.js" ],
"matches": [ "http://*.mysite.com/*" ],
"exclude_globs": [ "*board*" ],
} ],
"version": "0.1"
}
注意:使用“include_globs”加载 CDN 的 jQuery 不起作用。模拟 @include 是失败的,所以我不得不在扩展中包含 jQuery。
manifest.json 工作中
{
"name": "extension name",
"background_page": "background.html",
"content_scripts": [ {
"js": [ "js/jquery-1.7.1.min.js", "js/functions.js", "js/content.js" ],
"matches": [ "http://*.mysite.com/*" ],
"exclude_globs": [ "*board*" ],
} ],
"version": "0.1"
}
注意:这里我删除了在后台页面加载 jQuery。
background.html
<html>
<head>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/functions.js"></script>
</head>
</html>
content.js
console.log('testing console'); // this works
alert('testing native JS'); // this works
$('<div id="myTestingDiv" />').prependTo('body'); // this fails
【问题讨论】:
标签: jquery google-chrome-extension