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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(id);
//tell background.js about this
chrome.runtime.sendMessage({
message: "ytvideoid",
videoID: id
});
}
//was sponsor data found when doing SponsorsLookup
var sponsorDataFound = false;
//the actual sponsorTimes if loaded
var sponsorTimes = undefined;
//the video
var v;
//the last time looked at (used to see if this time is in the interval)
var lastTime;
//the last time in the video a sponsor was skipped
//used for the go back button
var lastSponsorTimeSkipped = null;
//if the notice should not be shown
//happens when the user click's the "Don't show notice again" button
var dontShowNotice = false;
chrome.storage.local.get(["dontShowNoticeAgain"], function(result) {
let dontShowNoticeAgain = result.dontShowNoticeAgain;
if (dontShowNoticeAgain != undefined) {
dontShowNotice = dontShowNoticeAgain;
}
});
chrome.runtime.onMessage.addListener( // Detect URL Changes
function(request, sender, sendResponse) {
//message from background script
if (request.message == "ytvideoid") {
//reset sponsor data found check
sponsorDataFound = false;
sponsorsLookup(request.id);
}
//messages from popup script
if (request.message == "sponsorStart") {
sponsorMessageStarted();
}
if (request.message == "isInfoFound") {
//send the sponsor times along with if it's found
sendResponse({
found: sponsorDataFound,
sponsorTimes: sponsorTimes
})
}
if (request.message == "getVideoID") {
sendResponse({
videoID: getYouTubeVideoID(document.URL)
})
}
if (request.message == "showNoticeAgain") {
dontShowNotice = false;
}
});
function sponsorsLookup(id) {
v = document.querySelector('video') // Youtube video player
let xmlhttp = new XMLHttpRequest();
//check database for sponsor times
xmlhttp.open('GET', serverAddresss + "/api/getVideoSponsorTimes?videoID=" + id, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
sponsorDataFound = true;
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
v.ontimeupdate = function () {
sponsorCheck(sponsorTimes);
};
} else {
sponsorDataFound = false;
}
};
xmlhttp.send(null);
}
function sponsorCheck(sponsorTimes) { // Video skipping
//see if any sponsor start time was just passed
sponsorTimes.forEach(function (sponsorTime, index) { // Foreach Sponsor in video
//the sponsor time is in between these times, skip it
//if the time difference is more than 1 second, than the there was probably a skip in time,
// and it's not due to playback
if (Math.abs(v.currentTime - lastTime) < 1 && sponsorTime[0] >= lastTime && sponsorTime[0] <= v.currentTime) {
//skip it
v.currentTime = sponsorTime[1];
lastSponsorTimeSkipped = sponsorTime[0];
//send out the message saying that a sponsor message was skipped
openSkipNotice();
setTimeout(closeSkipNotice, 2500);
}
lastTime = v.currentTime;
});
}
function goBackToPreviousTime() {
if (lastSponsorTimeSkipped != null) {
//add a tiny bit of time to make sure it is not skipped again
v.currentTime = lastSponsorTimeSkipped + 0.001;
closeSkipNotice();
}
}
//Opens the notice that tells the user that a sponsor was just skipped
function openSkipNotice(){
if (dontShowNotice) {
//don't show, return
return;
}
var noticeElement = document.createElement("div");
noticeElement.id = "sponsorSkipNotice";
noticeElement.className = "sponsorSkipObject";
var logoElement = document.createElement("img");
logoElement.id = "sponsorSkipLogo";
logoElement.src = chrome.extension.getURL("icons/LogoSponsorBlocker256px.png");
var noticeMessage = document.createElement("p");
noticeMessage.id = "sponsorSkipMessage";
noticeMessage.className = "sponsorSkipObject";
noticeMessage.innerText = "Hey, you just skipped a sponsor!";
var buttonContainer = document.createElement("div");
buttonContainer.setAttribute("align", "center");
var goBackButton = document.createElement("button");
goBackButton.innerText = "Go back";
goBackButton.className = "sponsorSkipObject";
goBackButton.className = "sponsorSkipButton";
goBackButton.addEventListener("click", goBackToPreviousTime);
var hideButton = document.createElement("button");
hideButton.innerText = "Hide";
hideButton.className = "sponsorSkipObject";
hideButton.className = "sponsorSkipButton";
hideButton.addEventListener("click", closeSkipNotice);
var dontShowAgainButton = document.createElement("button");
dontShowAgainButton.innerText = "Don't Show This Again";
dontShowAgainButton.className = "sponsorSkipObject";
dontShowAgainButton.className = "sponsorSkipDontShowButton";
dontShowAgainButton.addEventListener("click", dontShowNoticeAgain);
buttonContainer.appendChild(goBackButton);
buttonContainer.appendChild(hideButton);
buttonContainer.appendChild(document.createElement("br"));
buttonContainer.appendChild(document.createElement("br"));
buttonContainer.appendChild(dontShowAgainButton);
noticeElement.appendChild(logoElement);
noticeElement.appendChild(noticeMessage);
noticeElement.appendChild(buttonContainer);
var referenceNode = document.getElementById("info");
if (referenceNode == null) {
//old YouTube
referenceNode = document.getElementById("watch-header");
}
referenceNode.prepend(noticeElement);
}
//Closes the notice that tells the user that a sponsor was just skipped
function closeSkipNotice(){
let notice = document.getElementById("sponsorSkipNotice");
if (notice != null) {
notice.remove();
}
}
function dontShowNoticeAgain() {
chrome.storage.local.set({"dontShowNoticeAgain": true});
dontShowNotice = true;
closeSkipNotice();
}
function sponsorMessageStarted() {
let v = document.querySelector('video');
console.log(v.currentTime)
//send back current time
chrome.runtime.sendMessage({
message: "time",
time: v.currentTime
});
}
function getYouTubeVideoID(url) { // Returns with video id else returns false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
|