My Chrome extension has a Content Script that adds a DIV with a button. The OnClick JS function defined for the button, however, never executes (defined in the same Content Script), so the button does nothing. Why is that?
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"]
asked Apr 28, 2015 at 23:53
gene b.
12.6k31 gold badges142 silver badges277 bronze badges
-
I not really sure that this would work but try to add the even with javascript to the button document.getElementById('textBtn').addEventListener('click', test); If this not work then is because of the developer.chrome.com/extensions/contentSecurityPolicynramirez– nramirez2015年04月29日 04:18:58 +00:00Commented Apr 29, 2015 at 4:18
-
If what I told you doesn't work then this question is a duplication of this one stackoverflow.com/questions/29670938/…nramirez– nramirez2015年04月29日 04:19:55 +00:00Commented Apr 29, 2015 at 4:19
1 Answer 1
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
answered Apr 29, 2015 at 0:25
Zig Mandel
19.9k5 gold badges32 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js