Javascript Greasemonkey用户脚本被内容安全策略阻止

Javascript Greasemonkey用户脚本被内容安全策略阻止,javascript,greasemonkey,tampermonkey,Javascript,Greasemonkey,Tampermonkey,下面的GreaseMonkey/ViolentMonkey/Tampermonkey用户脚本在Gmail徽标旁边添加了一个点击锚 // ==UserScript== // @name CSP test // @namespace Violentmonkey Scripts // @match *://mail.google.com/* // @grant none // ==/UserScript== function addAnchor(){

下面的GreaseMonkey/ViolentMonkey/Tampermonkey用户脚本在Gmail徽标旁边添加了一个
点击

// ==UserScript==
// @name        CSP test 
// @namespace   Violentmonkey Scripts
// @match        *://mail.google.com/*
// @grant       none
// ==/UserScript==
 
function addAnchor(){
  let myanchor =  document.createElement("A");
  myanchor.appendChild(document.createTextNode("CLICK"));
  myanchor.setAttribute("href", "javascript:sampleFunc(this)"); 
  document.querySelectorAll('div[class="gb_xc gb_Ce"]')[1].appendChild(myanchor);
}
 
function sampleFunc(elt){ alert("Just an alert"); }
 
setTimeout(addAnchor, 4000);
从理论上讲,点击应该会发出警告信息;实际上,与从浏览器控制台发出的警报一样,警报会被以下命令阻止:

内容安全策略:页面的设置阻止在内联(“脚本src”)加载资源


我已经用Firefox 79和ViolentMonkey 2.12.7运行了userscript。

正如wOxxOm所建议的,添加一个点击事件监听器(而不是JavaScript链接)可以:


在Violentmonkey高级设置中将
默认注入模式设置为
自动
。另外,考虑使用AdvestTeListNER添加一个JS侦听器。@ WOXXOM:谢谢,AdvestListNead帮助。代码>/@inject-into-auto
没有。出于安全原因,它仍然值得使用吗?
// ==UserScript==
// @name        CSP test 
// @namespace   Violentmonkey Scripts
// @match        *://mail.google.com/*
// @grant       none
// ==/UserScript==
 
function addAnchor(){
  let myanchor =  document.createElement("A");
  myanchor.appendChild(document.createTextNode("CLICK"));
  myanchor.setAttribute("href", "#"); 
  myanchor.addEventListener("click", sampleFunc);
  document.querySelectorAll('div[class="gb_xc gb_Ce"]')[1].appendChild(myanchor);
}
 
function sampleFunc(elt){ alert("Just an alert"); }
 
setTimeout(addAnchor, 4000);