-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudacity_pause_youtube_frame_content.js
116 lines (102 loc) · 3.09 KB
/
udacity_pause_youtube_frame_content.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
if(document.referrer.startsWith("https://classroom.udacity.com")){
var safeGetYoutubePlayer = `
if(window.YT === undefined){
YT = {PlayerState : {
BUFFERING : 3,
CUED : 5,
ENDED : 0,
PAUSED : 2,
PLAYING : 1,
UNSTARTED : -1
}}
}
var errMsg = null;
var player = null;
if(yt === null){
errMsg = "UdacityPause error: yt is null, something went wrong."
}else{
/*
var videoFrame = document.querySelector("div[class^=youtube-player] iframe");
if(videoFrame === null){
errMsg = "UdacityPause error: videoFrame is null, something went wrong."
}else{
//player = YT.get(videoFrame.id);
if(player === null){
errMsg = "UdacityPause error: failed to YT.get player.";
}
}
*/
/*
player = yt.player.getPlayerByElement("player");
if(player === null){
errMsg = "UdacityPause error: yt.player.getPlayerByElement returned null, something went wrong."
}
*/
if(!yt || !yt.player || !yt.player.utils ||
!yt.player.utils.VideoTagPool.instance ||
!yt.player.utils.VideoTagPool.instance.A ||
!yt.player.utils.VideoTagPool.instance.A.length){
errMsg = "Player not found";
}else{
player = yt.player.utils.VideoTagPool.instance.A[0];
}
}
if(errMsg !== null){
alert(errMsg);
throw errMsg;
}
`
// var startScript = `
// ${safeGetYoutubePlayer}
// player.addEventListener("onStateChange", function(state){
// if(state === 0){
// // setTimeout(function(){
// // var nextButton = document.querySelector("a[class^=_auto-advance-overlay--button]");
// // if(nextButton){
// // nextButton.click();
// // }
// // }, 1000);
// // }
// });
// `;
// runRemoteScript(startScript);
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if(request.action === "udacity_pause_pause"){
var scriptContent = `
${safeGetYoutubePlayer}
if(player.paused){
player.play();
}else{
player.pause();
}
`;
runRemoteScript(scriptContent);
}else if(request.action === "udacity_pause_forward"){
seekUdacity(request.seekDuration);
}else if(request.action === "udacity_pause_back"){
seekUdacity(-request.seekDuration);
}
});
function seekUdacity(seconds){
var scriptContent = `
${safeGetYoutubePlayer}
var time = player.currentTime;
player.currentTime = time + ${seconds};
`;
runRemoteScript(scriptContent);
}
function runRemoteScript(scriptContent){
var randomTagId = "_" + guidGenerator();
var script = document.createElement('script');
script.id = randomTagId;
script.appendChild(document.createTextNode(scriptContent));
(document.body || document.head || document.documentElement).appendChild(script);
script.parentElement.removeChild(script);
}
function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
}