Skip to content

Commit

Permalink
Fix Quick Reblog double reblogs after extension update/restart (alter…
Browse files Browse the repository at this point in the history
…native 4) (AprilSylph#1601)
  • Loading branch information
marcustyphoon authored Sep 2, 2024
1 parent 18298b6 commit 822326b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/content_scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
};

const initMainWorld = () => new Promise(resolve => {
document.documentElement.addEventListener('xkitinjectionready', resolve, { once: true });
document.documentElement.addEventListener('xkit-injection-ready', resolve, { once: true });

const { nonce } = [...document.scripts].find(script => script.getAttributeNames().includes('nonce'));
const script = document.createElement('script');
Expand Down
15 changes: 10 additions & 5 deletions src/main_world/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
{
const moduleCache = {};

document.documentElement.addEventListener('xkitinjectionrequest', async event => {
window.removeXKitListener?.();

const controller = new AbortController();
window.removeXKitListener = () => controller.abort();

document.documentElement.addEventListener('xkit-injection-request', async event => {
const { detail, target } = event;
const { id, path, args } = JSON.parse(detail);

Expand All @@ -15,11 +20,11 @@

const result = await func.apply(target, args);
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', { detail: JSON.stringify({ id, result }) })
new CustomEvent('xkit-injection-response', { detail: JSON.stringify({ id, result }) })
);
} catch (exception) {
target.dispatchEvent(
new CustomEvent('xkitinjectionresponse', {
new CustomEvent('xkit-injection-response', {
detail: JSON.stringify({
id,
exception: {
Expand All @@ -32,7 +37,7 @@
})
);
}
});
}, { signal: controller.signal });

document.documentElement.dispatchEvent(new CustomEvent('xkitinjectionready'));
document.documentElement.dispatchEvent(new CustomEvent('xkit-injection-ready'));
}
6 changes: 3 additions & 3 deletions src/utils/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export const inject = (path, args = [], target = document.documentElement) =>
const { id, result, exception } = JSON.parse(detail);
if (id !== requestId) return;

target.removeEventListener('xkitinjectionresponse', responseHandler);
target.removeEventListener('xkit-injection-response', responseHandler);
exception ? reject(exception) : resolve(result);
};
target.addEventListener('xkitinjectionresponse', responseHandler);
target.addEventListener('xkit-injection-response', responseHandler);

target.dispatchEvent(
new CustomEvent('xkitinjectionrequest', { detail: JSON.stringify(data), bubbles: true })
new CustomEvent('xkit-injection-request', { detail: JSON.stringify(data), bubbles: true })
);
});

0 comments on commit 822326b

Please sign in to comment.