Skip to content

重构 GM_xmlhttpRequest 的实作 #862

@cyfung1031

Description

@cyfung1031
    if (param1.responseType === "stream" || param1.fetch || param1.redirect) {
      // 只有fetch支持ReadableStream、redirect这些,直接使用fetch
      return this.CAT_fetch(param1, sender, resultParam);
    }

不正确。 GM_xmlhttpRequest 用 fetch API 模拟 xmlhttpRequest 的条件是, fetch:true, redirect, anonymous
另外 GM_xmlhttpRequest 呼叫 CAT_xxxx 这个写法也不合适

xmlhttpRequest版 和 fetch版 的实作部份应该放在 pkg 工具库
而不是跟 GM_API 代码混在一起
( dealXhrResponse 和 dealFetch 等相关代码)
实际应用时,xmlhttpRequest版 的 xmlhttprequest 由Offscreen 提供

  1. stream, buffer, redirect, responseType 等部份要重写

  2. 严格使用 xmlhttpRequest ,只有在 fetch: true 才使用 fetch版。重申 fetch 只是一个较好的做法,但 GM_xmlhttpRequest 本身所有options都可以用 最原始的 xmlhttpRequest 实现 (不包括TM增加的 redirectanonymous


由于MV3倾向fetch API
因此,实作部份,应把logic倒过来, fetch 预设true, 除非 onProgress 那些xhr限定的东西指定了


補充

stream 要 fetch. 因為xhr response 要等 readyState4

chrome 只能在同一頁面的service worker 傳 blob/arraybuffer, chrome mv2/mv3 的 messaging api 是 JSON serialization 而不是 firefox的 structured clone algorithm.

日後chrome 或會改成 structured clone algorithm

@types/chrome 說明有錯

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions