首页 > 解决方案 > 当我尝试单击功能时,Chrome 扩展程序出错

问题描述

我有一个 chrome 扩展,它执行以下操作,尝试添加一个指向 google chrome 存储的链接,以便我可以获取该链接,并将其添加到我的 html 中。当我进行一些调试时,我注意到当我单击按钮添加链接时,它的 href 为javascript:getStorage.

完整的 HTML

<!DOCTYPE html>
<html>

<head>
    <title>Open URL</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body style="width: 128px; height: 128px;">
    <h2><b>Open URL</b></h2>
    <h3><a href="javascript:setStorage()" target="_blank">Add Link<br></a></h3>
    <h3><a href="https://www.ph4.org/r.php" target="_blank">Random<br></a></h3>
</body>
</html>

错误

Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

显现

{
  "manifest_version": 2,

  "web_accessible_resources": [
    "popup.html", "popup.js"
  ],

  "name": "UrlManager",
  "description": "Quickly open urls.",
  "version": "1.0.1",
  
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["popup.js"]
    }
  ],

  "permissions": [
    "tabs", 
    "storage", 
    "unlimitedStorage"
  ],

  "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html",
      "default_title": "Open a URL"
  }
}

JS

var storage = chrome.storage.local;

function setStorage() {
    var value = prompt("Whats your link")
    
    storage.set({key: value}, function() {
        console.log('Value is set to ' + value);
    });
}

 
function getStorage() {
    storage.get(['key'], function(result) {
        console.log('Value currently is ' + result.key);
    });
}

有没有什么办法解决这一问题?

标签: javascripthtmljsongoogle-chrome-extension

解决方案


推荐阅读