0

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
2
  • 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/contentSecurityPolicy Commented 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/… Commented Apr 29, 2015 at 4:19

1 Answer 1

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

answered Apr 29, 2015 at 0:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.