-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
184 lines (158 loc) · 4.52 KB
/
content.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* global chrome, PHRASES_TO_HIDE */
(function () {
const itemClass = 'TimelineItem';
const itemHiddenClass = 'TimelineItem-Hidden';
const toggleButtonID = 'tidy-timeline-button';
let status = 'active';
let numMatchingEvents = 0;
let pageUrl;
let monitorInterval;
const showEvent = (el) => {
el.classList.remove(itemHiddenClass);
el.classList.add(itemClass);
};
const hideEvent = (el) => {
el.classList.remove(itemClass);
el.classList.add(itemHiddenClass);
};
const querySelectorIncludesText = (selector, text) => {
return Array.from(document.querySelectorAll(selector)).find((el) =>
el.textContent.includes(text),
);
};
const getToggleButton = () => {
return document.getElementById(toggleButtonID);
};
const createToggleButton = () => {
const btn = document.createElement('button');
btn.setAttribute('id', toggleButtonID);
btn.onclick = handleClick;
document.body.appendChild(btn);
return btn;
};
const getItems = () => {
return document.querySelectorAll(`.${itemClass}`);
};
const getHiddenItems = () => {
return document.querySelectorAll(`.${itemHiddenClass}`);
};
const updateStatus = (newStatus) => {
status = newStatus;
updateToggleButton(status);
};
const updateToggleButton = async (status) => {
const btn = getToggleButton() || createToggleButton();
if (status !== 'idle') {
numMatchingEvents = getHiddenItems().length;
}
const count = `${numMatchingEvents} event${numMatchingEvents === 1 ? '' : 's'}`;
const message = status === 'idle' ? `Click to hide ${count}` : `${count} hidden`;
const loader =
status === 'loading' ? `<img src='${chrome.runtime.getURL('images/loader.gif')}' />` : '';
const icon =
status !== 'idle'
? `<span><img src='${chrome.runtime.getURL('images/32.png')}' /></span>`
: '';
btn.innerHTML = icon + message + loader;
await new Promise((res) => setTimeout(res, 10));
btn.classList.add('visible');
};
const loadMoreIfAvailable = () => {
const loadMoreBtn = querySelectorIncludesText('button', 'Load more');
if (loadMoreBtn) {
loadMoreBtn.click();
updateStatus('loading');
return true;
}
return false;
};
const tidyTimeline = () => {
getItems().forEach((el) => {
const shouldHide = PHRASES_TO_HIDE.find((phrase) => el.innerText.includes(phrase));
if (shouldHide) {
hideEvent(el);
}
});
updateStatus('active');
const loading = loadMoreIfAvailable();
if (!loading) {
stopWatching();
}
};
const observer = new MutationObserver((mutations) => {
const timelineChanges = mutations.find((mutation) => {
return [...mutation.addedNodes]
.map((m) => m.className && m.className.includes('js-timeline-item'))
.includes(true);
});
if (timelineChanges) {
tidyTimeline();
}
});
const startWatchingForTimelineChanges = (timeline) => {
observer.observe(timeline, {
attributes: false,
characterData: false,
childList: true,
subtree: true,
attributeOldValue: false,
characterDataOldValue: false,
});
};
const stopWatching = () => {
observer.disconnect();
};
const deactivate = () => {
getHiddenItems().forEach((el) => {
showEvent(el);
});
updateStatus('idle');
};
const handleClick = () => {
if (status === 'active') {
deactivate();
} else {
tidyTimeline();
}
};
const hideToggleButton = async () => {
const btn = getToggleButton();
if (btn) {
btn.classList.remove('visible');
}
};
const monitor = async () => {
const newPageUrl = document.location.href;
if (newPageUrl !== pageUrl) {
pageUrl = newPageUrl;
await new Promise((res) => setTimeout(res, 500));
const timeline = document.querySelectorAll('.pull-discussion-timeline')[0];
if (timeline) {
startWatchingForTimelineChanges(timeline);
tidyTimeline();
} else {
stopWatching();
await new Promise((res) => setTimeout(res, 500));
hideToggleButton();
}
}
};
const initialise = () => {
monitorInterval = setInterval(monitor, 1000);
};
window.addEventListener(
'load',
function load() {
window.removeEventListener('load', load, false);
initialise();
},
false,
);
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
clearInterval(monitorInterval);
} else {
initialise();
}
});
})();