Skip to content

Commit

Permalink
[fix] 拓宽判断是否标准HTML的条件
Browse files Browse the repository at this point in the history
  • Loading branch information
Eticeweng committed May 10, 2024
1 parent e8e5797 commit 17c1b10
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const STANDARD_HEADERS = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
};
const MARK = ["<html>", "<!DOCTYPE html"];
async function fetchSegment(url) {
const controller = new AbortController();
CUSTOM_LOG("about to fetch", url);
Expand All @@ -42,7 +43,9 @@ async function fetchSegment(url) {
}
read += textDecoder.decode(value);
receivedLength += value.length;
if (!read.startsWith("<!DOCTYPE html>")) {
let trimmedRead = read.trimStart();
let valid = MARK.find(mark => trimmedRead.startsWith(mark));
if (!valid) {
controller.abort();
CUSTOM_LOG(
":endpoint not standard HTML, aborting",
Expand Down
16 changes: 11 additions & 5 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,18 @@ function patchCSS() {
document.querySelector("html > head").appendChild(cssPatch);
}

const FETCHING = new Set();
let fetching = new Set();
let cacheMap = new Map();
async function loadPreview(url, container) {
let id = await sha1(url);
let nullableResult = cacheMap.get(id);
if (!nullableResult) {
if (FETCHING.has(id)) {
if (fetching.has(id)) {
return;
}
FETCHING.add(id);
fetching.add(id);
let turn = await window.link_preview.bakePreview(url);
FETCHING.delete(id);
fetching.delete(id);
turn._url = new URL(url);
if (!turn.error) {
turn.result = extractContent(htmlParser(turn.result));
Expand Down Expand Up @@ -181,11 +181,17 @@ async function loadPreview(url, container) {
// nullableResult.result
// );
// }
debounce(render(container, nullableResult), 100);
render(container, nullableResult);
}
}
onLoad();

// todo: debug
// Object.defineProperty(window, "lpDebug", {
// value: cacheMap,
// writable: false
// });

async function onLoad() {
const observer = new MutationObserver(async (mutationsList) => {
for (let mutation of mutationsList) {
Expand Down

0 comments on commit 17c1b10

Please sign in to comment.