mirror of
https://github.com/stashapp/CommunityScripts.git
synced 2026-02-04 10:49:10 -06:00
33 lines
897 B
JavaScript
33 lines
897 B
JavaScript
(() => {
|
|
if (window.stashListener) return;
|
|
|
|
const { fetch: originalFetch } = window;
|
|
const stashListener = new EventTarget();
|
|
|
|
window.fetch = async (...args) => {
|
|
let [resource, config] = args;
|
|
const response = await originalFetch(resource, config);
|
|
const contentType = response.headers.get("content-type");
|
|
|
|
if (
|
|
typeof resource === "string" &&
|
|
contentType &&
|
|
(contentType.indexOf('application/json') !== -1 ||
|
|
contentType.indexOf('application/graphql-response+json') !== -1) &&
|
|
resource.endsWith("/graphql")
|
|
) {
|
|
try {
|
|
const data = await response.clone().json();
|
|
stashListener.dispatchEvent(
|
|
new CustomEvent("response", { detail: data })
|
|
);
|
|
} catch (e) {
|
|
console.error("Error parsing JSON:", e);
|
|
}
|
|
}
|
|
return response;
|
|
};
|
|
|
|
window.stashListener = stashListener;
|
|
})();
|