-
Notifications
You must be signed in to change notification settings - Fork 4
/
bridge.js
108 lines (100 loc) · 3.15 KB
/
bridge.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
const alert_str = "[SwagLyrics for Spotify]";
console.log(alert_str, " initializing");
const stay = 4000;
let isFirst = true;
const interval = 1000;
const checkInterval = 400;
let cont = false;
const selectors = {
albumArt:
'.cover-art-image',
trackName: 'div.now-playing-bar__left div.now-playing div._2d35c1726829c507fca5a9b5b1aae1a2-scss.ellipsis-one-line div.c319b99793755cc3bba709fe1b1fda42-scss.ellipsis-one-line div.react-contextmenu-wrapper span a',
artistName: 'span .react-contextmenu-wrapper span a',
playPauseBtnTitle:
'#main > div > div.Root__top-container > div.Root__now-playing-bar > footer > div > div.now-playing-bar__center > div > div.player-controls__buttons > div:nth-child(3) button'
};
const checks = {
art: function () {
let img = document.querySelector(selectors.albumArt);
if (img !== null) {
return img.style.backgroundImage.slice(5, -2);
}
return null;
},
name: function () {
element = document.querySelector(`${selectors.trackName}`);
if (element !== null){
return element.innerText;
}
return null;
},
artist: function () {
element = document.querySelector(`${selectors.artistName}`);
if (element !== null){
return element.innerText;
}
return null;
},
playState: function () {
element = document.querySelector(`${selectors.playPauseBtnTitle}`);
if (element !== null){
return element.getAttribute("title");
}
return null;
}
};
const testConnection = new XMLHttpRequest();
const url = "http://127.0.0.1:5043/ping";
openTestConnection();
testServerConnection();
function openTestConnection() {
testConnection.open("GET", url, true);
testConnection.setRequestHeader("Content-type", "application/json");
testConnection.setRequestHeader("Access-Control-Allow-Origin", "*");
testConnection.setRequestHeader("Access-Control-Allow-Headers", "*");
}
function testServerConnection() {
setInterval(() => {
checkServerConnection();
}, checkInterval);
}
function checkServerConnection(){
console.log("Testing connection...");
openTestConnection();
testConnection.send();
testConnection.onreadystatechange = function () {
if (testConnection.readyState == 4 && (testConnection.status == 200 || testConnection.status == 105)) {
console.log("Connection established");
sendToSwSpotify(getSongData(true));
}
}
}
function getSongData(asJson = false){
let result = {};
let text = null;
cont = false;
for (let i in checks) {
if (checks.hasOwnProperty(i)) {
text = checks[i].call();
if (typeof text != "undefined" && text != null && text.length > 0) {
result[i] = text;
cont = true;
}
}
}
if(asJson === true){
return JSON.stringify({ title: result.name, artist: result.artist, playState: result.playState });
}
else {
return result;
}
}
function sendToSwSpotify(data) {
xhr2 = new XMLHttpRequest();
const url = "http://127.0.0.1:5043/getSong";
xhr2.open("POST", url, true);
xhr2.setRequestHeader("Content-type", "application/json");
xhr2.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr2.setRequestHeader("Access-Control-Allow-Headers", "*");
xhr2.send(data);
}