The Chrome extension popup is not working, click events are not handled

不打扰是莪最后的温柔 提交于 2019-11-25 22:33:39

问题


I have created a JavaScript variable and when I click on the button it should increment by 1, but its not happening.

Here\'s manifest.json.

{
  \"name\":\"Facebook\",
  \"version\":\"1.0\",
  \"description\":\"My Facebook Profile\",
  \"manifest_version\":2,
  \"browser_action\":{
    \"default_icon\":\"google-plus-red-128.png\",
    \"default_popup\":\"hello.html\"
  }
}

Here is the code for the html page

<!DOCTYPE html>
<html>
<head>
<script>
var a=0;
function count()
{
  a++;
  document.getElementById(\"demo\").innerHTML=a;
  return a;
}
</script>
</head>
<body>
<p id=\"demo\">=a</p>
<button type=\"button\" onclick=\"count()\">Count</button>
</body>
</html>

I want the extension to show me the value of a and increment it by one each time I click on the extension or the button

\"picture


回答1:


Your code is not working because it violates the default Content Security Policy. I've created a screencast of one minute to show what's wrong:

First, I've shown how to debug the problem. Right-click on your popup button, and click on "Inspect popup". After doing that, you will see the following error message:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

This explains that your code is not working, because it violates the default CSP: Inline JavaScript will not be executed. To solve the problem, you have to remove all inline JavaScript from your HTML file, and put it in a separate JS file.

The result is shown below:

hello.html (popup page)

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="demo">=a</p>
<button type="button" id="do-count">Count</button>
<script src="popup.js"></script>
</body>
</html>

popup.js

var a=0;
function count() {
    a++;
    document.getElementById('demo').textContent = a;
}
document.getElementById('do-count').onclick = count;

Note that I've replaced innerHTML with textContent. Learn to use textContent instead of innerHTML 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.




回答2:


Change your onclick:

onclick="count"

Or change your count function to something like this:

function count()
{

    var demo = document.getElementById("demo");
    return function() {
        demo.innerHTML = ++a;
    }

}

Here is a nice demo I put together:

Code (this assumes that you add id="the_button" to your button):

window.onload = function () {
    var button = document.getElementById("the_button");
    button.onclick = count();

    function count() {
        var a = 0;
        var demo = document.getElementById("demo");
        return function () {
            demo.innerHTML = ++a;
        }
    }
}

Demo: http://jsfiddle.net/maniator/ck5Yz/



来源:https://stackoverflow.com/questions/17601615/the-chrome-extension-popup-is-not-working-click-events-are-not-handled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!