内容 JavaScript 未在内容脚本中执行
Content JavaScript Not Executing in Content Script
我的 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"]
查看有关政策的官方文档。严禁内联 javascript。文档解释了如何操作。 https://developer.chrome.com/extensions/contentSecurityPolicy
我的 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"]
查看有关政策的官方文档。严禁内联 javascript。文档解释了如何操作。 https://developer.chrome.com/extensions/contentSecurityPolicy