forked from jyntran/nhaccuatui-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentscript.js
151 lines (131 loc) · 3.76 KB
/
contentscript.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
/* contentscript.js */
(function() {
$(document).ready(function(){
var totalTimeStr, totalTime,
currentTimeStr, currentTime,
metadata,
isScrobbled = false;
function onSongPage() {
return window.location.href.indexOf('nhaccuatui.com/bai-hat/') > -1;
}
function onPlaylistPage() {
return window.location.href.indexOf('nhaccuatui.com/playlist/') > -1;
}
function onVideoPage() {
return window.location.href.indexOf('nhaccuatui.com/video/') > -1;
}
function onUserPage() {
return window.location.href.indexOf('nhaccuatui.com/user/') > -1;
}
function getCurrentTime() {
return $('.utCurrentTime')
}
function getTotalTime() {
return $('.utTotalTime')
}
function getTrackName() {
if (onSongPage() || onVideoPage()) {
const elem = document.getElementsByClassName('name_title')[0];
return elem.children[0].innerText;
} else if (onPlaylistPage()) {
const elem = document.getElementById('nameSingerflashPlayer');
return elem.children[0].children[0].innerText
} else if (onUserPage()) {
return $('#list-item-music .cur .name_song').text()
}
}
function getArtistName() {
if (onSongPage() || onVideoPage()) {
const elem = document.getElementsByClassName('name_singer')[0];
return elem.innerText;
} else if (onPlaylistPage()) {
const elem = document.getElementById('nameSingerflashPlayer');
return elem.children[1].children[0].innerText;
} else if (onUserPage()) {
return $('#list-item-music .cur .name_singer').last().text();
}
}
function getMetadata() {
var trackName = getTrackName()
var artistName = getArtistName()
var metadata = {
track: trackName,
artist: artistName
};
chrome.runtime.sendMessage({
name: "metadata",
data: metadata
}, function(resp) {
//console.log('getMetadata', resp);
});
return metadata;
}
function scrobbleTrack(data) {
var obj = {
track: data.track,
artist: data.artist,
timestamp: new Date().getTime() / 1000
};
chrome.runtime.sendMessage({
name: 'scrobble',
data: obj
}, function(resp) {
if (!resp.data.error) {
isScrobbled = true;
//console.log('Success: scrobbled the following track: ' + data.artist + ' - ' + data.track);
} else {
//console.log('Error: could not scrobbled the following track: ' + data.artist + ' - ' + data.track);
}
});
}
/* https://stackoverflow.com/a/9640417 */
function hmsToSecondsOnly(str) {
var p = str.split(':'),
s = 0, m = 1;
while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}
return s;
}
function isHalfway(total, current) {
return current >= (total/2);
}
function checkTotalTime() {
var total = getTotalTime()
if (total.text() != totalTimeStr) {
totalTimeStr = total.text();
totalTime = hmsToSecondsOnly(totalTimeStr);
isScrobbled = false;
metadata = getMetadata();
}
}
function checkCurrentTime() {
checkTotalTime()
metadata = getMetadata();
currentTimeStr = getCurrentTime().text();
currentTime = hmsToSecondsOnly(currentTimeStr);
if (!isScrobbled && isHalfway(totalTime, currentTime)) {
scrobbleTrack(metadata);
} else
if (isScrobbled) {
// TODO: prevent message from being sent multiple times afterward
chrome.runtime.sendMessage({
name: 'scrobbled',
data: metadata
}, function(resp) {
//console.log(resp);
});
}
}
$('#playerMp3flashPlayer').ready(function(){
$('body').on('DOMSubtreeModified', '.utCurrentTime', checkCurrentTime);
});
$('#videonctPlayer').ready(function(){
$('body').on('DOMSubtreeModified', '.utCurrentTime', checkCurrentTime);
});
$('#playPanel').ready(function(){
$('body').on('DOMSubtreeModified', '.utCurrentTime', checkCurrentTime);
});
});
})();