Cause
This does not work because Chrome prohibits any inline code in extensions through the content security policy.
Inline JavaScript will not execute. This restriction prohibits how embedded <script> blocks embedded event handlers and (for example, <button onclick="..."> ).
How to detect
If this is really a problem, Chrome will create the following error in the console:
An embedded script was denied because it violates the following content security policy directive: "script -src" self "chrome-extension-resource:". To enable inline execution, either the "unsafe-inline" keyword or a hash ("sha256 -...") or nonce ("nonce -...") is required.
To access the JavaScript pop-up console (which is useful for debugging in general), right-click the extension button and select "View pop-up window" in the context menu.
Further information on popup debugging is available here .
How to fix
You need to remove all embedded JavaScript. The Chrome documentation has .
Suppose the original looks like this:
<a onclick="handler()">Click this</a>
You need to remove the onclick attribute and give the element a unique identifier:
<a id="click-this">Click this</a>
And then attach the listener from the script (which should be in the .js file, suppose popup.js ):
// Pure JS: document.addEventListener('DOMContentLoaded', function() { document.getElementById("click-this").addEventListener("click", handler); }); // The handler also must go in a .js file function handler() { /* ... */ }
Note the wrapper in the DOMContentLoaded event. This ensures that the element exists at runtime. Now add the script tag, for example, in the <head> document:
<script src="popup.js"></script>
Alternative if you use jQuery:
// jQuery $(document).ready(function() { $("#click-this").click(handler); });
Policy relaxation
Q: This error mentions ways to allow inline code. I donβt want / cannot change my code, how do I enable inline scripts?
A: Although the error says, it cannot include the built-in script :
There is no mechanism to ease the restrictions on the execution of embedded JavaScript In particular, setting a script policy that includes 'unsafe-inline' will have no effect.
Update: Starting with Chrome 46, you can select a whitelist of specific built-in code blocks:
As with Chrome 46, inline scripts can be whitelisted by specifying a base64-based hash code in the policy. This hash must be the prefix of the hash algorithm used (sha256, sha384 or sha512). See Using hashes for <script> elements for an example.
However, I see no reason to use this, and it will not include built-in attributes such as onclick="code" .