Chrome扩展程序 - 在作为HTML文件访问时工作,但不作为扩展名

时间:2014-04-26 09:18:55

标签: javascript jquery html google-chrome google-chrome-extension

我正在为chrome做扩展,但这是我第一次这样做,所以我制作了一个简单的脚本,看看我是否正确地做了。

当在浏览器中作为常规页面查看时,此脚本可以正常工作,但当它作为chrome扩展加载时,它会完全崩溃。有人能告诉我必须更改哪些特殊内容才能使其作为扩展名工作吗?

HTML:

<!doctype html>
<html>
    <head>
        <script src='lib/jquery-1.11.0.min.js'></script>
        <script src='lib/jquery.knob.js'></script>
        <script src='script.js'></script>
    </head>
    <body>
        <input type='text' value='0' class='dial' id='dis'>
        <input type='text' id='in' placeholder='Enter percent to change to'>
        <button onclick="change()">Change it!</button>
    </body>
</html>

使用Javascript:

$(function() {
    $("#dis").knob({
        'readOnly':true
    });
});
function change(){
    var num = document.getElementById('in').value;
    $('#dis').val(num).trigger('change');
};

Zip containing extension and source files.

Online working version (couldn't get it working on JsFiddle or JsBin)

1 个答案:

答案 0 :(得分:1)

请仔细阅读Chrome Content Security Policy上的文档。

您的问题在于使用onclick属性。它算作内联代码。你需要摆脱它。

HTML:

<button id="button">Change it!</button>

脚本:

$(function() {
    $("#button").click(change);
    $("#dis").knob({
        'readOnly':true
    });
});