forked from web-scrobbler/web-scrobbler
/
inject.js
428 lines (363 loc) · 11.2 KB
/
inject.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* All connectors are defined here, instead of manifest.
*
* Matching connector is injected to the page after document_end event.
*
* Do not include jQuery - it is included by default.
*
*
* Supported fields:
*
* label
* - label to be shown in options to enable/disable the connector
* - be careful with renaming, as connector disable state depends on the label
*
* matches
* - array of positive matches in format described in Chrome Ext. Dev. guide
* - connectors are processed in order and the first match is used; you can use
* this behaviour to emulate exclude matches
*
* js
* - array of paths of files to be executed
* - all executions happen on or after 'document_end'
*
* allFrames (optional)
* - boolean value representing InjectDetails.allFrames
* - FALSE by default
*
*/
var connectors = [
{
label: "Baidu Music",
matches: ["*://play.baidu.com/*"],
js: ["connectors/baidu.js"]
},
{
label: "YouTube",
matches: ["*://www.youtube.com/watch*", "*://www.youtube.com/user/*"],
js: ["connectors/youtube.js"]
},
{
label: "TTNET Müzik",
matches: ["*://www.ttnetmuzik.com.tr/*"],
js: ["connectors/ttnet.js"]
},
{
label: "Thesixtyone",
matches: ["*://www.thesixtyone.com/*"],
js: ["connectors/61.js"]
},
{
label: "Google Play Music",
matches: ["*://play.google.com/music/*"],
js: ["connectors/googlemusic.js"]
},
{
label: "MySpace",
matches: ["*://myspace.com/*"],
js: ["connectors/myspace.js"]
},
{
label: "Pitchfork",
matches: ["*://pitchfork.com/*", "*://www.pitchfork.com/*"],
js: ["connectors/pitchfork.js"]
},
{
label: "Fizy",
matches: ["*://fizy.com/*", "*://fizy.org/*"],
js: ["connectors/fizy.js"]
},
{
label: "Virgin Radio Turkiye",
matches: ["*://*.virginradioturkiye.com/*", "*://*.radioeksen.com/*"],
js: ["connectors/virginradiotr.js"]
},
{
label: "Ghostly Discovery",
matches: ["http://ghostly.com/discovery/play"],
js: ["connectors/ghostly.js"]
},
{
label: "Bandcamp",
matches: ["*://*.bandcamp.com/*"],
js: ["connectors/bandcamp.js"]
},
{
label: "Jango",
matches: ["*://www.jango.com/*"],
js: ["connectors/jango.js", "connectors/jango-dom-inject.js"]
},
{
label: "Pandora",
matches: ["*://www.pandora.com/*"],
js: ["connectors/pandora.js"]
},
{
label: "Deezer",
matches: ["*://www.deezer.com/*"],
js: ["connectors/deezer.js"]
},
{
label: "SoundCloud",
matches: ["*://soundcloud.com/*"],
js: ["connectors/soundcloud.js"]
},
{
label: "Amazon",
matches: ["*://www.amazon.com/gp/dmusic/mp3/player*", "*://www.amazon.de/gp/dmusic/mp3/player*", "*://www.amazon.es/gp/dmusic/mp3/player*", "*://www.amazon.co.uk/gp/dmusic/mp3/player*"],
js: ["connectors/amazon.js"]
},
{ // DEAD?
label: "Z-Music",
matches: ["*://z-music.org/*"],
js: ["connectors/zmusic.js"]
},
{
label: "VK",
matches: ["*://vk.com/*"],
js: ["connectors/vk.js"]
},
{
label: "Zvooq",
matches: ["*://zvooq.ru/*"],
js: ["connectors/zvooq.js"]
},
{
label: "Weborama",
matches: ["*://www.weborama.fm/*"],
js: ["connectors/weborama.js"],
allFrames: true
},
{
label: "22 Tracks",
matches: ["*://22tracks.com/*"],
js: ["connectors/22tracks.js"]
},
{
label: "Megalyrics",
matches: ["*://megalyrics.ru/*"],
js: ["connectors/megalyrics.js"],
allFrames: true
},
{
label: "iHeart",
matches: ["*://*.iheart.com/*"],
js: ["connectors/iheart.js"]
},
{
label: "Indie Shuffle",
matches: ["*://www.indieshuffle.com/*"],
js: ["connectors/indieshuffle.js"]
},
{
label: "Tuba.FM",
matches: ["*://fm.tuba.pl/*"],
js: ["connectors/tubafm.js"]
},
{
label: "Spotify",
matches: ["https://play.spotify.com/*"],
js: ["connectors/spotify.js"]
},
{
label: "Grooveshark",
matches: ["*://grooveshark.com/*"],
js: ["connectors/grooveshark.js"]
},
{
label: "Plug.dj",
matches: ["http://plug.dj/*"],
js: ["connectors/plugdj.js"]
},
{
label: "Turntable",
matches: ["*://turntable.fm/*"],
js: ["connectors/turntable.js"]
},
{
label: "Slacker (iframe)",
matches: ["*://www.slacker.com/webplayer/index_embed.vm"],
js: ["connectors/slacker.js"]
},
{
label: "Slacker (main page)",
matches: ["*://www.slacker.com/*"],
js: ["connectors/slacker2.js"]
},
{
label: "Daytrotter",
matches: ["*://www.daytrotter.com/*"],
js: ["connectors/daytrotter.js"]
},
{
label: "AOL Radio",
matches: ["*://aolradio.slacker.com/*"],
js: ["connectors/aolradio.js"]
},
{
label: "HillyDilly",
matches: ["*://www.hillydilly.com/*"],
js: ["connectors/hillydilly.js"]
},
{
label: "Xbox Music",
matches: ["*://music.xbox.com/*"],
js: ["connectors/xboxmusic.js"]
},
{
label: "8tracks",
matches: ["*://8tracks.com/*"],
js: ["connectors/8tracks.js"]
},
{
label: "Moje Polskie Radio",
matches: ["*://moje.polskieradio.pl/station/*"],
js: ["connectors/mojepolskieradio.js"]
},
{
label: "Nova Planet",
matches: ['*://www.novaplanet.com/radionova/player'],
js: ["connectors/novaplanet.js"]
},
{
label: "Radio+ Belgium",
matches: ["*://www.radioplus.be/*"],
js: ["connectors/radioplusbe.js"]
},
{
label: "Songza",
matches: ["*://songza.com/*"],
js: ["connectors/songza.js"]
},
{
label: "Douban Programme",
matches: ["http://music.douban.com/programme/*"],
js: ["connectors/douban-programme.js"]
},
{
label: "Focus@Will",
matches: ["*://www.focusatwill.com/*"],
js: ["connectors/focusatwill.js"]
},
{
label: "Le Tourne Disque",
matches: ["*://www.letournedisque.com/*"],
js: ["connectors/letournedisque.js"]
},
{
label: "Rdio",
matches: ["*://www.rdio.com/*"],
js: ["connectors/rdio.js"]
},
{
label: "Reddit Playlister",
matches: ["*://redditplayer.phoenixforgotten.com/*"],
js: ["connectors/redditplayer.js"]
},
{
label: "Sullen-Ural",
matches: ["*://sullen-ural.ru/*", "*://*.sullen-ural.ru/*"],
js: ["connectors/sullen-ural.js"]
}
];
/**
* Creates regex from single match pattern
*
* @author lacivert
* @param {String} input
* @returns RegExp
*/
function createPattern(input) {
if (typeof input !== 'string') return null;
var match_pattern = '^'
, regEscape = function (s) {
return s.replace(/[[^$.|?*+(){}\\]/g, '\\$&');
}
, result = /^(\*|https?|file|ftp|chrome-extension):\/\//.exec(input);
// Parse scheme
if (!result) return null;
input = input.substr(result[0].length);
match_pattern += result[1] === '*' ? 'https?://' : result[1] + '://';
// Parse host if scheme is not `file`
if (result[1] !== 'file') {
if (!(result = /^(?:\*|(\*\.)?([^\/*]+))/.exec(input))) return null;
input = input.substr(result[0].length);
if (result[0] === '*') { // host is '*'
match_pattern += '[^/]+';
} else {
if (result[1]) { // Subdomain wildcard exists
match_pattern += '(?:[^/]+\.)?';
}
// Append host (escape special regex characters)
match_pattern += regEscape(result[2]);// + '/';
}
}
// Add remainder (path)
match_pattern += input.split('*').map(regEscape).join('.*');
match_pattern += '$';
return new RegExp(match_pattern);
}
/**
* @param {String} label
* @returns boolean
*/
function isConnectorEnabled(label) {
var disabledArray = JSON.parse(localStorage.disabledConnectors);
return (disabledArray.indexOf(label) === -1);
}
/**
* Injects connectors to tabs upon page loading
*/
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// wait for the Loaded event
if (changeInfo.status !== 'complete')
return;
// run first available connector
var anyMatch = !connectors.every(function (connector) {
var matchOk = false;
connector.matches.forEach(function (match) {
matchOk = matchOk || createPattern(match).test(tab.url);
});
if (matchOk === true) {
console.log('connector ' + connector.label + ' matched for ' + tab.url);
setActionIcon(ACTION_SITE_RECOGNIZED, tabId);
if (!isConnectorEnabled(connector.label)) {
setActionIcon(ACTION_SITE_DISABLED, tabId);
return false; // break forEach
}
// Ping the content page to see if the script is already in place.
// In the future, connectors will have unified interface, so they will all support
// the 'ping' request. Right now only YouTube supports this, because it
// is the only site that uses ajax navigation via History API (which is quite hard to catch).
// Other connectors will work as usual.
//
// Sadly there is no way to silently check if the script has been already injected
// so we will see an error in the background console on load of every supported page
chrome.tabs.sendMessage(tabId, { type: 'ping' }, function (response) {
// if the message was sent to a non existing script or the script
// does not implement the 'ping' message, we get response==undefined;
if (!response) {
console.log('-- loaded for the first time, injecting the scripts');
// inject all scripts and jQuery, use slice to avoid mutating
var scripts = connector.js.slice(0);
scripts.unshift(JQUERY_PATH);
scripts.forEach(function (jsFile) {
var injectDetails = {
file: jsFile,
allFrames: connector.allFrames ? connector.allFrames : false
};
chrome.tabs.executeScript(tabId, injectDetails);
});
}
else {
console.log('-- subsequent ajax navigation, the scripts are already injected');
}
});
}
return !matchOk;
});
// hide page action if there is no match
if (!anyMatch) {
chrome.pageAction.hide(tabId);
}
});