|
| 1 | +import { showStatus } from "./ui.js"; |
| 2 | + |
| 3 | +function buildIssueUrl(tabUrl) { |
| 4 | + const domain = new URL(tabUrl).hostname.replace(/^www\./, ''); |
| 5 | + const title = `Unsupported URL detected on ${domain}`; |
| 6 | + const body = [ |
| 7 | + 'This page is not currently supported by Marian:', |
| 8 | + '', |
| 9 | + tabUrl, |
| 10 | + '', |
| 11 | + '**Steps to reproduce:**', |
| 12 | + '1. Open the above URL with the extension installed', |
| 13 | + '2. Open the extension sidebar', |
| 14 | + '3. See that details are not loaded', |
| 15 | + '', |
| 16 | + '**Expected behavior:**', |
| 17 | + 'Details should load for supported product pages.' |
| 18 | + ].join('\n'); |
| 19 | + |
| 20 | + return 'https://github.com/jacobtender/marian-extension/issues/new' |
| 21 | + + `?title=${encodeURIComponent(title)}` |
| 22 | + + `&body=${encodeURIComponent(body)}` |
| 23 | + + `&labels=${encodeURIComponent('bug')}`; |
| 24 | +} |
| 25 | + |
| 26 | +// Polling function to try multiple times before giving up (unchanged behavior) |
| 27 | +export function tryGetDetails(retries = 8, delay = 300) { |
| 28 | + let didRefresh = false; |
| 29 | + |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + function attempt(remaining) { |
| 32 | + chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => { |
| 33 | + if (!tab?.id) { |
| 34 | + reject('No active tab found.'); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + chrome.tabs.sendMessage(tab.id, 'ping', (response) => { |
| 39 | + console.log('Ping response:', response, 'Remaining attempts:', remaining); |
| 40 | + if (chrome.runtime.lastError || response !== 'pong') { |
| 41 | + if (remaining > 0) { |
| 42 | + setTimeout(() => attempt(remaining - 1), delay); |
| 43 | + } else { |
| 44 | + if (!didRefresh) { |
| 45 | + didRefresh = true; |
| 46 | + // showStatus("Content script not ready, refreshing tab..."); |
| 47 | + chrome.tabs.reload(tab.id, { bypassCache: true }); |
| 48 | + showStatus("Tab reloaded, fetching details..."); |
| 49 | + |
| 50 | + const onUpdated = (updatedTabId, info) => { |
| 51 | + if (updatedTabId === tab.id && info.status === 'complete') { |
| 52 | + chrome.tabs.onUpdated.removeListener(onUpdated); |
| 53 | + console.log(retries, 'Tab reloaded, fetching details again...'); |
| 54 | + setTimeout(() => attempt(retries), 350); |
| 55 | + } |
| 56 | + }; |
| 57 | + chrome.tabs.onUpdated.addListener(onUpdated); |
| 58 | + } else { |
| 59 | + const issueUrl = buildIssueUrl(tab?.url || '(unknown URL)'); |
| 60 | + showStatus(` |
| 61 | + This site is supported, but this page isn't yet.<br/> |
| 62 | + Please <a href="${issueUrl}" target="_blank" rel="noopener noreferrer">report</a> the full URL of this page so we can add support! |
| 63 | + `); |
| 64 | + // reject('Unsupported URL or no content script after refresh.'); |
| 65 | + } |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + chrome.tabs.sendMessage(tab.id, 'getDetails', (details) => { |
| 71 | + if (chrome.runtime.lastError || !details) { |
| 72 | + reject('Failed to retrieve book details.'); |
| 73 | + return; |
| 74 | + } |
| 75 | + resolve(details); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + } |
| 80 | + attempt(retries); |
| 81 | + }); |
| 82 | +} |
0 commit comments