-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
49 lines (37 loc) · 1.21 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
chrome.runtime.onMessage.addListener((request) => {
updateElements(request.query);
});
const findParent = (element, selector) => {
let parents = [];
while (element = element.parentElement.closest(selector)) {
parents.push(element);
}
return parents[0];
}
const hideElement = (element) => {
if (element) element.style.display = "none";
}
const showElement = (element) => {
if (element) element.style.display = "block";
}
const filterHidden = (elements) => (
elements.filter((element) => {
const timeline = findParent(element, ".js-timeline-item");
const comment = findParent(element, ".inline-comments");
return visible(timeline) || visible(comment)
})
)
const visible = (element) => {
if (!element) return null;
const style = window.getComputedStyle(element);
return (style.display != 'none') && (style.visibility != 'hidden');
}
const updateElements = (query) => {
const elements = document.querySelectorAll(query);
const hiddenElements = filterHidden([...elements])
action = hiddenElements.length ? hideElement : showElement;
elements.forEach(element => {
action(findParent(element, ".js-timeline-item"))
action(findParent(element, ".inline-comments"))
});
}