forked from samuraiexx/roll_together
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_script.js
178 lines (153 loc) · 5.19 KB
/
content_script.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
173
174
175
176
177
178
/* content_script.js manipulates the HTML5 video-player and captures actions related to it. */
function checkService() {
if (window.location.href.includes("crunchyroll.com")) {
return Site.CRUNCHYROLL;
}
else if (window.location.href.includes("funimation.com")) {
return Site.FUNIMATION;
}
else if (window.location.href.includes("wakanim.tv")) {
return Site.WAKANIM;
}
}
const service = checkService();
const ignoreNext = {};
let player = null;
let lastFrameProgress = null;
function getState(stateName) {
return player[stateName];
}
function getStates() {
const [paused, currentProgress] = [
getState("paused"),
getState("currentTime"),
];
lastFrameProgress = lastFrameProgress || currentProgress;
const timeJump = Math.abs(currentProgress - lastFrameProgress) > LIMIT_DELTA_TIME;
const state = paused ? States.PAUSED : States.PLAYING;
lastFrameProgress = currentProgress;
return { state, currentProgress, timeJump };
}
/* Add event listener from pagescript.js for accessing page script objects,
* such as the brightcove player api on Funimation */
var scriptElement = document.createElement('script');
scriptElement.src = chrome.extension.getURL('pagescript.js');
(document.head || document.documentElement).appendChild(scriptElement);
scriptElement.parentNode.removeChild(scriptElement);
/* Handles local actions and sends messages to let background.js propagate
* actions to other users */
const handleLocalAction = action => () => {
if (ignoreNext[action] === true) {
ignoreNext[action] = false;
return;
}
const { state, currentProgress, timeJump } = getStates();
const type = WebpageMessageTypes.LOCAL_UPDATE;
log('Local Action', action, { type, state, currentProgress });
switch (action) {
case Actions.PLAY:
case Actions.PAUSE:
chrome.runtime.sendMessage({ type, state, currentProgress });
break;
case Actions.TIMEUPDATE:
timeJump && chrome.runtime.sendMessage({ type, state, currentProgress });
break;
}
}
/* Used by handleRemoteUpdate to trigger video player actions from other users. */
function triggerAction(action, progress) {
ignoreNext[action] = true;
switch (action) {
case Actions.PAUSE:
if (service === Site.FUNIMATION) {
window.postMessage({
crosswatch_action: action,
currentTime: progress
}, "*");
} else {
player.pause();
player.currentTime = progress;
}
break;
case Actions.PLAY:
if (service === Site.FUNIMATION) {
window.postMessage({
crosswatch_action: action
}, "*");
} else {
player.play();
}
break;
case Actions.TIMEUPDATE:
if (service === Site.FUNIMATION) {
window.postMessage({
crosswatch_action: action,
currentTime: progress
}, "*");
} else {
player.currentTime = progress;
}
break;
default:
ignoreNext[action] = false;
}
}
function sendRoomConnectionMessage(roomId) {
const { state, currentProgress } = getStates();
const type = WebpageMessageTypes.ROOM_CONNECTION;
chrome.runtime.sendMessage(
{ state, currentProgress, type, roomId }
);
}
/* Used by handleBackgroundMessage to handle REMOTE_UPDATE */
function handleRemoteUpdate({ roomState, roomProgress }) {
log('Handling Remote Update', { roomState, roomProgress });
const { state, currentProgress } = getStates();
if (state !== roomState) {
if (roomState === States.PAUSED) triggerAction(Actions.PAUSE, roomProgress);
if (roomState === States.PLAYING) triggerAction(Actions.PLAY, roomProgress);
}
if (Math.abs(roomProgress - currentProgress) > LIMIT_DELTA_TIME) {
triggerAction(Actions.TIMEUPDATE, roomProgress);
}
}
function handleBackgroundMessage(args) {
log("Received message from Background", args);
const type = args.type;
const roomId = args.roomId;
switch (type) {
case BackgroundMessageTypes.ROOM_CONNECTION:
/* Handle connection to create a new room. */
sendRoomConnectionMessage(roomId);
break;
case BackgroundMessageTypes.REMOTE_UPDATE:
handleRemoteUpdate(args);
break;
default:
throw "Invalid BackgroundMessageType: " + type;
}
}
/* Main function of content script. Checks for the appropriate <video>
* element on the page. "player0" is the id given to the <video> tag within
* the iframe of CrunchyRoll's vilos-player.
* An local action listener (handleLocalAction) is set up to listen for
* actions performed on the video by the local user, and will use the
* chrome.runtime.onMessage listener in background.js to propagate
* LOCAL_UPDATE actions to other users.
* The runtime.onMessage listener handleBackgroundMessage is used to handle
* messages from backround.js */
function runContentScript() {
const videotags = document.getElementsByTagName("video");
player = videotags[0]
if (!player) {
setTimeout(runContentScript, 500);
return;
}
for (action in Actions) {
player.addEventListener(Actions[action], handleLocalAction(Actions[action]));
}
chrome.runtime.onMessage.addListener(handleBackgroundMessage);
/* Send message to runtime.onMessage listener in backend.js to connect to room. */
chrome.runtime.sendMessage({ type: WebpageMessageTypes.CONNECTION });
}
runContentScript();