以下GreaseMonkey / ViolentMonkey / Tampermonkey用户脚本在Gmail徽标旁边添加了一个CLICK
锚点。
// ==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);
从理论上讲,单击应引起警告消息;实际上,从浏览器控制台开始,警报会被以下方式阻止:
内容安全策略:页面的设置阻止以内联方式(“ script-src”)加载资源。
我已经使用Firefox 79和ViolentMonkey 2.12.7运行了用户脚本。
答案 0 :(得分:0)
按照wOxxOm的建议,添加单击事件侦听器(而不是JavaScript链接)是可行的:
// ==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);