forked from christianvuerings/idownload-hypem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idownload-hypem.user.js
162 lines (145 loc) · 5.26 KB
/
idownload-hypem.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
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
// ==UserScript==
// @name iDownload Hypem
// @version 0.2
// @namespace http://denbuzze.com/
// @description Download music from Hypem.com
// @match http://*.hypem.com/*
// @include http://*hypem.com/*
// ==/UserScript==
/*global document, soundManager*/
var exec = function(fn) {
var script = document.createElement('script');
script.setAttribute('id', 'idownload');
script.setAttribute('type', 'application/javascript');
script.textContent = '(' + fn + ')();';
document.body.appendChild(script);
document.body.removeChild(script);
};
exec(function() {
var localhostExists = false;
var passRequest = function(event, callback) {
var target = event.currentTarget;
if (target.readyState === 4 && target.status === 200) {
if (callback) {
callback(true);
}
} else if (target.readyState === 4) {
if (callback) {
callback(false);
}
}
};
var makeRequest = function(url, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(event) {
passRequest(event, callback);
};
httpRequest.open('GET', url);
httpRequest.send();
};
/**
* Add CSS to the document
* @param {String} css The CSS you want to add
*/
var addGlobalCSS = function(css) {
var sel = document.createElement('style');
sel.setAttribute('type','text/css');
sel.appendChild(document.createTextNode(css));
var hel = document.documentElement.firstChild;
while(hel && hel.nodeName!='HEAD') {
hel = hel.nextSibling;
}
if(hel && hel.nodeName=='HEAD') {
hel.appendChild(sel);
} else {
document.body.insertBefore(sel,document.body.firstChild);
}
return sel;
};
/**
* Add CSS for the download button
*/
var addDownloadButtonCSS = function() {
addGlobalCSS('' +
' #downloadButton { border-right: 1px solid #272727; color: #e0e0e0 !important; display: block; float: left; overflow: hidden; font-size: 16px; padding: 7px 7px 5px; }' +
' #downloadButton:hover { color: #277be1 !important; text-decoration: none; }' +
' #downloadButton:active { color: #ff0000 !important; }' +
' #player-nowplaying { max-width: 545px }'
);
};
/**
* Add the download button (only when it hasn't been added yet)
*/
var addDownloadButton = function() {
if (document.getElementById('downloadButton') === null) {
addDownloadButtonCSS();
var downloadButton = document.createElement('a');
downloadButton.setAttribute('id', 'downloadButton');
downloadButton.setAttribute('href', '#');
downloadButton.setAttribute('target', '_newtab');
var downloadText = document.createTextNode('▼');
downloadButton.appendChild(downloadText);
var playerFav = document.getElementById('playerFav');
var parent = playerFav.parentNode;
parent.insertBefore(downloadButton, playerFav);
}
};
var executeDownload = function(e) {
var downloadButton = document.getElementById('downloadButton');
if (localhostExists) {
e.preventDefault();
makeRequest(downloadButton.getAttribute('href'));
}
};
/**
* Set the download link
* @param {String} url The URL you want to use for the download link
*/
var setDownloadLink = function(sound) {
var url = sound.url;
var filename = '';
if (localhostExists) {
if (trackList && trackList[activeList] && trackList[activeList][currentTrack]){
var currentPlayingTrack = trackList[activeList][currentTrack];
var addquery = '';
if (url.indexOf('?') > -1) {
addquery = '&';
} else {
addquery = '?';
}
filename = addquery + 'filename=' + encodeURIComponent(currentPlayingTrack.artist + ' - ' + currentPlayingTrack.song);
}
url = 'http://localhost:5000/download/' + encodeURIComponent(url+filename);
}
var downloadButton = document.getElementById('downloadButton');
if (downloadButton.getAttribute('href') !== url) {
downloadButton.setAttribute('href', url);
downloadButton.removeEventListener('click', executeDownload);
downloadButton.addEventListener('click', executeDownload);
}
};
var checkPlaying = function() {
var currentSound = soundManager.soundIDs[0];
if (currentSound) {
addDownloadButton();
setDownloadLink(soundManager.sounds[currentSound]);
}
};
var initPolling = function() {
soundManager.onready(function() {
setInterval(checkPlaying, 500);
});
};
var checkLocalhostExists = function() {
makeRequest('http://localhost:5000/exists', function(data){
localhostExists = data;
initPolling();
});
};
var init = function() {
if (soundManager) {
checkLocalhostExists();
}
};
init();
});