试图在 tampermonkey 中制作一个警报按钮

Trying to make a Alert button in tampermonkey

我一直在尝试让警报按钮在 tampermonkey 中工作,但我似乎没有让它工作。 这是我的脚本;

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "top:15px;left:15px;position:absolute;z-index:99999; width:50px; height 50px; background_color; ff0000";
button.onclick = "alert('Test Alert')"
document.body.appendChild(button);

您只能将函数分配给onclick。如果你赋值一个字符串,当点击发生时不执行函数:

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "top:15px;left:15px;position:absolute;z-index:99999; width:50px; height 50px; background_color; ff0000";
button.onclick = () => alert('Test Alert');
document.body.appendChild(button);