-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.js
107 lines (83 loc) · 2.87 KB
/
Parser.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
function Parser() {};
// Initialize properties
Parser.prototype.initialize = function(controls, playing, tracks) {
this.controls = controls;
this.playing = playing;
this.tracks = tracks;
};
// Retrieve the current playstate.
Parser.prototype.playState = function() {
var classes = this.controls.play.getAttribute('class').split(' ');
for (var i = 0; i < classes.length; i++ ) {
if ( classes[i] == 'play' || classes[i] == 'pause' ) {
return classes[i];
}
}
// Default
return 'play';
};
// Retrieve the current favorite state.
Parser.prototype.favoriteState = function() {
var classes = this.controls.favorite.getAttribute('class').split(' ');
for (var i = 0; i < classes.length; i++) {
if ( classes[i] === 'fav-off' || classes[i] === 'fav-on' ) {
return classes[i];
}
}
// Default
return 'fav-off';
};
// Retrieve the current artist
Parser.prototype.artist = function() {
return this.playing.querySelectorAll('a')[0].innerText;
};
// Retrieve the current track title
Parser.prototype.title = function() {
return this.playing.querySelectorAll('a')[1].innerText;
};
// Retrieve the blog url for the current track
Parser.prototype.url = function() {
return this.playing.querySelectorAll('.read-post')[0].getAttribute('href');
};
// Retrieve the id for this track
Parser.prototype.trackId = function() {
return this.playing.querySelectorAll('a')[1].getAttribute('href').split('/')[2];
};
// Retrieve the active playlist from the DOM
Parser.prototype.playlist = function() {
var playlist = [];
for ( var i = 0; i < this.tracks.length; i++ ) {
var that = this.tracks[i].querySelector('.section-player');
var section = {};
// Sometimes there's a section without a button because Hype Machine likes
// to mess with me. That's cool though. We'll just catch the null value and
// not push it into our playlist.
try {
section.id = that.getAttribute('data-itemid');
section.artist = that.querySelector('.track_name .artist').innerText;
section.title = that.querySelector('.track_name .track').innerText;
section.button = that.querySelector('.tools .playdiv .play-ctrl').getAttribute('id');
section.state = this.listItemState( section.button );
playlist.push(section);
} catch(e) {}
}
return playlist;
};
Parser.prototype.shareUrl = function(network) {
var icon = 'icon-' + network;
return 'http:' + this.playing.getElementsByClassName(icon)[0].getAttribute('data-href');
};
Parser.prototype.listItemState = function(id) {
var classes = document.getElementById(id).getAttribute('class').split(' ');
for (var i = 0; i < classes.length; i++ ) {
if ( classes[i] == 'play' || classes[i] == 'pause' ) {
return classes[i];
}
}
// Default
return 'play';
};
// Update the tracks property
Parser.prototype.setTracks = function(tracks) {
this.tracks = tracks;
};