-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
86 lines (73 loc) · 2.6 KB
/
scraper.js
File metadata and controls
86 lines (73 loc) · 2.6 KB
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
// Case where there are no more tracks :)
function Scraper(className){
console.log('starting to scrape track information. Will redirect on completion');
this.ids=[];
this.finished=0;
this.className=className||'youtube_link';
this.videConversationSite='http://www.vidtomp3.com/';
this.setupListeners();
this.loadAllTracks(this.saveAllUrls,this);
}
Scraper.prototype.setupListeners = function(){ // Scraper scope $ is different than 8track's $ (JQuery)
var self=this; // This code allows hijacking of the correct JQuery
// Chrome Extensions cannot have access to window scope
// Hence the event passing and script injection
document.addEventListener('idReturned',function(evt){
self.ids.push(evt.detail);
self.finished++;
if(self.finished===self.total)
self.saveUrls();
});
document.addEventListener('idNotReturned',function(evt){
self.finished++;
if(self.finished===self.total)
self.saveUrls();
});
Helper.injectScript(chrome.extension.getURL('getJQuery.js'), 'body');
}
Scraper.prototype.loadAllTracks = function(cb,context){
var moreBtn=$('.more')[0],
numTracks=parseInt($('.favs_count').text()),
cbCalled=false, //For Backup
moveOn=function(){
cbCalled=true;
$(window).off('DOMNodeInserted',listener);
cb.apply(context||this,[]);
},
listener=function(){
numTracks--;
if(numTracks===this.defaultSongsPerPage) //Does not reload already present songs
moveOn();
},
backup=function(){
if(!cbCalled)
moveOn();
},
href=moreBtn.href;
moreBtn.href=href.replace(this.defaultSongsPerPage,numTracks)
.replace('2','1'); // Make button load all tracks
$(window).on('DOMNodeInserted',listener);
moreBtn.click();
setTimeout(backup,8000);
}
Scraper.prototype.saveUrls = function(){
var data={},
self=this;
data[STORAGE_ID]=this.ids.join(' ');
chrome.storage.sync.set(data,function(){
alert('saved ' + self.ids.length + ' ids, now going to download');
self.goToDownload();
});
}
Scraper.prototype.goToDownload = function(){
window.location=this.videConversationSite;
}
Scraper.prototype.defaultSongsPerPage=20;
Scraper.prototype.youtubeURLBase="http://www.youtube.com/v/";
Scraper.prototype.saveAllUrls = function(cb,context){
this.elems=document.getElementsByClassName(this.className);
this.total=this.total=this.elems.length;
for (var i=0;i<this.elems.length;i++) {
this.elems[i].click();
}
}