Skip to content

Commit

Permalink
Add rewriting to background script
Browse files Browse the repository at this point in the history
  • Loading branch information
TetraTsunami committed Jul 12, 2024
1 parent fbb7681 commit 1d417e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
28 changes: 21 additions & 7 deletions src/background/index.chrome.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import { DOMParser } from 'linkedom';
import { installedHandler } from './background';
import { parseAndReply } from './parsing';
import { resolveURL } from './services';

const scrapeHandler = async ({ url }, res: (response?: any) => void) => {
let oldUrl = url
let newUrl = ""
try {
const resp = await fetch(url)
const html = await resp.text();
const doc = new DOMParser().parseFromString(html, 'text/html');
// @ts-expect-error - linkedom's document is FAKE and missing lots of properties, but we don't care because we don't use them :)
await parseAndReply(doc, url, res)
let doc: Document
while (oldUrl !== newUrl) {
oldUrl = newUrl
const resp = await fetch(oldUrl)
const html = await resp.text()
// @ts-expect-error - linkedom's document is FAKE and missing lots of properties, but we don't care because we don't use them :)
doc = new DOMParser().parseFromString(html, 'text/html');
newUrl = await resolveURL(doc, oldUrl) || oldUrl
}
await parseAndReply(doc, newUrl, res)
} catch (err) {
res({ error: err.message })
}
}

const parseHTMLHandler = async ({ html, url }, res: (response?: any) => void) => {
try {
const doc = new DOMParser().parseFromString(html, 'text/html');
const doc = new DOMParser().parseFromString(html, "text/html")
// @ts-expect-error - see above
await parseAndReply(doc, url, res)
const newUrl = await resolveURL(doc, url)
if (newUrl) {
await scrapeHandler({ url: newUrl }, res)
} else {
// @ts-expect-error - see above
await parseAndReply(doc, url, res)
}
} catch (err) {
res({ error: err.message })
}
Expand Down
23 changes: 18 additions & 5 deletions src/background/index.firefox.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { installedHandler } from "./background";
import { parseAndReply } from "./parsing";
import { resolveURL } from "./services";

const scrapeHandler = async ({ url }, res: (response?: any) => void) => {
let oldUrl = url
let newUrl = ""
try {
const resp = await fetch(url)
const html = await resp.text();
const doc = new DOMParser().parseFromString(html, "text/html")
await parseAndReply(doc, url, res)
let doc: Document
while (oldUrl !== newUrl) {
oldUrl = newUrl
const resp = await fetch(oldUrl)
const html = await resp.text()
doc = new DOMParser().parseFromString(html, "text/html")
newUrl = await resolveURL(doc, oldUrl) || oldUrl
}
await parseAndReply(doc, newUrl, res)
} catch (err) {
res({ error: err.message })
}
Expand All @@ -15,7 +23,12 @@ const scrapeHandler = async ({ url }, res: (response?: any) => void) => {
const parseHTMLHandler = async ({ html, url }, res: (response?: any) => void) => {
try {
const doc = new DOMParser().parseFromString(html, "text/html")
await parseAndReply(doc, url, res)
const newUrl = await resolveURL(doc, url)
if (newUrl) {
await scrapeHandler({ url: newUrl }, res)
} else {
await parseAndReply(doc, url, res)
}
} catch (err) {
res({ error: err.message })
}
Expand Down

0 comments on commit 1d417e9

Please sign in to comment.