Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.27k stars 422 forks source link

[BUG] GM_download.abort may take too much time #2088

Open CrayonSSR opened 4 months ago

CrayonSSR commented 4 months ago

Expected Behavior

After executing the abort method, GM_download will attempt to terminate the request as promptly as possible.

Actual Behavior

GM_download will wait for the server's response head before cancelling the request, then triggers the onerror event.

Specifications

Script

app = Flask(name)

@app.route('/assets//') def get_image(wait_time, name):

sleep(wait_time) # seconds

return send_from_directory('assets', name, mimetype='image/png')

if name == 'main': app.run(host='0.0.0.0',port=7777,debug=True)


* Test abort cost
```js
// ==UserScript==
// @name                GM_download abort test
// @match               https://www.tampermonkey.net/*
// @connect             127.0.0.1
// @connect             *
// @grant               GM_download
// @run-at              document-start
// @version             1.0
// ==/UserScript==

// tampermonkey download mode: browser

const start = Date.now()

const downloader = GM_download({
    url: 'http://localhost:7777/assets/15/test.png',
    name: 'test/test.jpg',
    timeout: 5000,
    onerror(event) {
        console.log(`Abort cost ${( Date.now() - start - 300) / 1000}s`, event)
    },
    ontimeout(event) {
        console.log(`ontimeout cost ${( Date.now() - start) / 1000}s`, event)
    },
})

setTimeout(() => downloader.abort(), 300)

Test log

// Note: URL rule: '/assets/<wait_time(s)>/<file_name>'
// Note: Without abort, GM_download could successfuly save the file after <wait_time>
// Note: The timeout event never trigger.

// url: 'http://localhost:7777/assets/10/test.png'
// log: Abort cost 10.286s { details: {current: 'USER_CANCELED'} }

// url: 'http://localhost:7777/assets/15/test.png'
// log: Abort cost 15.082s { details: {current: 'USER_CANCELED'} }

// url: 'http://localhost:7777/assets/33/test.png'
// log: Abort cost 33.996s { details: {current: 'USER_CANCELED'} }
derjanb commented 4 months ago

The "Download Mode" setting is untouched ("Default")? Is this correct?

CrayonSSR commented 4 months ago

The "Download Mode" setting is untouched ("Default")? Is this correct?

My setting - Download Mode: Browser api

image

image

JsGarcia5 commented 1 month ago

sorry to bother you. You're KC Enhancer's developer, right? I see your name in the script. Now that afdian is dead, where will you post updates?

CrayonSSR commented 1 month ago

sorry to bother you. You're KC Enhancer's developer, right? I see your name in the script. Now that afdian is dead, where will you post updates?

Afdian is dead ???

Afdian has two domains, afdian.com and afdian.net.

Are you saying that afdian.net has been abandoned.