···11(async function () {
22+ console.log("Popup script started"); // Log: Script start
23 const params = new URLSearchParams(window.location.search);
34 let raw = params.get("payload");
55+ console.log("Initial raw payload:", raw); // Log: Raw payload
46 if (!raw) {
57 const [tab] = await chrome.tabs.query({
68 active: true,
79 currentWindow: true,
810 });
911 raw = tab.url;
1212+ console.log("Raw payload from active tab:", raw); // Log: Tab URL payload
1013 }
1414+1515+ const list = document.getElementById("dest");
1616+ list.innerHTML = "<li>Processing...</li>";
1717+1818+ // Ensure WormholeTransform and its methods are available
1919+ if (
2020+ !window.WormholeTransform ||
2121+ typeof window.WormholeTransform.parseInput !== "function"
2222+ ) {
2323+ list.innerHTML = "<li>Error: Transform script not loaded correctly.</li>";
2424+ console.error("Popup: WormholeTransform.parseInput is not available.");
2525+ return;
2626+ }
2727+1128 const info = await window.WormholeTransform.parseInput(raw);
1212- const list = document.getElementById("dest");
1313- if (!info?.atUri) {
1414- list.innerHTML = "<li>No at:// reference found.</li>";
2929+ console.log("Parsed info:", JSON.stringify(info, null, 2)); // Log: Parsed info object
3030+3131+ if (!info || (!info.did && !info.atUri)) {
3232+ list.innerHTML = "<li>No DID or at:// reference found in the input.</li>";
3333+ console.log("No DID or atUri found in info, exiting."); // Log: Exit condition
1534 return;
1635 }
1717- const dests = window.WormholeTransform.buildDestinations(info);
1818- list.innerHTML = dests
1919- .map(
2020- (d) =>
2121- `<li>
2222- <a href="${d.url}"
2323- target="_blank"
2424- rel="noopener noreferrer"
2525- style="display:block; padding:6px 8px; border:1px solid #ccc; border-radius:6px; background:#fafafa; text-decoration:none; color:inherit; font-size:14px;">
2626- ${d.label}
2727- </a>
2828- </li>`
2929- )
3030- .join("");
3636+3737+ const DID_HANDLE_CACHE_KEY = "didHandleCache"; // Caching re-enabled
3838+3939+ const renderDestinations = (destinations) => {
4040+ console.log(
4141+ "Rendering destinations:",
4242+ JSON.stringify(destinations, null, 2)
4343+ ); // Log: Destinations to render
4444+ if (destinations && destinations.length > 0) {
4545+ list.innerHTML = destinations
4646+ .map(
4747+ (d) =>
4848+ `<li>
4949+ <a href="${d.url}"
5050+ target="_blank"
5151+ rel="noopener noreferrer"
5252+ style="display:block; padding:6px 8px; border:1px solid #ccc; border-radius:6px; background:#fafafa; text-decoration:none; color:inherit; font-size:14px;">
5353+ ${d.label}
5454+ </a>
5555+ </li>`
5656+ )
5757+ .join("");
5858+ } else {
5959+ list.innerHTML = "<li>No actions available at this time.</li>";
6060+ console.log("No destinations to render."); // Log: No destinations
6161+ }
6262+ };
6363+6464+ // Initial render based on whatever info.handle might exist (e.g. from direct handle input)
6565+ let currentDests = window.WormholeTransform.buildDestinations(info);
6666+ renderDestinations(currentDests);
6767+6868+ // If a DID is present but the handle is not, try to resolve or get from cache.
6969+ if (info.did && !info.handle) {
7070+ console.log(
7171+ `Popup: DID ${info.did} present, handle missing. Checking cache...`
7272+ );
7373+7474+ const cacheData = await chrome.storage.local.get(DID_HANDLE_CACHE_KEY);
7575+ const cachedHandles = cacheData[DID_HANDLE_CACHE_KEY] || {};
7676+7777+ if (cachedHandles[info.did]) {
7878+ console.log(
7979+ `Popup: Found handle '${cachedHandles[info.did]}' for DID ${
8080+ info.did
8181+ } in cache.`
8282+ );
8383+ info.handle = cachedHandles[info.did];
8484+ currentDests = window.WormholeTransform.buildDestinations(info);
8585+ renderDestinations(currentDests);
8686+ } else {
8787+ console.log(
8888+ `Popup: Handle for DID ${info.did} not in cache. Attempting to resolve...`
8989+ );
9090+ if (!currentDests || currentDests.length === 0) {
9191+ list.innerHTML =
9292+ "<li>Resolving identifier to check for more actions...</li>";
9393+ }
9494+ try {
9595+ if (typeof window.WormholeTransform.resolveDidToHandle !== "function") {
9696+ list.innerHTML = "<li>Error: Resolve function not loaded.</li>";
9797+ console.error(
9898+ "Popup: WormholeTransform.resolveDidToHandle is not available."
9999+ );
100100+ return;
101101+ }
102102+ const resolvedHandle =
103103+ await window.WormholeTransform.resolveDidToHandle(info.did);
104104+ console.log(
105105+ `Popup: Resolved handle: ${resolvedHandle} for DID: ${info.did}`
106106+ );
107107+ if (resolvedHandle) {
108108+ info.handle = resolvedHandle;
109109+ currentDests = window.WormholeTransform.buildDestinations(info);
110110+ console.log("Popup: Re-rendering destinations with resolved handle.");
111111+ renderDestinations(currentDests);
112112+113113+ // Update cache with the newly resolved handle
114114+ const updatedCachedHandles = {
115115+ ...cachedHandles,
116116+ [info.did]: resolvedHandle,
117117+ };
118118+ await chrome.storage.local.set({
119119+ [DID_HANDLE_CACHE_KEY]: updatedCachedHandles,
120120+ });
121121+ console.log(
122122+ `Popup: Saved resolved handle ${resolvedHandle} for DID ${info.did} to cache.`
123123+ );
124124+ } else {
125125+ if (!currentDests || currentDests.length === 0) {
126126+ list.innerHTML =
127127+ "<li>Could not resolve identifier. No actions available.</li>";
128128+ }
129129+ console.log(
130130+ `Popup: Handle resolution returned null/false for DID: ${info.did}`
131131+ );
132132+ }
133133+ } catch (error) {
134134+ console.error("Popup: Error resolving handle:", error);
135135+ if (!currentDests || currentDests.length === 0) {
136136+ list.innerHTML =
137137+ "<li>Error resolving identifier. No actions available.</li>";
138138+ }
139139+ }
140140+ }
141141+ } else if (info.did && info.handle) {
142142+ console.log(
143143+ "Popup: DID and handle already present in info, no resolution or cache check needed.",
144144+ JSON.stringify(info, null, 2)
145145+ );
146146+ } else {
147147+ console.log(
148148+ "Popup: Condition for handle resolution/cache check not met (no DID or handle already present)."
149149+ );
150150+ }
151151+152152+ // Add event listener for the Empty Cache button
153153+ const emptyCacheButton = document.getElementById("emptyCacheBtn");
154154+ if (emptyCacheButton) {
155155+ emptyCacheButton.addEventListener("click", async () => {
156156+ try {
157157+ await chrome.storage.local.remove(DID_HANDLE_CACHE_KEY);
158158+ console.log("Popup: DID Handle Cache Cleared!");
159159+ // Optionally, provide user feedback in the popup itself, e.g., change button text
160160+ emptyCacheButton.textContent = "Cache Cleared!";
161161+ setTimeout(() => {
162162+ emptyCacheButton.textContent = "Empty Cache";
163163+ }, 1500);
164164+ // You might want to re-render or force a re-check if the current view depends on cached data
165165+ // For now, it just clears it. Next time popup opens with a relevant DID, it will miss cache.
166166+ } catch (error) {
167167+ console.error("Popup: Error clearing cache:", error);
168168+ emptyCacheButton.textContent = "Error Clearing!";
169169+ }
170170+ });
171171+ } else {
172172+ console.warn("Popup: Empty Cache button not found.");
173173+ }
174174+175175+ console.log("Popup script finished (caching re-enabled)."); // Log: Script end
31176})();
+56
service-worker.js
···11+// service-worker.js
22+33+// Try to import transform.js.
44+// In Manifest V3, for service workers, direct global function exposure from importScripts is typical.
55+// If transform.js is structured to put its functions on a global (e.g., self.WormholeTransform),
66+// we'd use that. Assuming it makes functions globally available for now.
77+try {
88+ importScripts('transform.js');
99+} catch (e) {
1010+ console.error("Failed to import transform.js in service-worker:", e);
1111+}
1212+1313+const DID_HANDLE_CACHE_KEY = 'didHandleCache'; // Caching re-enabled
1414+1515+chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
1616+ // Ensure functions are available (they should be if importScripts worked)
1717+ if (typeof parseInput !== 'function' || typeof resolveDidToHandle !== 'function') {
1818+ console.error('Service Worker: parseInput or resolveDidToHandle not defined. Check transform.js import.');
1919+ return;
2020+ }
2121+2222+ if (changeInfo.status === 'complete' && tab.url) {
2323+ console.log(`Service Worker: Tab ${tabId} updated to complete, URL: ${tab.url}`);
2424+ try {
2525+ const info = await parseInput(tab.url);
2626+ if (info && info.did && !info.handle) { // We have a DID but no immediate handle
2727+ console.log(`Service Worker: Found DID ${info.did} from ${tab.url}, attempting to resolve handle for pre-caching.`);
2828+2929+ // Check if we already have this DID in cache to avoid unnecessary re-fetching by the service worker itself
3030+ // The popup will have its own logic to prefer cache then fetch.
3131+ const cache = await chrome.storage.local.get(DID_HANDLE_CACHE_KEY);
3232+ const currentCache = cache[DID_HANDLE_CACHE_KEY] || {};
3333+ if (currentCache[info.did]) {
3434+ console.log(`Service Worker: Handle for DID ${info.did} already in cache, no pre-fetch needed by worker.`);
3535+ return;
3636+ }
3737+3838+ const resolvedHandle = await resolveDidToHandle(info.did);
3939+4040+ if (resolvedHandle) {
4141+ console.log(`Service Worker: Successfully resolved DID ${info.did} to handle ${resolvedHandle}. Caching.`);
4242+ const newCacheEntry = { [info.did]: resolvedHandle };
4343+ const updatedCache = { ...currentCache, ...newCacheEntry };
4444+ await chrome.storage.local.set({ [DID_HANDLE_CACHE_KEY]: updatedCache });
4545+ console.log(`Service Worker: DID ${info.did} -> Handle ${resolvedHandle} saved to cache.`);
4646+ } else {
4747+ console.log(`Service Worker: Could not resolve handle for DID ${info.did} from ${tab.url}. Not caching.`);
4848+ }
4949+ }
5050+ } catch (error) {
5151+ console.error(`Service Worker: Error processing tab update for ${tab.url}:`, error);
5252+ }
5353+ }
5454+});
5555+5656+console.log("Service Worker started and listeners added (caching re-enabled).");