Home > Article > Web Front-end > Why Aren\'t My Chrome Extension Popup\'s Click Events Firing?
Chrome Extension Popup Click Events Not Firing: Resolving Content Security Policy Violations
Your Chrome extension popup is not responding to button clicks, hindering the intended behavior. The culprit lies in the default Content Security Policy (CSP) enforced by the browser.
Understanding the Problem
To debug the issue, right-click on the popup button and select "Inspect popup." You will encounter an error message stating that an inline script violates the "script-src" CSP directive. This explains why the click event is not triggering.
Solution: Separating JavaScript Code
To rectify the issue, move all inline JavaScript code from the HTML file into a separate JS file. This follows the CSP requirement by isolating scripts from the HTML document.
Revised Code:
hello.html (Popup Page)
<!DOCTYPE html> <html> <head> </head> <body> <p>
popup.js
var a = 0; function count() { a++; document.getElementById('demo').textContent = a; } document.getElementById('do-count').onclick = count;
Note: Replace innerHTML with textContent in cases where you intend to modify text. While this is not critical in this example, it is recommended to use textContent for enhanced security (XSS mitigation) in complex applications.
The above is the detailed content of Why Aren\'t My Chrome Extension Popup\'s Click Events Firing?. For more information, please follow other related articles on the PHP Chinese website!