Skip to content

Commit

Permalink
Avoid promise chain in GM_xmlhttpRequest
Browse files Browse the repository at this point in the history
Currently, the design principle of ChromeXt is
1. await GM_xmlhttpRequest return the reponse data;
2. await GM.xmlHttpRequest return the xhr object;
3. onload / onerror take the xhr object as the only parameters.

We must deconstruct the xhr (proxy) to avoid promise chaining so that
the above design is valid.
  • Loading branch information
JingMatrix committed May 1, 2024
1 parent 974ab6e commit 9de7f62
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/src/main/assets/GM.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ function GM_xmlhttpRequest(details) {
}

if (!Array.isArray(request.cookie)) delete request.cookie;
if (request.method == "HEAD") request.redirect = "manual";
ChromeXt.dispatch("xmlhttpRequest", {
id: GM_info.script.id,
request,
Expand Down Expand Up @@ -875,7 +876,7 @@ class ResponseSink {
const event = new ProgressEvent(type, this.xhr);
this.xhr.dispatchEvent(event);
if (typeof this.xhr["on" + type] == "function") {
this.xhr["on" + type](data || this.xhr);
this.xhr["on" + type](data || { ...this.xhr });
}
}
static async prepare(type, data) {
Expand Down Expand Up @@ -1213,6 +1214,12 @@ GM.bootstrap = () => {
if (typeof sync == "function") {
GM[name] = function () {
const result = sync.apply(null, arguments);
if (name == "xmlHttpRequest") {
return new Promise(async (resolve) => {
await result;
resolve({ ...result });
});
}
if (result instanceof Promise) return result;
return new Promise(async (resolve) => {
resolve(await result);
Expand Down Expand Up @@ -1286,7 +1293,7 @@ GM.bootstrap = () => {
}

GM_info.scriptHandler = "ChromeXt";
GM_info.version = "3.8.0";
GM_info.version = "3.8.1";
Object.freeze(GM_info);
ChromeXt.scripts.push(GM_info);
}
Expand Down

0 comments on commit 9de7f62

Please sign in to comment.