Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compatibility for SwSpotify #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 86 additions & 90 deletions notifications.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,99 @@
var alert_str = "[SwagLyrics for Spotify]";
const notification_timeout = 4000;
const interval = 1000;
const hash = hex_md5("temp");

const alert_str = "[SwagLyrics for Spotify]";
console.log(alert_str, "initializing (available: " + (typeof window.Notification != 'undefined') + ")");
var stay = 4000;
var isFirst=true;
var interval = 1000;
var hash = hex_md5("temp");

const selectors = {
albumArt:
'#main .Root__now-playing-bar .now-playing-bar__left .cover-art-image.cover-art-image-loaded',
trackName: '.track-info__name a',
artistName: '.track-info__artists a',
playPauseBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(3)',
prevBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(2)',
nextBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(4)',
albumArt:
'#main .Root__now-playing-bar .now-playing-bar__left .cover-art-image.cover-art-image-loaded',
trackName: '.track-info__name a',
artistName: '.track-info__artists a',
playPauseBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(3)',
prevBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(2)',
nextBtn:
'#main .Root__now-playing-bar .now-playing-bar__center .player-controls__buttons button:nth-child(4)',
};
var checks = {
art: function() {
var $img = $('#cover-art').find('.sp-image-img');
if ($img.length > 0) {

return document.querySelector(`${selectors.albumArt}`).style.backgroundImage;
}
return null;
},
name: function() {
return document.querySelector(`${selectors.trackName}`).innerText;
},
artist: function() {
return document.querySelector(`${selectors.artistName}`).innerText;
}
};

const checks = {
art: () => {
let $img = $('#cover-art').find('.sp-image-img');
if ($img.length > 0) {
return document.querySelector(`${selectors.albumArt}`).style.backgroundImage;
}
return null;
},
name: () => {
return document.querySelector(`${selectors.trackName}`).innerText;
},
artist: () => {
return document.querySelector(`${selectors.artistName}`).innerText;
}
};


if (window.Notification) {
console.log(alert_str, "requesting permission");
window.Notification.requestPermission(function() {
console.log(alert_str, "permission granted");
if (!localStorage.scn_hash) {
localStorage.scn_hash = hash;
}
setInterval(function() {
var result = {};
var hash = null;
var text = null;
var cont = false;
for (var 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;
}
}
}

xhr = new XMLHttpRequest();
var url = "http://127.0.0.1:5042/getsong";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Access-Control-Allow-Headers","*");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = JSON.parse(xhr.responseText);

}
}
var data = JSON.stringify({title:result.name,artist:result.artist});
if(isFirst){
xhr.send(data);
isFirst=false;
}
console.log(alert_str, "requesting notification permission");
window.Notification.requestPermission(function () {
console.log(alert_str, "notification permission granted");
if (!localStorage.scn_hash) {
localStorage.scn_hash = hash;
}
setInterval(function () {
let result = {};
let hash = null;
let text = null;
let cont = false;

if (cont) {
setTimeout(function() {
hash = hex_md5(JSON.stringify(result));
if (localStorage.scn_hash !== hash) {
localStorage.scn_hash = hash;
console.log(alert_str, "new song", result);
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;
}
}
}

const xhr = new XMLHttpRequest();
const url = "http://127.0.0.1:5043";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
JSON.parse("{}");
}
};
let data = JSON.stringify({title: result.name, artist: result.artist});

xhr.send(data);
var notification = new window.Notification(result.name, {
body: result.artist,
icon: result.art
});
setTimeout(function() {
notification.close();
}, stay);
}
}, 200);
}
}, interval);
});
}
// if(isFirst){
// // send initial data packet
// xhr.send(data);
// isFirst = false;
// }

if (cont) {
setTimeout(function () {
hash = hex_md5(JSON.stringify(result));
if (localStorage.scn_hash !== hash) {
localStorage.scn_hash = hash;
console.log(alert_str, "new song", result);
let notification = new window.Notification(result.name, {
body: result.artist,
icon: result.art
});
setTimeout(function () {
notification.close();
}, notification_timeout);
}
xhr.send(data);
}, 200);
}
}, interval);
});
}



Expand Down