This repository has been archived by the owner on Aug 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanager.js
91 lines (79 loc) · 2.94 KB
/
manager.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
// This file contains the Manager class.
// -----------------------------------------------------------------------------
/**
* Manages a collection of videos by applying settings and polling the DOM.
*/
class Manager {
/**
* Constructs a new Manager object.
*/
constructor() {
this.album = new Album();
this.settings = new Settings();
this.ready = true;
// Call request() when the DOM is mutated.
const observer = new MutationObserver(() => this.request());
observer.observe(document.getRootNode(), {childList: true, subtree: true});
}
/**
* Updates the visibility of the video collection based on Settings.hidden()
* and Settings.ignored().
*/
display() {
const hidden = !this.settings.ignored() && this.settings.hidden();
Logger.debug(`Manager.display(): ${hidden ? "hiding" : "showing"} videos.`);
const videos = this.album.getVideos();
videos.forEach(video => hidden ? video.hide() : video.show());
}
/**
* Submits a request to poll the DOM. Poll requests that are issued in rapid
* succession are batched together into a single poll request.
*/
request() {
const callback = () => {
Logger.debug("Manager.request(): polling the DOM.");
this.poll();
this.ready = true;
};
if (this.ready) {
this.ready = false;
setTimeout(callback, BATCH_TIME_MILLISECONDS);
}
}
/**
* Updates the video collection by extracting videos from the DOM.
*
* Note: This function should not be called directly; use request() instead.
*/
poll() {
const album = this.extract();
this.album.merge(album);
this.display();
}
/**
* Returns the videos on the current page which pass the enabled filters and
* have a watch progress that matches or exceeds the view threshold.
*
* @returns {Album} - Collection of viewed videos.
*/
extract() {
const extractor = new Extractor();
extractor.insert(extractExploreVideos);
extractor.insert(extractGridVideos);
extractor.insert(extractHistoryVideos);
extractor.insert(extractHomeVideos);
const filters = {
[HIDE_PLAYLISTS_CHECKBOX_STORAGE_KEY]: extractPlaylistVideos,
[HIDE_RECOMMENDATIONS_CHECKBOX_STORAGE_KEY]: extractRecommendedVideos,
[HIDE_SEARCHES_CHECKBOX_STORAGE_KEY]: extractSearchVideos
};
for (const [key, subextractor] of Object.entries(filters)) {
if (this.settings.state[key] !== false) {
extractor.insert(subextractor);
}
}
const threshold = this.settings.threshold();
const videos = extractor.extract(document, threshold);
return new Album(videos);
}
}