-
Notifications
You must be signed in to change notification settings - Fork 48
/
extension.js
160 lines (139 loc) · 6.56 KB
/
extension.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
// Extension main script
var stylesheetUrl = chrome.extension.getURL("hypestyles.css");
// This is all the JS that will be injected in the document body
var main = function() {
/**
* Return outerHTML for the first element in a jQuery object,
* or an empty string if the jQuery object is empty;
*/
jQuery.fn.outerHTML = function() {
return (this[0]) ? this[0].outerHTML : '';
};
/**
* Utility to wrap the different behaviors between W3C-compliant browsers
* and IE when adding event handlers.
*
* @param {Object} element Object on which to attach the event listener.
* @param {string} type A string representing the event type to listen for
* (e.g. load, click, etc.).
* @param {function()} callback The function that receives the notification.
*/
function addListener(element, type, callback) {
if (element.addEventListener) element.addEventListener(type, callback);
else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
/***
* The name _dlExtGATracker is chosen so as not to conflict with HypeMachine's possible
* use of Google Analytics object.
*/
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','_dlExtGATracker');
_dlExtGATracker('create', 'UA-44161401-1', 'hypem.com');
_dlExtGATracker('send', 'pageview');
//Random String generator.
var generator = function()
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = "";
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.charAt(rnum);
}
return randomstring;
};
//Here is where we can randomly generate the name of the styles to avoid them checking for specific names.
//Can add more here if they check additional names.
//ref:http://jonraasch.com/blog/javascript-style-node
var triArrowString = generator();
var css = document.createElement('style');
css.type = 'text/css';
var styles = '.'+triArrowString+'{ width: 0; height: 0; border-left: 9px solid transparent; border-right: 9px solid transparent; border-top: 10px solid #494949; }';
styles += '.arrow:hover .'+triArrowString+'{ border-top: 10px solid #0063DC; }';
//done this way for IE aparantly.
if (css.styleSheet) css.styleSheet.cssText = styles;
else css.appendChild(document.createTextNode(styles) );
document.getElementsByTagName("head")[0].appendChild(css);
// Adds a download button next to each track
var buttonScript = function() {
// Wait for the tracks script to load
var tracks = window.displayList['tracks'];
if (tracks === undefined || tracks.length < 1) {
setTimeout(buttonScript, 1);
}
else {
// Check if this particular page has been processed
// through a previous call
if (jQuery('.dl').length < tracks.length) {
jQuery('ul.tools').each(function(index, track) {
var song = tracks[index];
var title = song.song;
var artist = song.artist;
var id = song.id;
var key = song.key;
var hasDownloadButton = jQuery(track).data("hasDownloadButton");
if (typeof(hasDownloadButton) === 'undefined' || !hasDownloadButton){
jQuery.getJSON("/serve/source/"+ id + "/" + key, function(data) {
var download_url = data.url;
var download_button = document.createElement("a");
download_button.target = "_top";
download_button.href = download_url;
download_button.className = "DownloadSongButton";
download_button.download = artist + ' - ' + title + '.mp3';
download_button.innerHTML = '<table class="arrow"><tr><td rowspan="2"><input type="checkbox" /></td><td><div class="rect-arrow"></div></td></tr><tr><td class="'+triArrowString+ '"></td></tr></table>';
jQuery(track).prepend('<li class="dl"><table class="spacer"></table>' + jQuery(download_button)[0].outerHTML + '</li>');
});
jQuery(track).data("hasDownloadButton", true);
}
});//each
}
}
};//buttonscript
// Add a master link to download all songs on the page at once
jQuery("#submenu").append('<li id="DownloadAllSection">Select <span id="DownloadSelectAll">All</span>/<span id="DownloadSelectNone">None</span> · <span id="DownloadAllSongsButton">Download</span></li>');
jQuery("#DownloadAllSongsButton").click(function() {
jQuery(".DownloadSongButton").each(function() {
if (jQuery(this).find("input[type='checkbox']:checked").length) {
(function (btn) {
setTimeout(function() {
var a = document.createElement('a');
a.download = '';
a.href = btn;
a.dispatchEvent(new MouseEvent('click'));
}, 10);
})(jQuery(this).prop('href'));
}
});
});
jQuery("#DownloadSelectAll").click(function(e) {
e.preventDefault();
jQuery(".DownloadSongButton input[type='checkbox']").prop('checked', true);
});
jQuery("#DownloadSelectNone").click(function(e) {
e.preventDefault();
jQuery(".DownloadSongButton input[type='checkbox']").prop('checked', false);
});
jQuery('ul.tools').on('click', '.DownloadSongButton', function() {
console.log( "Downloading - " + jQuery(this)[0].download );
_dlExtGATracker('send', 'event', 'download', 'click', 'song-downloads', 1);
});
// Run it right away
buttonScript();
jQuery(document).ajaxComplete(function(event,request, settings){
buttonScript();
});
};
// Lets create the script objects
var injectedScript = document.createElement('script');
injectedScript.type = 'text/javascript';
injectedScript.text = '('+main+')("");';
(document.body || document.head).appendChild(injectedScript);
//Lets create the CSS object. This has to be done this way rather than the manifest.json
//because we want to override some of the CSS properties so they must be injected after.
var injectedCSS = document.createElement('link');
injectedCSS.type = 'text/css';
injectedCSS.rel = 'stylesheet';
injectedCSS.href = stylesheetUrl;
(document.body || document.head).appendChild(injectedCSS);