forked from MrDaPoyo/indieseas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
138 lines (126 loc) · 3.26 KB
/
Copy pathworker.js
File metadata and controls
138 lines (126 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { parse } from "node-html-parser";
export default {
async fetch(request, env, ctx) {
let url = new URL(request.url);
const apiKey = url.searchParams.get("key");
if (apiKey !== env.API_KEY) {
return new Response("Unauthorized: Invalid API key", {
status: 401,
});
}
let name = url.searchParams.get("path");
if (!name) return new Response("No URL to scrape detected.");
if (name.startsWith("http://") || name.startsWith("https://")) {
name = new URL(name).href;
} else {
name = `https://${name}`;
}
const answer = await fetch(name);
if (!answer)
return new Response(`Failed to fetch: ${name}`, { status: 500 });
if (!answer.ok)
return new Response(`Failed to fetch: ${name}`, { status: 500 });
let foundButtons = {};
const answerText = await answer.text();
const root = parse(answerText, {
fixNestedATags: false,
parseNoneClosedTags: false,
blockTextElements: {
script: false,
noscript: false,
style: false,
pre: false,
textarea: false,
document: false,
},
comment: false,
ignoreWhitespace: true,
normalizeWhitespace: true,
trimWhitespace: true,
upperCaseTagName: false,
lowerCaseTagName: false,
});
const images = root.querySelectorAll("img");
for (const img of images) {
if (!img) continue;
const imgSrc = img.getAttribute("src");
if (!imgSrc) continue;
var src = imgSrc;
if (
src &&
!src.startsWith("http://") &&
!src.startsWith("https://")
) {
src = new URL(src, name).href;
}
let links_to = null;
const parentAnchor = img.closest("a");
if (parentAnchor && parentAnchor.getAttribute("href")) {
const href = parentAnchor.getAttribute("href");
try {
if (
!href.startsWith("http://") &&
!href.startsWith("https://")
) {
if (href.startsWith("/")) {
links_to = new URL(href, name).href;
} else {
links_to = `https://${href}`;
}
} else {
links_to = href;
}
} catch (error) {
links_to = href;
}
}
const imgAlt = img.getAttribute("alt");
const imgTitle = img.getAttribute("title");
if (!foundButtons.buttons) {
foundButtons.buttons = [];
}
foundButtons.buttons.push({
src: src,
links_to: links_to,
alt: imgAlt,
title: imgTitle,
});
}
let allText = root.text || "";
allText = allText.replace(/\n/g, " ");s
allText = allText.replace(/\r/g, " ");
allText = allText.replace(/<\/?[^>]+(>|$)/g, " $& ");
allText = allText.replace(/ +/g, " ");
allText = allText
.replace(/<[^>]+>/g, " ")
.trim()
.toLowerCase();
return new Response(
JSON.stringify({
buttons: foundButtons.buttons || [],
title: root.querySelector("title")?.text,
description: root
.querySelector("meta[name='description']")
?.getAttribute("content"),
keywords: root
.querySelector("meta[name='keywords']")
?.getAttribute("content"),
meta: root.querySelector("meta")?.getAttribute("content"),
rawText: allText,
links: root.querySelectorAll("a").map(function (a) {
return {
href: a.getAttribute("href"),
text: a.text,
};
}),
}) || {},
{
status: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
}
);
},
};