Skip to content

Commit

Permalink
fix encoding and parsing of certain websites that require referer
Browse files Browse the repository at this point in the history
  • Loading branch information
maoserr committed Nov 9, 2024
1 parent fae5272 commit e2a5b98
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
19 changes: 19 additions & 0 deletions assets/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const main_def = {
func: readability_ex,
inputs: {}
},
'RawHTML':{
func: rawChapter,
inputs: {}
}
}
}

Expand Down Expand Up @@ -274,6 +278,7 @@ function nu_toc_parser(inputs, url, source, helpers) {
*/
function readability(inputs, url, source, helpers) {
let parser = new DOMParser();
console.log(source)
let dom = parser.parseFromString(source, "text/html");
let out = helpers["readability"](dom);
// Generic parser
Expand All @@ -284,6 +289,20 @@ function readability(inputs, url, source, helpers) {
};
}

function rawChapter(inputs, url, source, helpers) {
let parser = new DOMParser();
let dom = parser.parseFromString(source, "text/html");
dom.querySelectorAll("img[src]").forEach(
e=>console.log(e.getAttribute('src')))
let out = dom.body.outerHTML;
// Generic parser
return {
title: out.title,
html: out.content,
message: "Parsed simple chapter"
};
}


/**
* Parse using extended readability logic
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"primeicons": "^7.0.0",
"primevue": "^3.53.0",
"prismjs": "^1.29.0",
"tsepub": "^1.1.25",
"tsepub": "^1.1.29",
"vue": "^3.5.12",
"vue-codemirror": "^6.1.1",
"vue-prism-editor": "^2.0.0-alpha.2",
Expand Down
24 changes: 23 additions & 1 deletion src/entry/service_worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import browser, {Tabs} from "webextension-polyfill";
import browser, {DeclarativeNetRequest, Tabs} from "webextension-polyfill";
import Rule = DeclarativeNetRequest.Rule;

function set_badge() {
const man = browser.runtime.getManifest()
Expand All @@ -7,6 +8,27 @@ function set_badge() {
browser.action.setBadgeText({text: "d"}).then().catch(() => {
});
}
const rules: Rule[] = [{
id: 1,
condition: {
initiatorDomains: [browser.runtime.id],
resourceTypes: ['xmlhttprequest', 'image'],
},
action: {
type: 'modifyHeaders',
requestHeaders: [{
header: 'Referer',
operation: 'set',
value: 'https://sspai.com',
}],
},
}];
browser.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map(r => r.id),
addRules: rules,
}).then(r=>console.log(`Rule successfully added: ${browser.runtime.getURL("")}`)).catch(
e => console.error("Failed to add rule")
);
}

browser.runtime.onStartup.addListener(set_badge)
Expand Down
4 changes: 3 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"downloads",
"storage",
"clipboardRead",
"unlimitedStorage"],
"unlimitedStorage",
"declarativeNetRequestWithHostAccess",
"declarativeNetRequestFeedback"],
"options_ui": {
"page": "options.html"
}
Expand Down

0 comments on commit e2a5b98

Please sign in to comment.