This commit is contained in:
Peter 2023-06-18 15:29:10 +08:00
commit 02e9b44285
3 changed files with 37 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.zip

19
background.js Normal file
View File

@ -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"]
);

17
manifest.json Normal file
View File

@ -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/*"
]
}