This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
inject.js
82 lines (65 loc) · 2.17 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
(function() {
const {ipcRenderer} = require('electron');
var currentTrack = {};
var updateTimer;
function scheduleScrobbleUpdate(info) {
clearTimeout(updateTimer);
info.timestamp = Math.floor((+new Date()) / 1000);
updateTimer = setTimeout(function() {
ipcRenderer.send('player-scrobble-time', info)
}, 61000); // a bit bigger then minimal limit
}
function runWhenLoaded(func) {
if (playerLoadedp()) {
func();
return;
}
var timer = setInterval(function() {
if (playerLoadedp()) {
clearInterval(timer);
func();
return;
}
}, 200);
}
function runWhenTrackInfoChanges(func) {
var target = document.querySelector('#playerSongInfo');
target.focus();
var observer = new MutationObserver(() => func());
var config = {
childList: true,
subtree: true // see crbug.com/134322
};
observer.observe(target, config);
}
function playerLoadedp() {
return !!document.querySelector("#player-bar-play-pause");
}
function isPlaying() {
var el = document.querySelector("#player-bar-play-pause");
return el && el.classList.contains("playing");
}
function trackInfo() {
return {
track: document.querySelector("#currently-playing-title").innerText,
album: document.querySelector(".player-album").innerText,
artist: document.querySelector(".player-artist").innerText
};
}
function tracksEqualp(one, two) {
return one.track === two.track
&& one.album === two.album
&& one.artist === two.artist;
}
function analyzeTrackChange() {
var playing = isPlaying();
if (!playing) return;
var info = trackInfo();
if (!tracksEqualp(currentTrack, info)) {
currentTrack = info;
scheduleScrobbleUpdate(info);
ipcRenderer.send('player-song-change', info)
}
}
runWhenLoaded(runWhenTrackInfoChanges.bind(null, analyzeTrackChange));
}());