This repository has been archived by the owner on Sep 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
153 lines (145 loc) · 6.17 KB
/
player.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
var comments = {};
var spots = [];
var options = {};
chrome.storage.sync.get(null, function (data) {
options = data;
});
function showComment(comment) {
var container = document.getElementById("yttc-container");
for (var spot = 0; spots.indexOf(spot) > -1; spot++);
if (110 * spot + 110 <= container.offsetHeight) {
spots.push(spot);
var element = document.createElement("div");
element.className = "yttc-comment";
element.innerHTML = ''
+ '<img class="profile-image" src="' + comment.authorProfileImageUrl + '">'
+ '<p class="profile-name">' + comment.authorDisplayName + '</p>'
+ '<p class="content">' + comment.textView + '</p>';
element.style.top = (110 * spot) + "px";
container.appendChild(element);
element.querySelector(".profile-image").addEventListener("load", function () {
element.style.animationPlayState = "running";
setTimeout(function () {
spots.splice(spots.indexOf(spot), 1);
element.remove();
}, 10000);
});
}
}
function waitUntilAvailable(parent, target, callback) {
if (document.querySelector(target) != null) {
callback();
} else {
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) {
if (node.matches(target)) {
observer.disconnect();
callback();
}
});
});
});
observer.observe(document.querySelector(parent), {
childList: true
});
}
}
waitUntilAvailable("#player-api", "#movie_player", function () {
var container = document.createElement("div");
container.id = "yttc-container";
document.querySelector("#movie_player").appendChild(container);
var style = document.createElement("style");
document.head.appendChild(style);
if (options.fullscreen_only) {
style.sheet.insertRule(""
+ "#movie_player:not(.ytp-fullscreen) #yttc-container {"
+ "display: none;"
+ "}"
);
}
var previousTime = 0;
document.querySelector("#player video").addEventListener("timeupdate", function () {
var currentTime = Math.round(this.currentTime);
if (currentTime != previousTime && comments.hasOwnProperty(currentTime)) {
comments[currentTime].forEach(function (comment) {
showComment(comment);
});
}
previousTime = currentTime;
});
var previousVideo = null;
document.querySelector("#player video").addEventListener("canplay", function () {
var videoID = location.search.match(/v=([^&]+)(&|$)/)[1];
if (videoID != previousVideo) {
previousVideo = videoID;
comments = {};
chrome.runtime.sendMessage({action: "FETCH_COMMENTS", videoID: videoID}, function (items) {
var noLists = options.hasOwnProperty("no_lists") ? options.no_lists : true;
var noClockTimes = options.hasOwnProperty("no_clocktimes") ? options.no_clocktimes : true;
var commentTexts = [];
for (var id in items) {
var comment = items[id];
comment.textView = /^(?:\d[\W\d]+)?([\w\W]*)$/.exec(comment.textOriginal)[1];
if (/([\d:]+)\s*-\s*([\d:]+)/.test(comment.textOriginal) && comment.timestamps.length == 2) {
// Time range
// Only begining
comment.timestamps = [comment.timestamps[0]];
} else if (noLists && items[id].timestamps.length >= 2) {
// List
delete items[id];
continue;
}
if (noClockTimes) {
if (/\d{1,2}:?\d{1,2}\s?(?:am|pm)/.test(items[id].textOriginal)) {
delete items[id];
continue;
}
}
if (/^\s*$/.test(comment.textView)) {
// Empty
delete items[id];
continue;
}
if (commentTexts.indexOf(comment.textOriginal) > -1) {
// Duplicate
delete items[id];
continue;
}
commentTexts.push(comment.textOriginal);
comment.timestamps.forEach(function (timestamp) {
if (typeof comments[timestamp] == "undefined") comments[timestamp] = [];
comments[timestamp].push(comment);
});
}
var limit = options.hasOwnProperty("limit") ? options.limit : 5;
var start = parseInt(Object.keys(comments)[0]);
var end = parseInt(Object.keys(comments).pop());
var duration = 10;
while (start <= end) {
var queue = [];
for (var time = start; time <= start + duration; time++) {
if (comments.hasOwnProperty(time)) {
comments[time].forEach(function (comment, i) {
queue.push({
likeCount: comment.likeCount,
timestamp: time,
index: i
});
});
}
}
if (queue.length > limit) {
queue.sort(function (a, b) {
return a.likeCount - b.likeCount;
});
queue.slice(0, queue.length - limit).forEach(function (ref) {
comments[ref.timestamp].splice(ref.index, 1);
});
}
start++;
}
});
}
});
});