-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadRemover.js
More file actions
96 lines (86 loc) · 2.47 KB
/
Copy pathadRemover.js
File metadata and controls
96 lines (86 loc) · 2.47 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
/**
* Ad Remover for Page Download Extension
* Removes ad elements from downloaded HTML based on common patterns
*/
// Simple EasyList parser focused only on element hiding rules
function parseEasyListForSelectors(easyListContent) {
const lines = easyListContent.split('\n');
const selectors = [];
for (const line of lines) {
try {
// Skip comments and non-selector rules
if (line.startsWith('!') || line.startsWith('[') || line.trim() === '') {
continue;
}
// Handle standard element hiding rules (##)
if (line.includes('##')) {
const parts = line.split('##');
if (parts.length === 2) {
// Add generic rules (no domain specified)
if (parts[0] === '') {
selectors.push(parts[1]);
}
}
}
} catch (error) {
// Skip problematic rules
console.log('Error parsing rule:', error);
}
}
return selectors;
}
// Remove ads from HTML content by adding a style tag
// Now accepts EasyList selectors parameter
function removeAdsFromHTML(html, easyListSelectors = []) {
try {
// Common ad selectors to hide
const commonAdSelectors = [
// Ad containers
'[class*="ad-container"]',
'[class*="ad-wrapper"]',
'[class*="adunit"]',
'[class*="adsbox"]',
'[id*="ad-container"]',
// Ad networks
'[class*="adsbygoogle"]',
'[id*="div-gpt-ad"]',
'[id*="google_ads"]',
// Common patterns
'.advertisement',
'.sponsored-content',
'.dfp-tag',
'.banner-ads',
'.ad-placement',
// Iframes
'iframe[src*="doubleclick.net"]',
'iframe[src*="googlesyndication.com"]',
'iframe[src*="ad-delivery"]'
];
// Combine EasyList selectors with our common selectors
// Filter out any invalid selectors that might cause issues
const allSelectors = [...new Set([...easyListSelectors, ...commonAdSelectors])];
// Create a style tag to hide ads instead of removing them
const adBlockingStyle = `
<style>
/* Hide ad elements - combined from EasyList and common patterns */
${allSelectors.join(',\n')} {
display: none !important;
}
</style>
`;
// Insert our style tag before the closing head tag
let modifiedHtml = html;
if (html.includes('</head>')) {
modifiedHtml = html.replace('</head>', `${adBlockingStyle}</head>`);
}
return modifiedHtml;
} catch (error) {
console.error('Error removing ads from HTML:', error);
return html; // Return original HTML on error
}
}
// Export functionality
self.AdRemover = {
parseEasyListForSelectors,
removeAdsFromHTML
};