猿问

Chrome 扩展程序政策错误:拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令

我正在开发一个chrome扩展来完成一个简单的任务,除了我花了大部分时间在一行html代码上,以弄清楚如何正确地赋予它正确的权限,我试图这样做,但我不知道实际放什么。


我在尝试运行 java 脚本时遇到的错误:


拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“script-src 'self' https://apis.google.com”。启用内联执行需要“不安全内联”关键字、哈希(“sha256-...”)或随机数(“nonce-...”)。


这就是我试图修复策略错误的原因:


<meta http-equiv = "Content-Security-Policy" content="script-src 'self' data: https://apis.google.com 'unsafe-inline' 'sha256-base64EncodedHash'">

我尝试将内容安全策略放在清单文件中,并尝试在上面的代码中加入其他内容。


我所有的代码(我知道我不应该这样做,但我不知道是什么导致了这个问题):


网页:


<html>

<head>

    <title>Tracillion</title>

    <link rel = "stylesheet" type = "text/css" href = "style.css">

    <meta http-equiv = "Content-Security-Policy" content="script-src 'self' data: https://apis.google.com 'unsafe-inline' 'sha256-base64EncodedHash'">

    <script type = "text/javascript" src = "popup.js"></script>

</head>

<body>

    <h1 class = "header">Enter what you would like to search!</h1>

    <div class = "line"></div>

    <input class = "input" id = "input">

    <button type = "submit" class = "button" onclick="loadSearch()">Search</button>

</body>

脚本:


function loadSearch() {

var inputResult = document.getElementById("input").value;

var final = 'https://www.google.com/search?q=' + encodeURI(inputResult + ' site:v3rmillion.net');

chrome.tabs.create({url: final});}

清单:


{

  "name": "bruh",

  "description": "bruh moment",

  "version": "1.0",

  "background": {

    "scripts": [

      "popup.js"

    ]

  },

  "manifest_version": 2,


  "browser_action": {

    "default_icon": "icon.png",

    "default_popup": "popup.html"

  },


  "permissions": [

    "tabs"

  ]

}


慕田峪9158850
浏览 283回答 1
1回答

明月笑刀无情

我想通了!这是因为我有一个内联执行...而不是手动在html中执行onclick=“我的函数()”,我只是通过javascript听它!这是我更新的代码:脚本:function loadSearch() {&nbsp;var inputResult = document.getElementById("input").value;&nbsp;var final = 'https://www.google.com/search?q=' + encodeURI(inputResult + '&nbsp;&nbsp;site:v3rmillion.net');&nbsp;chrome.tabs.create({url: final});}document.addEventListener('DOMContentLoaded', function() {&nbsp;document.querySelector('button').addEventListener('click', loadSearch, false);}, false)网页:<html><head>&nbsp; &nbsp; <title>Tracillion</title>&nbsp; &nbsp; <link rel = "stylesheet" type = "text/css" href = "style.css">&nbsp; &nbsp; <script type = "text/javascript" src = "popup.js"></script></head><body>&nbsp; &nbsp; <h1 class = "header">Enter what you would like to search!</h1>&nbsp; &nbsp; <div class = "line"></div>&nbsp; &nbsp; <input class = "input" id = "input">&nbsp; &nbsp; <button type = "submit" class = "button">Search</button></body>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答