Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is not working because it violates the default <a href="https://developer.chrome.com/extensions/contentSecurityPolicy.html">Content Security Policy</a>. I've created a screencast of one minute to show what's wrong:</p> <p><a href="https://i.stack.imgur.com/s8Wxd.gif" title="1 minute screencast to show what&#39;s wrong"><img src="https://i.stack.imgur.com/s8Wxd.gif" alt="screencast" title="1 minute screencast to show what&#39;s wrong"></a></p> <p>First, I've shown how to debug the problem. Right-click on your popup button, and click on <a href="https://developer.chrome.com/extensions/tut_debugging.html#inspect-popup">"Inspect popup"</a>. After doing that, you will see the following error message:</p> <blockquote> <p>Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".</p> </blockquote> <p>This explains that your code is not working, because it violates the default CSP: <a href="https://developer.chrome.com/extensions/contentSecurityPolicy.html#JSExecution">Inline JavaScript will not be executed</a>. To solve the problem, you have to remove all inline JavaScript from your HTML file, and put it in a separate JS file.</p> <p>The result is shown below:</p> <h3><code>hello.html</code> (popup page)</h3> <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;p id="demo"&gt;=a&lt;/p&gt; &lt;button type="button" <b>id="do-count"</b>&gt;Count&lt;/button&gt; <b>&lt;script src="popup.js"&gt;&lt;/script&gt;</b> &lt;/body&gt; &lt;/html&gt; </code></pre> <h3><code>popup.js</code></h3> <pre class="lang-js prettyprint-override"><code>var a=0; function <b>count</b>() { a++; document.getElementById('demo').<b>textContent</b> = a; } <b>document.getElementById('do-count').onclick = count;</b></code></pre> <p>Note that I've replaced <code>innerHTML</code> with <code>textContent</code>. Learn to use <code>textContent</code> instead of <code>innerHTML</code> when you intend to change the text. In this simple example it does not matter, but in more complex applications, it might become a security issue in the form of XSS.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload