From 02e9b442851bd9ac4cff556cb8093197728abe7e Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 18 Jun 2023 15:29:10 +0800 Subject: [PATCH] v1.0 --- .gitignore | 1 + background.js | 19 +++++++++++++++++++ manifest.json | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 .gitignore create mode 100644 background.js create mode 100644 manifest.json 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/*" + ] +}