JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 内容安全 – 谷歌Chrome扩展的策略错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个Chrome扩展,它将打开新标签页中的所有链接.

以下是我的代码文件:

的manifest.json

{
  "name": "A browser action which changes its icon when clicked.","version": "1.1","permissions": [
    "tabs","<all_urls>"
  ],"browser_action": {     
    "default_title": "links",// optional; shown in tooltip
    "default_popup": "popup.html"        // optional
  },"content_scripts": [
    {
    "matches": [ "<all_urls>" ],"js": ["BACkground.js"]
    }
  ],"manifest_version": 2
}

popup.html

<!doctype html>
<html>
  <head>
    <title>My Awesome Popup!</title>
    <script>
function getPageandSELEctedTexTindex() 
  { 
    chrome.tabs.getSELEcted(null,function(tab) { 
    chrome.tabs.sendrequest(tab.id,{greeTing: "Hello"},function (responsE) 
    { 
        console.log(response.farewell); 
    }); 
   }); 
        } 
chrome.browserAction.onClicked.addListener(function(tab) { 
        getPageandSELEctedTexTindex(); 
});
         </script>
  </head>
  <body>
    <button onclick="getPageandSELEctedTexTindex()">
      </button>
  </body>
</html>

BACkground.js

chrome.extension.onrequest.addListener(
  function(request,sender,sendResponsE) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeTing == "Hello")
    updateIcon();  

});
function updateIcon() {
  var allLinks = document.links;
  for (var i=0; i<allLinks.length; i++) {
    alllinks[i].style.BACkgroundColor='#ffff00';

}
}

最初我想突出显示页面上的所有链接或以某种方式标记它们;但是我收到错误“拒绝执行内联脚本,因为Content-Security-Policy”.

当我按弹出窗口内的按钮时,我得到这个错误:拒绝执行内联事件处理程序,因为Content-Security-Policy.

请帮我修复这些错误,@R_486_9447@使用我的chrome扩展名打开新标签页中的所有链接.

解决方法

“manifest_version”的后果之一是2,默认情况下启用 Content Security Policy. Chrome开发人员选择严格执行此操作,并始终禁止使用内置JavaScript代码 – 只允许在外部JavaScript文件中执行代码(以防止扩展中的 Cross-Site Scripting vulnerabilities).因此,不要在popup.html中定义getPageandSELEctedTexTindex()函数,您应该将其放入popup.js文件并将其包含在popup.html中:
<script type="text/javascript" src="popup.js"></script>

< button onclick =“getPageandSELEctedTexTindex()”>必须改变,onclick属性也是一个内联脚本.您应该分配ID属性:< button id =“button”&gt ;.然后在popup.js中,您可以将该事件处理程序附加到该按钮:

window.addEventListener("load",function()
{
  document.getElementById("button")
          .addEventListener("click",getPageandSELEctedTexTindex,falsE);
},falsE);

大佬总结

以上是大佬教程为你收集整理的javascript – 内容安全 – 谷歌Chrome扩展的策略错误全部内容,希望文章能够帮你解决javascript – 内容安全 – 谷歌Chrome扩展的策略错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。