From 9089653961fd99b79d3e4d36c9f0321666a884b1 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sat, 2 Nov 2024 16:05:58 +0530 Subject: [PATCH] Fix: Handle PDF content in Firefox Firefox does not allow extensions to run content scripts on PDF pages, so we have to work around this limitation. This commit adds a workaround that retrieves the PDF URL and type directly from the tab. This ensures that the extension can handle PDFs in Firefox while waiting for the bug in Firefox to be fixed. --- src/libs/get-html.ts | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/libs/get-html.ts b/src/libs/get-html.ts index ddb2ad7..29bbd9e 100644 --- a/src/libs/get-html.ts +++ b/src/libs/get-html.ts @@ -42,14 +42,30 @@ export const getDataFromCurrentTab = async () => { .query({ active: true, currentWindow: true }) .then(async (tabs) => { const tab = tabs[0] + try { + const data = await browser.scripting.executeScript({ + target: { tabId: tab.id }, + func: _getHtml + }) - const data = await browser.scripting.executeScript({ - target: { tabId: tab.id }, - func: _getHtml - }) - - if (data.length > 0) { - resolve(data[0].result) + if (data.length > 0) { + resolve(data[0].result) + } + } catch (e) { + console.log("error", e) + // this is a weird method but it works + if (import.meta.env.BROWSER === "firefox") { + // all I need is to get the pdf url but somehow + // firefox won't allow extensions to run content scripts on pdf https://bugzilla.mozilla.org/show_bug.cgi?id=1454760 + // so I set up a weird method to fix this issue by asking tab to give the url + // and then I can get the pdf url + const result = { + url: tab.url, + content: "", + type: "pdf" + } + resolve(result) + } } }) }