Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:"
Chrome 擴展默認 CSP 策略禁止內聯腳本(<script>code</script> 和 onclick="..."),只允許加載同源外部腳本文件。
popup.html
<!DOCTYPE html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<button id="btn">點擊</button>
</body>
</html>
popup.js
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('btn').addEventListener('click', function() {
document.getElementById('player_sidebar').style.width = '100px';
});
});
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"browser_action": {
"default_popup": "popup.html",
"default_title": "Click me"
},
"content_security_policy": "script-src 'self'; object-src 'self'"
}
.js 文件,HTML 中只引用不內聯。addEventListener,避免內聯 onclick。eval 配合 'unsafe-eval' 策略(不推薦)。