GM_fetch

fetch pollyfill using GM_xmlhttpRequest

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/421384/1134973/GM_fetch.js

  1. function GM_fetch(url, opt={}){
  2. return new Promise((resolve, reject)=>{
  3. // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
  4. // https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
  5. opt.url = url
  6. opt.data = opt.body
  7. opt.responseType = "blob"
  8. opt.onload = resp=>{
  9. resolve(new Response(resp.response, {status: resp.status,
  10. // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
  11. headers: Object.fromEntries(resp.responseHeaders.trim().split(/[\r\n]+/).map(line=>{parts=line.split(': ');return [parts.shift(), parts.join(': ')]}))
  12. }))
  13. }
  14. opt.ontimeout = ()=>reject("fetch timeout")
  15. opt.onerror = error=>reject(error)
  16. opt.onabort = ()=>reject("fetch abort")
  17. GM_xmlhttpRequest(opt)
  18. })
  19. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址