Skip to content

Commit

Permalink
feat: ar:// protocol doc links
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Sep 27, 2023
1 parent 43767d2 commit e603a2f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module "@arconnect/webext-bridge" {
event: Event;
auth_listening: number;
auth_chunk: Chunk;
ar_protocol: ProtocolWithReturn<{ url: string }, { url: sting }>;
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/contents/ar_protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { sendMessage } from "@arconnect/webext-bridge";
import type { PlasmoCSConfig } from "plasmo";
import { isString } from "typed-assert";

export const config: PlasmoCSConfig = {
matches: ["file://*/*", "http://*/*", "https://*/*"],
run_at: "document_start",
all_frames: true
};

document.addEventListener("DOMContentLoaded", async () => {
// all elements with the "ar://" protocol
const elements = document.querySelectorAll(
'a[href^="ar://"], img[src^="ar://"], iframe[src^="ar://"], ' +
'audio > source[src^="ar://"], video > source[src^="ar://"], ' +
'link[href^="ar://"], embed[src^="ar://"], object[data^="ar://"],' +
'script[src^="ar://"]'
);
const fields = {
src: ["img", "iframe", "source", "embed", "script"],
href: ["a", "link"],
data: ["object"]
};

for (const el of elements) {
// ask the background script to return the correct ar:// url
try {
const res = await sendMessage(
"ar_protocol",
{ url: el[fields[el.tagName]] },
"background"
);

// check result
isString(res?.url);

el[fields[el.tagName]] = res.url;

// reload parent
if (el.tagName === "SOURCE") {
// @ts-expect-error
el.parentNode.load();
}
} catch {
console.error(`Failed to load ar:// resource: ${el[fields[el.tagName]]}`);
}
}
});
2 changes: 2 additions & 0 deletions src/gateways/wayfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export function useGateway(requirements: Requirements) {
})();
}, [requirements]);

// TODO: health check

return activeGateway;
}

Expand Down

0 comments on commit e603a2f

Please sign in to comment.