-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideo-Auto-Downloader.user.js
93 lines (82 loc) · 2.95 KB
/
Video-Auto-Downloader.user.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
// ==UserScript==
// @name Video Auto-downloader
// @namespace png-vid-archive
// @description Downloads every viewed online video
// @include http*://*youtube.com/*
// @include http*://vimeo.com/*
// @grant none
// @version 1.33.7.2
// ==/UserScript==
function inject(func) {
// Injects a script into the content page.
// Needed to escape Chrome sandbox
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.appendChild(document.createTextNode("(" + func + ")();"));
document.body.appendChild(script);
document.body.removeChild(script);
}
function YoutubeAutoDownloader() {
// This fucntion gets injected into the page and executed
var YOUTUBE_ARCHIVER_HOST = "locker.phinugamma.org";
var currentUrl;
function sendPage(uri) {
// Fuck your same-origin policy :smug:
var image = new Image();
image.src = '//' + YOUTUBE_ARCHIVER_HOST + '/dlvid?url=' + escape(uri);
console.log("[Auto-download] Sent request for " + uri);
}
function checkPageNavigate() {
// Poll for page changes. Needed since youtube will dynamically load pages now.
locationStr = document.location.toString();
if (locationStr != currentUrl) {
console.log("[Auto-download] Sending Youtube page");
currentUrl = locationStr;
sendPage(currentUrl);
}
setTimeout(checkPageNavigate, 2000);
}
function getEmbeddedPlayer() {
return document.getElementById("player").childNodes[0];
}
function checkEmbedReadiness() {
if (window.yt && getEmbeddedPlayer()) {
console.log("[Auto-download] Setting up embedded handler")
setupEmbeddedEventHandler();
} else {
console.log("[Auto-download] Embedded player not ready. Trying again...")
setTimeout(checkEmbedReadiness, 1000);
}
}
function getURLFromPlayer(player) {
// Returns a clean URL from the player
// Need to remove timestamp that embedded player tries to add
return player.getVideoUrl().replace(/t=[0-9]*&/, "");
}
function setupEmbeddedEventHandler() {
// Hook into embedded player event
var player = getEmbeddedPlayer();
player.addEventListener('onStateChange', function(state) {
if (state == 1) { // Playing
var playingVid = getURLFromPlayer(player);
if (playingVid != currentUrl) {
currentUrl = playingVid;
sendPage(currentUrl);
}
}
});
}
if (window.top == window.self) {
// This is a youtube page. Start the checker to look for page changes, and
// send archive commands as needed.
console.log("[Auto-download] Detected video page");
checkPageNavigate();
} else if (window.location.toString().search('/embed/') > 0) {
// This is a youtube embed. Need to hook up event handler
console.log("[Auto-download] Detected embed");
checkEmbedReadiness();
}
}
// o shit waddup
console.log("[Auto-download] Youtube Auto-dowloader loading");
inject(YoutubeAutoDownloader);