-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoplay.js
172 lines (150 loc) · 9.3 KB
/
autoplay.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
(function () {
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var autoplayEnabledObserver = new MutationObserver(function(mutations) {
var isAutoplayEnabled = isAutoplayActive(mutations[0].target.outerHTML);
if (isAutoplayEnabled) {
chrome.storage.local.get('urlHistory', function (result) {
var urlHistory = new Set(result.urlHistory);
// wait until video recommendations become fully available to use the ones for the current video, not for the previous page
let recommendationsObserver = new MutationObserver(function (mutations) {
let nextAutoplayLink = mutations[0].target.href;
chrome.storage.local.get('whitelist', function (whitelist) {
if (urlHistory.has(nextAutoplayLink) && !whitelist.whitelist.includes(nextAutoplayLink)) {
var composeObserver = new MutationObserver(async function (mutations) {
var linkElements = document.getElementsByClassName("yt-simple-endpoint style-scope ytd-compact-video-renderer"); // hope that other videos are loaded when first video link update is seen by mutation observer
var videoLengths = document.querySelectorAll("#overlays > ytd-thumbnail-overlay-time-status-renderer > span");
await saveNextVideoCandidate(urlHistory, linkElements, videoLengths);
chrome.storage.local.get('clearSitesAtLoop', function (clearSites) {
chrome.storage.local.get('candidate', function (result) {
let candidate = result.candidate;
if (clearSites.clearSitesAtLoop) {
chrome.storage.local.get('blacklist', function (blacklist) {
chrome.storage.local.set({'urlHistory': blacklist.blacklist}, function() {
if (location.href === nextAutoplayLink) {
location.href = candidate;
}
});
});
} else {
if (location.href === nextAutoplayLink) {
location.href = candidate;
}
}
});
});
});
function addObserverAtVideoEnd() {
chrome.storage.local.get('videoEndInterval', function (result) {
let composeBox = document.querySelector("#movie_player > div.html5-endscreen.ytp-player-content.videowall-endscreen.ytp-show-tiles");
if (!composeBox) {
window.setTimeout(addObserverAtVideoEnd, result.videoEndInterval);
return;
}
let config = {attributes: true};
composeObserver.observe(composeBox, config);
});
}
addObserverAtVideoEnd();
}
});
// for background tabs, we do not check in advance, but when the suggested autoplay page is already loaded. Easier to compare that way because video suggestions might not be loaded
if (document.visibilityState === "hidden") {
chrome.storage.local.get('whitelist', function (whitelist) {
if (urlHistory.has(location.href) && !whitelist.whitelist.includes(location.href)) {
let linkElements = document.getElementsByClassName("yt-simple-endpoint style-scope ytd-compact-video-renderer");
let videoLengths = document.querySelectorAll("#overlays > ytd-thumbnail-overlay-time-status-renderer > span");
(async function() {
await saveNextVideoCandidate(urlHistory, linkElements, videoLengths);
})();
chrome.storage.local.get('candidate', function (result) {
if (result.candidate !== "https://www.youtube.com") {
location.href = result.candidate;
} else {
chrome.storage.local.get('backgroundRedirectEntry',
function(data) {
location.href = linkElements[data.backgroundRedirectEntry].href; // worst case: updates back and forth many times, but eventually, a different link will be chosen
}
);
}
});
}
});
}
});
function waitForVideoRecommendations() {
chrome.storage.local.get('videoRecommendationsInterval', function (result) {
var linkElements = document.getElementsByClassName("yt-simple-endpoint style-scope ytd-compact-video-renderer");
if (!linkElements) {
//The node we need does not exist yet.
window.setTimeout(waitForVideoRecommendations, result.videoRecommendationsInterval);
return;
}
var config = {attributes: true, childList: true};
recommendationsObserver.observe(linkElements[0], config);
});
}
waitForVideoRecommendations();
});
} else {
chrome.storage.local.get('toggleAutoplayClearsSites', function (result) {
if (result.toggleAutoplayClearsSites) {
// reset url history if user has turned autoplay off
chrome.storage.local.get('blacklist', function (blacklist) {
chrome.storage.local.set({'urlHistory': blacklist.blacklist});
});
}
});
}
});
function detectIfAutoplayEnabled() {
chrome.storage.local.get('toggleCheckInterval', function (result) {
var composeBox = document.querySelector("#toggle");
if (!composeBox) {
window.setTimeout(detectIfAutoplayEnabled, result.toggleCheckInterval);
return;
}
var config = {attributes: true};
autoplayEnabledObserver.observe(composeBox,config);
});
}
detectIfAutoplayEnabled();
})();
function isAutoplayActive(toggle) {
return toggle.includes('aria-pressed="true"');
}
function isPlaylistLink(linkToAnalyze) {
return linkToAnalyze.includes("&list=") || linkToAnalyze.includes("&start_radio=")
}
function getIntegerMinutes(timeString) {
// valid formats, which all need to be considered ([hh:m]m:ss): 1:23:34, 24:00:01, 32:31, 0:03
let timeContents = timeString.split(":");
let numberOfColons = timeContents.length - 1;
if (numberOfColons === 2) {
return parseInt(timeContents[0]) * 60 + parseInt(timeContents[1]);
} else if (numberOfColons === 1) {
return parseInt(timeContents[0]);
}
}
function saveNextVideoCandidate(urlHistory, linkElementsToChooseFrom, videoLengths) {
chrome.storage.local.get('minimumVideoLength', function (minVideoLength) {
chrome.storage.local.get('maximumVideoLength', function (videoLength) {
chrome.storage.local.get('includePlaylists', function (result) {
for (let i = 1; i < linkElementsToChooseFrom.length; i++) {
let linkToAnalyze = linkElementsToChooseFrom[i].href;
if (!urlHistory.has(linkToAnalyze) && (result.includePlaylists || !isPlaylistLink(linkToAnalyze))) {
// check video time
let colonTime = videoLengths[i].innerText;
let integerMinutes = getIntegerMinutes(colonTime);
if (integerMinutes < minVideoLength.minimumVideoLength) {
if (videoLength.maximumVideoLength === "0" || integerMinutes < videoLength.maximumVideoLength) {
chrome.storage.local.set({'candidate': linkToAnalyze});
return;
}
}
}
}
chrome.storage.local.set({'candidate': window.location.href}); // highly unlikely scenario that nothing suitable is found. In this case, looping is better than redirecting to www.youtube.com
});
});
});
}