nostr-links/content.js

31 lines
1.1 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function() {
const linkRels = ['alternate', 'me', 'author'];
const selectorUntyped = linkRels.map(rel => `link[rel="${rel}"][href^="nostr:"]`).join(',');
const selectorTyped = linkRels.map(rel => `link[rel="${rel}"][type="application/nostr+json"]`).join(',');
const untypedLinks = document.querySelectorAll(selectorUntyped);
const typedLinks = document.querySelectorAll(selectorTyped);
const links = [...untypedLinks, ...typedLinks]
const linkMap = new Map();
if (links.length > 0) {
links.forEach(link => {
const uri = link.href.replace(/^(nostr|web+nostr):/, "").trim();
let text = link.title?.trim() || '';
if (link.rel === "me") text = `Me: ${text}`;
if (link.rel === "author") text = `Author: ${text}`;
linkMap.set(uri, {
uri: uri,
text: text,
rel: link.rel
// icon: "icon.png"
});
});
browser.runtime.sendMessage({
action: "showNostrLinksPageAction",
links: Array.from(linkMap.values())
});
}
});