0

我正在制作一个 chrome 扩展,我将拥有的一些功能将使用 jQuery。我在网上找不到可行的解决方案时遇到了麻烦。

核心.js:

$(document).ready(function(){
init();
});

function init()
{
    $(":button").click(function(){
        console.log("test");
        //more code here...
    });
}

扩展名.html:

<html>
<head>
<title>awesome</title>
<script src="scripts/core.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
</head>
<body>
<button id="getThem">GET!</button>
</body>
</html>

在我的清单中,我有名称、版本、manifest_version (2)、描述、browser_action->default_popup (extention.html),我不知道该放什么。你能帮我把这些基本元素拼凑起来吗?

4

1 回答 1

1

由于内容安全策略,Chrome 扩展进程中的页面无法加载外部脚本。如果您检查弹出窗口,控制台会显示此错误。

最好的解决方案是将 (jQuery) 库与您的扩展程序捆绑在一起 - 这没有额外的成本,并且使您的弹出窗口加载速度更快。

如果您真的想从外部 URL 嵌入脚本,请放宽 Content Security policy,例如。

"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"

这只能用于 https 方案。如果您想从 http 资源加载,那么您就不走运了(实际上有一种解决方法,但请不要使用它。)。

于 2013-07-04T14:54:57.040 回答