Skip to content

Instantly share code, notes, and snippets.

@okj579
Last active November 24, 2020 11:10
Show Gist options
  • Save okj579/9c1e8e94762e70f08aaa789cc0541cfd to your computer and use it in GitHub Desktop.
Save okj579/9c1e8e94762e70f08aaa789cc0541cfd to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name WWCD Now Playing Notifications
// @match *://cd101.com/stream-player/index.php
// @match *://cd1025.com/stream-player/index.php
// @match *://cd929fm.com/stream-player/index.php
// @grant none
// ==/UserScript==
(function() {
'use strict';
let style = document.createElement('style');
document.head.appendChild(style);
style.sheet.insertRule('.now-playing {padding: 0;}');
style.sheet.insertRule('.now-playing li {list-style: none; clear:both}');
style.sheet.insertRule('.now-playing img {float:left; margin: 0 5px 5px 0}');
style.sheet.insertRule('.now-playing span {display: block}');
let nowPlaying = document.createElement('ul');
nowPlaying.classList = 'now-playing';
document.querySelector('#top-left').appendChild(nowPlaying);
Notification.requestPermission();
var lastResult;
function check(){
fetch('/a/refresh')
.then(resp => resp.text())
.then(result => {
if (result == lastResult) {
return;
}
lastResult = result;
nowPlaying.innerHTML = result;
let ul = document.createElement('ul');
ul.innerHTML = result;
let li = ul.firstElementChild;
li.querySelectorAll('strong').forEach(el => el.parentElement.removeChild(el));
result = {
thumb: li.querySelector('img.thumb').src,
artist: li.querySelector('.artist').innerText.trim(),
song: li.querySelector('.song').innerText.trim(),
}
if (Notification.permission == 'granted') {
new Notification(result.song, {
body: result.artist,
icon: result.thumb,
tag: 'now-playing',
renotify: true,
silent: true,
});
}
});
}
check();
setInterval(check, 60000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment