-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathhide_recommended_posts.js
More file actions
78 lines (62 loc) · 2.76 KB
/
hide_recommended_posts.js
File metadata and controls
78 lines (62 loc) · 2.76 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
import { buildStyle, getTimelineItemWrapper, filterPostElements, postSelector } from '../../utils/interface.js';
import { onNewPosts } from '../../utils/mutations.js';
import { timelineObject } from '../../utils/react_props.js';
import { followingTimelineFilter } from '../../utils/timeline_id.js';
const excludeClass = 'xkit-no-recommended-posts-done';
const hiddenAttribute = 'data-no-recommended-posts-hidden';
const unHiddenAttribute = 'data-no-recommended-posts-many';
const timeline = followingTimelineFilter;
const includeFiltered = true;
export const styleElement = buildStyle(`
[${hiddenAttribute}]:not([${unHiddenAttribute}]) {
content: linear-gradient(transparent, transparent);
height: 0;
}
:not([${unHiddenAttribute}]) + [${unHiddenAttribute}]::before {
content: "Too many recommended posts to hide!";
display: block;
padding: 25px 20px;
border-radius: 3px;
margin-bottom: var(--post-padding);
background-color: rgba(var(--white-on-dark), 0.13);
color: rgba(var(--white-on-dark), 0.65);
font-weight: 700;
text-align: center;
line-height: 1.5em;
}
`);
const precedingHiddenPosts = ({ previousElementSibling: previousElement }, count = 0) => {
// If there is no previous sibling, stop counting
if (!previousElement) return count;
// If the previous sibling is hidden, count it and continue
if (previousElement.matches(`[${hiddenAttribute}]`)) return precedingHiddenPosts(previousElement, count + 1);
// If the previous sibling is not a post, skip over it
if (!previousElement.matches(postSelector) || !previousElement.querySelector(postSelector)) return precedingHiddenPosts(previousElement, count);
// Otherwise, we've hit a non-hidden post; stop counting
return count;
};
const processPosts = async function (postElements) {
filterPostElements(postElements, { excludeClass, timeline, includeFiltered }).forEach(async postElement => {
const { recommendationReason } = await timelineObject(postElement);
if (!recommendationReason) return;
const { loggingReason } = recommendationReason;
if (!loggingReason) return;
if (loggingReason.startsWith('pin:')) return;
if (loggingReason.startsWith('search:')) return;
if (loggingReason === 'orbitznews') return;
const timelineItem = getTimelineItemWrapper(postElement);
timelineItem.setAttribute(hiddenAttribute, '');
if (precedingHiddenPosts(timelineItem) >= 10) {
timelineItem.setAttribute(unHiddenAttribute, '');
}
});
};
export const main = async function () {
onNewPosts.addListener(processPosts);
};
export const clean = async function () {
onNewPosts.removeListener(processPosts);
$(`.${excludeClass}`).removeClass(excludeClass);
$(`[${hiddenAttribute}]`).removeAttr(hiddenAttribute);
$(`[${unHiddenAttribute}]`).removeAttr(unHiddenAttribute);
};