-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
72 lines (62 loc) · 2 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
//? Removes the infinite scroll from the YouTube recommendations and comments sections.
const SELECTORS = {
recommendationsElement: [
'ytd-item-section-renderer.style-scope.ytd-watch-next-secondary-results-renderer',
'#related'
],
commentsElement: [
'ytd-comments.style-scope.ytd-watch-flexy',
'#comments'
]
};
const STYLES = {
common: {
backgroundColor: '#272727',
overflowY: 'auto',
padding: '12px',
borderRadius: '12px',
marginBottom: '24px'
},
maxHeights: {
defaultRatio: 0.8,
recommendations: 1200,
comments: 600
}
};
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
function findElement(selectors) {
if (typeof selectors === 'string') return document.querySelector(selectors);
return selectors.reduce((element, selector) => element || document.querySelector(selector), null);
}
function updateSectionHeight(section, maxHeight) {
if (!section) return false;
const height = Math.min(window.innerHeight * STYLES.maxHeights.defaultRatio, maxHeight);
Object.assign(section.style, STYLES.common, { maxHeight: `${height}px` });
return true;
}
function updateAllSections() {
const sections = {
recommendations: findElement(SELECTORS.recommendationsElement),
comments: findElement(SELECTORS.commentsElement)
};
return updateSectionHeight(sections.recommendations, STYLES.maxHeights.recommendations) &&
updateSectionHeight(sections.comments, STYLES.maxHeights.comments);
}
const observer = new MutationObserver(() => {
if (updateAllSections()) {
observer.disconnect();
}
});
if (!updateAllSections()) {
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
}
window.addEventListener('resize', debounce(updateAllSections, 250));