-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.js
More file actions
31 lines (27 loc) · 1.17 KB
/
Copy pathreadability.js
File metadata and controls
31 lines (27 loc) · 1.17 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
// Lightweight readability extraction — strips nav/footer/aside/ads,
// prefers article/main content, falls back to body.innerText.
(() => {
const strip = ['nav', 'footer', 'aside', 'header', '[role="navigation"]',
'[role="banner"]', '[role="contentinfo"]', '[aria-hidden="true"]',
'.ad', '.ads', '.advertisement', '.sidebar', '.cookie-banner',
'#cookie-consent', '.popup', '.modal',
'#SIvCob', '[data-locale-picker]', '[role="listbox"]',
'#Lb4nn', '.language-selector', '.locale-selector',
'[data-language-picker]', '#langsec-button'];
// Try article or main first
let root = document.querySelector('article') ||
document.querySelector('[role="main"]') ||
document.querySelector('main');
if (!root) {
// Clone body and strip junk
root = document.body.cloneNode(true);
for (const sel of strip) {
root.querySelectorAll(sel).forEach(el => el.remove());
}
} else {
root = root.cloneNode(true);
}
// Clean up: remove scripts, styles, hidden elements
root.querySelectorAll('script, style, noscript, svg, [hidden]').forEach(el => el.remove());
return root.innerText.replace(/\n{3,}/g, '\n\n').trim();
})()