我正在使用tampermonkey进入用户脚本,无法解决此错误,任何帮助将不胜感激。
我检测到键很好,空格键触发此功能,只要键保持在向下位置,它就会重复。控制台或多或少地正常写入输出30秒,然后出现TypeError。
根据声誉限制,这是一个截图:
用户脚本:
// ==UserScript==
// @name TEST STUFF--------------------
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @run-at document-start
// @include http://*
// @include https://*
// @grant none
// ==/UserScript==
( function()
{
'use strict';
window.addEventListener ( "keydown", CaptureKeyPress );
window.addEventListener ( "keyup", CaptureKeyPress );
var Hotkeys =
{
perform: 32
};
var HotkeyToggle = false;
function CaptureKeyPress ( a )
{
if ( a.keyCode == Hotkeys.perform )
{
a.preventDefault();
a.stopPropagation();
a.cancelBubble = true;
a.stopImmediatePropagation();
if ( a.type == "keydown" && !HotkeyToggle )
{
console.clear();
HotkeyToggle = true;
perform();
}
if ( a.type == "keyup" && HotkeyToggle )
{
HotkeyToggle = false;
}
}
}
function perform()
{
if(HotkeyToggle == false) // exit
{
return 0
}
//do stuff...
console.info("working...");
if(HotkeyToggle == true) // continue after everything completes
{
setTimeout(() => {
perform()
}, 280);
return 0
}
return 1
}
} ) ();
这是Chrome中已确认的错误:
另一个看起来有效的解决方案是.bind
window
的功能,例如:
window.clearTimeout = window.clearTimeout.bind(window);
window.clearInterval = window.clearInterval.bind(window);
window.setTimeout = window.setTimeout.bind(window);
window.setInterval = window.setInterval.bind(window);
该错误应该在Chrome 75中修复。
我使用Tampermonkey和谷歌浏览器时遇到了同样的问题。对我有用的是使用window.setTimeout
而不是setTimeout
。