-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.js
More file actions
143 lines (122 loc) · 5 KB
/
Copy pathrss.js
File metadata and controls
143 lines (122 loc) · 5 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
139
140
141
142
143
export async function fetchFeedAndParse(url, cache = {}) {
const headers = {};
if (cache.etag) headers["If-None-Match"] = cache.etag;
if (cache.lastModified) headers["If-Modified-Since"] = cache.lastModified;
let res;
try {
res = await fetch(url, { headers });
} catch (err) {
console.warn(`[RSS-BOOK] Fetch failed for ${url}:`, err.message);
return null;
}
if (res.status === 304) {
return { title: "", items: [], etag: cache.etag, lastModified: cache.lastModified };
}
if (!res.ok) {
console.warn(`[RSS-BOOK] Feed returned ${res.status}: ${url}`);
return null;
}
const text = await res.text();
const etag = res.headers.get("ETag") || "";
const lastModified = res.headers.get("Last-Modified") || "";
// DOMParser is NOT available in MV3 service workers — use regex-based XML parsing
// RSS 2.0? (zuerst prüfen — <rss> ist ein eindeutiges Top-Level-Tag; wenn ein RSS-Feed
// "<feed>" als Text in einem CDATA-Beschreibungsfeld enthält, würde eine nachgestellte
// Atom-Prüfung den Feed fälschlich als Atom erkennen und 0 Items liefern)
if (/<rss[\s>]/i.test(text) || /<channel[\s>]/i.test(text)) {
const channelBlock = extractFirstBlock(text, "channel") || text;
const title = extractDirectChildText(channelBlock, "title");
const channelItems = extractAllBlocks(channelBlock, "item");
const itemBlocks = channelItems.length ? channelItems : extractAllBlocks(text, "item");
const items = itemBlocks.map(parseRssItem);
return { title, items, etag, lastModified };
}
// Atom?
if (/<feed[\s>]/i.test(text)) {
const title = extractTagText(text, "feed", "title");
const items = extractAllBlocks(text, "entry").map(parseAtomEntry);
return { title, items, etag, lastModified };
}
console.warn(`[RSS-BOOK] Unknown feed format: ${url}`);
return null;
}
// --- Regex-based XML helpers ---
function extractFirstBlock(xml, tag) {
// Match <tag ...>...</tag> (non-greedy, handles nested same-name tags poorly but OK for feeds)
const re = new RegExp(`<${tag}[\\s>][\\s\\S]*?</${tag}>`, "i");
const m = xml.match(re);
return m ? m[0] : "";
}
function extractAllBlocks(xml, tag) {
const re = new RegExp(`<${tag}[\\s>][\\s\\S]*?</${tag}>`, "gi");
return [...xml.matchAll(re)].map(m => m[0]);
}
function extractTagText(xml, parentTag, childTag) {
// Finds the first <childTag> inside the first <parentTag> block
const parent = extractFirstBlock(xml, parentTag);
if (!parent) return "";
return extractTextContent(parent, childTag);
}
function extractDirectChildText(block, tag) {
// Gets text content of the first occurrence of <tag>
return extractTextContent(block, tag);
}
function extractTextContent(xml, tag) {
// Matches <tag>text</tag> or <tag attr="...">text</tag>, extracts text
const re = new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)</${tag}>`, "i");
const m = xml.match(re);
if (!m) return "";
return decodeXmlEntities(stripCdata(m[1].trim()));
}
function stripCdata(text) {
return text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/gi, "$1");
}
function extractAttr(xml, tag, attr) {
// Matches <tag ... attr="value" ...> or <tag ... attr='value' ...>
const tagRe = new RegExp(`<${tag}\\s[^>]*${attr}\\s*=\\s*["']([^"']*)["'][^>]*/?>`, "i");
const m = xml.match(tagRe);
return m ? decodeXmlEntities(m[1]) : "";
}
function decodeXmlEntities(s) {
return s
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)))
.replace(/&#x([0-9a-fA-F]+);/g, (_, h) => String.fromCharCode(parseInt(h, 16)))
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&/g, "&");
}
// --- Feed item parsers ---
function parseRssItem(block) {
const title = extractTextContent(block, "title") || "(ohne Titel)";
const link = extractTextContent(block, "link") || "";
const guid = extractTextContent(block, "guid") || "";
const pubDate = extractTextContent(block, "pubDate") || "";
return { title, link, guid, published: pubDate };
}
function parseAtomEntry(block) {
const title = extractTextContent(block, "title") || "(ohne Titel)";
// Atom <link> uses href attribute; prefer rel="alternate"
const link = extractAtomAlternateLink(block) || extractAtomLink(block) || "";
const id = extractTextContent(block, "id") || "";
const updated = extractTextContent(block, "updated") || "";
return { title, link, guid: id, published: updated };
}
function extractAtomAlternateLink(block) {
// Match <link ... rel="alternate" ... href="..." ...> regardless of attribute order
const links = [...block.matchAll(/<link\s[^>]*\/?>/gi)];
for (const m of links) {
const tag = m[0];
if (/rel\s*=\s*["']alternate["']/i.test(tag)) {
const href = tag.match(/href\s*=\s*["']([^"']*)["']/i);
if (href) return decodeXmlEntities(href[1]);
}
}
return "";
}
function extractAtomLink(block) {
// Find first <link ... href="..."> in block
const m = block.match(/<link\s[^>]*href\s*=\s*["']([^"']*)["'][^>]*\/?>/i);
return m ? decodeXmlEntities(m[1]) : "";
}