簡體   English   中英

SetimeOut間隔失敗,顯示“無法將undefined或null轉換為object”

[英]SetimeOut interval fails with “Cannot convert undefined or null to object”

我正在使用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
    }
} ) ();

這可能是特定於TamperMonkey的問題,也可能是Chrome本身的新安全策略/錯誤 - 我遇到了同樣的事情,並在調試器中捕獲了它,並且沒有任何參數為null / undefined; setTimeout未被覆蓋。

編輯:有問題的用戶腳本與我正在調試的用戶腳本之間的共享特征是setTimeout的“遞歸”使用。 我改為將其改為setInterval ,這似乎已經修復了我的情況。 在此輸入圖像描述

這是Chrome中已確認的錯誤:

在TM github上報道

在bugs.chromium.org上報道

另一個看起來有用的解決方案是將函數.bindwindow ,例如:

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是使用window.setTimeout而不是setTimeout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM