内容JavaScript不在内容脚本中执行

时间:2015-04-28 23:53:17

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

我的Chrome扩展程序有一个内容脚本,可以添加带按钮的DIV。但是,为按钮定义的OnClick JS函数从不执行(在同一内容脚本中定义),因此按钮不执行任何操作。那是为什么?

contentscript.js

var msg = "Click this button <button onclick='test()'>Test</button>";

var div = document.createElement( 'div' );
div.id = 'testDiv';
document.body.appendChild( div );
document.getElementById('testDiv').innerHTML = msg;

function test()
{
    alert('in test()');  // Never gets here
}

的manifest.json

"content_scripts": [
    {
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "css": ["contentstyle.css"],
    "js": ["jquery-1.11.2.min.js", "contentscript.js"]

1 个答案:

答案 0 :(得分:2)

Look at the official docs regarding policies. Inline javascript is strictly prohibited. The docs explain how to do it. https://developer.chrome.com/extensions/contentSecurityPolicy