Skip to content

onerror & ontimeout don't work on GM_xmlhttpRequest, and cause the program to hang. #555

Description

@F9y4ng

As shown in the following code, when the timeout is set, the ontimeout method will not be executed even if the timeout occurs, and will cause the program to hang and stop running subsequent code. (The same situation also happens with the onerror method):

// ==UserScript==
// @name        NewScript-kybl1qtk
// @description This is your new file, start writing code
// @match       *://*/*
// @grant       GM_xmlhttpRequest
// ==/UserScript==

(async function(){
    try{
        const rst = await test("https://abs.twimg.com/favicons/twitter.3.ico");
        console.log(rst)
        if (rst) {
          const img = new Image()
          img.src = rst;
          document.documentElement.prepend(img)
        }
    } catch(e) {
        console.error(e.message)
    }
    
    function test(url) {
         return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
              url: url,
              headers: { Accept: "*/*", Referer: url },
              method: "GET",
              timeout: 10,
              responseType: "blob",
              onreadystatechange: response => {
                if (response.readyState !== 4) return;
                if (response.status === 200) {
                  let blob = response.response;
                  let oFileReader = new FileReader();
                  oFileReader.onloadend = e => resolve(e.target.result);
                  oFileReader.readAsDataURL(blob);
                } else if (response.status !== 0) {
                  reject(new Error("NoAccessError"));
                }
              },
              onerror: () => reject(new Error("NetworkError")),
              ontimeout: () => reject(new Error("TimeoutError")),
            });
          });
    }
})()

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

Status
Release

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions