-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.user.js
More file actions
235 lines (210 loc) · 8.07 KB
/
sync.user.js
File metadata and controls
235 lines (210 loc) · 8.07 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// ==UserScript==
// @name SyncPlayers
// @version 0.5
// @description Sync playback between YouTube video and mpv
// @match https://www.youtube.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @run-at document-idle
// @noframes
// ==/UserScript==
(function () {
'use strict';
const PORT = 8001;
const protocol = "ws"
let syncButton;
let running = false;
function addButton() {
// Check URL
if (!window.location.pathname.startsWith("/watch")) { return }
syncButton = document.querySelector('#syncButton');
if (syncButton) { return };
console.log("addButton");
// Create Button, append it to player controls
syncButton = document.createElement('button');
syncButton.id = 'syncButton';
syncButton.style.background = 'none';
syncButton.style.border = 'none';
syncButton.style.cursor = 'pointer';
syncButton.style.marginLeft = '10px';
syncButton.style.marginRight = '10px';
syncButton.style.textShadow = '0px 0px 4px black';
syncButton.innerText = 'Sync';
syncButton.style.color = '#fff';
syncButton.style.fontWeight = 'bold';
syncButton.onclick = startSync;
const controls = document.querySelector('.ytp-chrome-controls');
controls.appendChild(syncButton);
};
document.addEventListener('yt-navigate-finish', () => {
// Run the function every time you navigate to a new page
addButton();
});
document.addEventListener('yt-navigate-start', () => {
// Stop the script if it is still running while a new page is being loaded
if (running) {
// console.log("navigate-start");
stopSync();
};
});
window.addEventListener('beforeunload', function (e) {
// Stop the script if it is still running while the window is being closed
if (running) {
stopSync();
};
});
// Declare variables so they can be accessed outside startSync function
let gTime;
let websocket;
let player;
let mainVideo;
function sendState() {
const msg = {
type: "set",
property: "pause",
//value: data
value: player.getPlayerState()
};
websocket.send(JSON.stringify(msg));
};
function sendSpeed() {
const msg = {
type: "set",
property: "speed",
value: player.getPlaybackRate()
};
websocket.send(JSON.stringify(msg));
};
// Send current playback time
function getTime() {
const currentPlaybackTime = player.getCurrentTime();
const currentTimeSec = Date.now() / 1000;
const msg = {
type: "playbackSync",
property: "playback-time",
value: currentPlaybackTime,
time: currentTimeSec
};
websocket.send(JSON.stringify(msg));
//console.log(currentTime);
};
function stopSync() {
running = false;
mainVideo.removeEventListener("timeupdate", getTime);
player.removeEventListener("onStateChange", sendState);
player.removeEventListener("onPlaybackRateChange", sendSpeed);
const msg = {
type: "notice",
value: "clientStop"
};
websocket.send(JSON.stringify(msg));
websocket.close(1000);
};
function startSync() {
running = true;
syncButton.innerText = "UnSync";
syncButton.onclick = stopSync;
websocket = new WebSocket(`${protocol}://localhost:${PORT}/`);
player = document.getElementById('movie_player');
mainVideo = document.getElementsByClassName('html5-main-video')[0];
// Handle messages received from server
websocket.addEventListener("message", ({ data }) => {
const msg = JSON.parse(data);
//console.log(msg);
if (msg.type == "set") {
switch (msg.property) {
case "pause":
player.removeEventListener("onStateChange", sendState);
if (msg.value) {
player.pauseVideo();
} else {
let Ystate = player.getPlayerState();
let i = 0
while (Ystate != 1 && i < 10) {
player.playVideo();
// mainVideo.play();
Ystate = player.getPlayerState();
i++
// console.log(Ystate);
}
// console.log(Ystate);
};
player.addEventListener("onStateChange", sendState);
break;
case "playback-time":
player.seekTo(msg.value, true);
break;
case "speed":
player.setPlaybackRate(msg.value);
break;
case "speedOffset":
mainVideo.playbackRate = player.getPlaybackRate() + msg.value;
break;
case "removeListener":
if (msg.value == "state") {
player.removeEventListener("onStateChange", sendState);
} else if (msg.value == "playback-time") {
mainVideo.removeEventListener("timeupdate", getTime);
};
break;
case "addListener":
if (msg.value == "state") {
player.addEventListener("onStateChange", sendState);
} else if (msg.value == "playback-time") {
mainVideo.addEventListener("timeupdate", getTime);
};
break;
};
} else if (msg.type == "get") {
let answer = {};
switch (msg.property) {
case "playback-time":
answer = {
type: "get-property",
property: "playback-time",
value: player.getCurrentTime()
};
break;
case "url":
answer = {
type: "get-property",
property: "url",
value: window.location.href
};
break;
};
websocket.send(JSON.stringify(answer));
//console.log("answering:");
//console.log(answer);
} else if (msg.type == "notice") {
switch (msg.value) {
case "stopping server":
running = false;
mainVideo.removeEventListener("timeupdate", getTime);
player.removeEventListener("onStateChange", sendState);
player.removeEventListener("onPlaybackRateChange", sendSpeed);
websocket.close(1000);
break;
};
};
});
player.addEventListener("onStateChange", sendState);
player.addEventListener("onPlaybackRateChange", sendSpeed);
document.addEventListener("focus", function () {
const msg = {
type: "notice",
value: "focus"
};
websocket.send(JSON.stringify(msg));
console.log("Page in focus")
})
websocket.addEventListener("error", function (e) {
console.log(e);
stopSync();
});
websocket.onclose = () => {
syncButton.innerText = "Sync";
syncButton.onclick = startSync;
};
};
})();