-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_cleaner.js
159 lines (149 loc) · 5.37 KB
/
web_cleaner.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ==UserScript==
// @name Clean News Pages
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Remove distracting elements from various news sites
// @author You
// @match https://www.npr.org/*
// @match https://www.nationalgeographic.com/*
// @match https://www.bbc.com/*
// @match https://www.scientificamerican.com/*
// @match https://www.nytimes.com/*
// @match https://www.vox.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 定义要移除的元素和属性配置
const config = {
"https://www.npr.org": {
"elements": ["header", "footer","aside"],
"classes": [
"share-tools share-tools--secondary",
"callout-end-of-story-mount-piano-wrap",
"recommended-stories",
"bucketwrap internallink insettwocolumn inset2col",
"audio-module"
],
"attras": {
"id": ["main-sidebar","npr-player"],
"aria-label": ["Tweet","advertisement"]
}
},
"https://www.nationalgeographic.com": {
"classes": [
"AlertBanner alert_top",
"GlobalNav",
"InlineImagePromo__Content",
"FITT_Article_end",
"FrameBackgroundFull FrameBackgroundFull--dark",
"InlineEmail__Container",
"theme-e FITT_Article_main__sidebar oBTii mrzah pNwJE iWsMV vkle",
"GlobalFooter",
"GlobalFooter__CopyrightWrapper",
"natgeo-ad",
"CHWlW ZgZTu FlDNH hQfuy feniZ wBpop uAyYK yJYJo OGgqj tQHio PCVZs Kiog iKqXF eeiyR",
"rEPuv seFhp WtEci"
],
"attras": {
"data-testid": [
"prism-editors-note",
"prism-share",
"prism-newsletter-form"
]
}
},
"https://www.bbc.com": {
"elements": ["header","aside","footer","nav"],
"attras":{
"data-component":["links-block"]
}
},
"https://www.scientificamerican.com": {
"elements": ["header", "footer","aside"]
},
"https://www.nytimes.com": {
"elements": ["footer","aside"],
"attras": {
"data-testid": [
"share-tools"
],
"id":["bottom-wrapper"]
},
"classes":["bottom-of-article"]
},
"https://www.vox.com":{
"elements":["aside", "footer", "nav","form"],
"attras": {
"aria-label":["Top Navigation"],
"data-concert-ads-name":[
"desktop_article_body",
"desktop_feature_footer",
"desktop_feature_body",
"desktop_feature_body_dynamic"
]
},
"classes":[
"_1j2ggcb0",
"x2c1mg2 x2c1mg5 x2c1mg7",
"duet--layout--rail x2c1mg0 x2c1mg1",
"duet--layout--article-recirc _1ot72dd0 _1agbrix1",
"dynamic-js-slot dfp_ad--held-area up-show",
"duet--ad-container--default",
"duet--layout--header-pattern _1iaikll1",
"duet--cta--newsletter mr4kua8"
]
}
};
// 获取当前网站的配置
const currentHost = window.location.hostname;
const currentConfig = config[`https://${currentHost}`];
if (currentConfig) {
// 定义移除元素的函数
function removeElements() {
// 移除指定的元素
if (currentConfig.elements) {
currentConfig.elements.forEach(selector => {
const elements = document.querySelectorAll(selector);
elements.forEach(element => element.remove());
});
}
// 移除指定的类
if (currentConfig.classes) {
currentConfig.classes.forEach(className => {
const elements = document.getElementsByClassName(className);
while (elements.length > 0) {
elements[0].remove();
}
});
}
// 移除指定属性的元素
if (currentConfig.attras) {
for (const attr in currentConfig.attras) {
currentConfig.attras[attr].forEach(value => {
const selector = `[${attr}="${value}"]`;
const elements = document.querySelectorAll(selector);
elements.forEach(element => element.remove());
});
}
}
}
// 初始移除元素
removeElements();
// 创建 MutationObserver 以监听 DOM 变化
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
removeElements();
}
});
});
// 配置 MutationObserver
const config = {
childList: true,
subtree: true
};
// 开始观察文档主体
observer.observe(document.body, config);
}
})();