refactor: one shared extension-API module, and drive the dApp flows on Firefox (closes #153)
This commit was merged in pull request #281.
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
// AutistMask content script — bridges between inpage (window.ethereum)
|
||||
// and the background service worker via extension messaging.
|
||||
|
||||
const {
|
||||
hasBrowserNamespace,
|
||||
runtimeApi,
|
||||
sendMessage,
|
||||
storageGet,
|
||||
storageSet,
|
||||
} = require("../shared/browserApi");
|
||||
|
||||
// In Chrome (MV3), inpage.js runs as a MAIN-world content script declared
|
||||
// in the manifest, so no injection is needed here. In Firefox (MV2), the
|
||||
// "world" key is not supported, so we inject via a <script> tag.
|
||||
if (typeof browser !== "undefined") {
|
||||
if (hasBrowserNamespace()) {
|
||||
const script = document.createElement("script");
|
||||
script.src = browser.runtime.getURL("src/content/inpage.js");
|
||||
script.src = runtimeApi().getURL("src/content/inpage.js");
|
||||
script.onload = function () {
|
||||
this.remove();
|
||||
};
|
||||
@@ -14,23 +22,27 @@ if (typeof browser !== "undefined") {
|
||||
}
|
||||
|
||||
// Send the persisted EIP-6963 provider UUID to the inpage script.
|
||||
// Generated once at install time and stored in chrome.storage.local.
|
||||
(function sendProviderUuid() {
|
||||
const storage =
|
||||
typeof browser !== "undefined"
|
||||
? browser.storage.local
|
||||
: chrome.storage.local;
|
||||
storage.get("eip6963Uuid", (items) => {
|
||||
let uuid = items?.eip6963Uuid;
|
||||
// Generated once at install time and stored in extension storage.
|
||||
(async function sendProviderUuid() {
|
||||
let uuid = null;
|
||||
try {
|
||||
const items = await storageGet("eip6963Uuid");
|
||||
uuid = items?.eip6963Uuid;
|
||||
if (!uuid) {
|
||||
uuid = crypto.randomUUID();
|
||||
storage.set({ eip6963Uuid: uuid });
|
||||
await storageSet({ eip6963Uuid: uuid });
|
||||
}
|
||||
window.postMessage(
|
||||
{ type: "AUTISTMASK_PROVIDER_UUID", uuid },
|
||||
location.origin,
|
||||
);
|
||||
});
|
||||
} catch {
|
||||
// Storage was unavailable or refused the write. The announcement
|
||||
// still has to go out — a provider that never announces is invisible
|
||||
// to every EIP-6963 dApp — so it goes under a fresh uuid that this
|
||||
// page load will not outlive.
|
||||
if (!uuid) uuid = crypto.randomUUID();
|
||||
}
|
||||
window.postMessage(
|
||||
{ type: "AUTISTMASK_PROVIDER_UUID", uuid },
|
||||
location.origin,
|
||||
);
|
||||
})();
|
||||
|
||||
// Relay requests from the page to the background script
|
||||
@@ -39,27 +51,31 @@ window.addEventListener("message", (event) => {
|
||||
if (event.data?.type !== "AUTISTMASK_REQUEST") return;
|
||||
const { id, method, params } = event.data;
|
||||
|
||||
const runtime =
|
||||
typeof browser !== "undefined" ? browser.runtime : chrome.runtime;
|
||||
|
||||
runtime.sendMessage(
|
||||
{ type: "AUTISTMASK_RPC", id, method, params, origin: location.origin },
|
||||
(response) => {
|
||||
sendMessage({
|
||||
type: "AUTISTMASK_RPC",
|
||||
id,
|
||||
method,
|
||||
params,
|
||||
origin: location.origin,
|
||||
})
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
window.postMessage(
|
||||
{ type: "AUTISTMASK_RESPONSE", id, ...response },
|
||||
"*",
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
})
|
||||
.catch(() => {
|
||||
// No receiver: the background context is gone. The page's promise
|
||||
// stays pending, which is what it did before this was a promise
|
||||
// at all; turning it into a rejection here is a change to what
|
||||
// dApps see and belongs to its own issue.
|
||||
});
|
||||
});
|
||||
|
||||
// Listen for events pushed from the background (e.g. accountsChanged)
|
||||
const runtime =
|
||||
typeof browser !== "undefined" ? browser.runtime : chrome.runtime;
|
||||
|
||||
runtime.onMessage.addListener((msg) => {
|
||||
runtimeApi().onMessage.addListener((msg) => {
|
||||
if (msg.type === "AUTISTMASK_EVENT") {
|
||||
window.postMessage(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user