无法将JavaScript添加到Chrome扩展HTML

时间:2019-03-29 20:07:54

标签: javascript google-chrome google-chrome-extension

我想向chrome扩展弹出窗口添加一些JavaScript编码。 我正在使用以下代码:

<!DOCTYPE html>
  <html>
    <head>
      <style>
        button {
          height: 30px;
          width: 30px;
          outline: none;
        }
      </style>
      <script type="text/javascript" src="popup.js"></script> 
    </head>
    <body>
      <button id="ShowButton"></button>
    </body>
  </html>

popup.js文件与html文件位于同一文件夹中。

我收到此错误:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-n39xN31cyZtiXqcqzIa0nbASOdc/O6Jshi15mzpw5oA='), or a nonce ('nonce-...') is required to enable inline execution.

这是我的popup.js内容:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
    document.getElementById("ShowButton").value= url;
});

这是manifest.json:

    {
 "manifest_version": 2,
 "name": "eRelatorio",
 "description": "Extensão para fazer o relatorio de jogo na plataforma S**ore!",
 "version": "1.0",
 "author": "GoodReferee",
"browser_action": {
   "default_title": "Have a good day",
   "default_popup": "popup.html"
 },
 "icons": { "128": "icon.png" },
"chrome_url_overrides" : {
  "newtab": "newtab.html"
},
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
 "permissions": ["activeTab","tabs"],
 "permissions": [
    "https://www.google.com/"
  ]
}

这是错误图片:

Error Image

1 个答案:

答案 0 :(得分:0)

尝试将以下代码添加到manifest.json

使用不安全内联

 "content_security_policy": "script-src 'self' 'unsafe-inline'; object-src 'self'",