commit 02e9b442851bd9ac4cff556cb8093197728abe7e Author: Peter Date: Sun Jun 18 15:29:10 2023 +0800 v1.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f66c74 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.zip \ No newline at end of file diff --git a/background.js b/background.js new file mode 100644 index 0000000..d082495 --- /dev/null +++ b/background.js @@ -0,0 +1,19 @@ +browser.webRequest.onBeforeRequest.addListener( + async (details) => { + const url = details.url; + const tabId = details.tabId; + + const response = await fetch(url); + const html = await response.text(); + const regex = /"name":"Datasheet","page_count":\d+,"url":"([^"]+)"/; + const match = html.match(regex); + + if (match && match[1]) { + // use first match - it seems like octopart's html viewer can render + // multiple documents but i have not found an example of this. + browser.tabs.update(tabId, { url: match[1] }); + } + }, + { urls: ["https://octopart.com/datasheet/*"], types: ["main_frame"] }, + ["blocking"] +); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..92fb49f --- /dev/null +++ b/manifest.json @@ -0,0 +1,17 @@ +{ + "manifest_version": 2, + "name": "Redirect Octopart datasheets", + "version": "1.0", + + "description": "View the original datasheet PDF in the browser's native PDF viewer instead of the Octopart's HTML PDF viewer.", + + "background": { + "persistent": true, + "scripts": ["background.js"] + }, + "permissions": [ + "webRequest", + "webRequestBlocking", + "https://octopart.com/datasheet/*" + ] +}