NowSniper

Now Playing Data

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         NowSniper
// @namespace    NowSniper
// @version      1.0.1
// @description  Now Playing Data
// @author       Kıraç Armağan Önal
// @match        *://open.spotify.com/*
// @match        *://soundcloud.com/*
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    'use strict';

    var ws = new WebSocket("ws://127.0.0.1:9081");
    unsafeWindow.ws = ws;
    function wsSend(data){
        ws.send(JSON.stringify({data,hostname:window.location.hostname,date:Date.now()}));
    }
    function StartFunction(){

        setInterval(()=>{
            let hostname = window.location.hostname;
            if (hostname == "soundcloud.com") {
              let isPlaying = document.querySelector(".playControl").classList.contains("playing");
              let artwork = document.querySelector(".playbackSoundBadge span.sc-artwork").style.backgroundImage.slice(5,-2).replace("t50x50","t500x500");
              let title = document.querySelector(".playbackSoundBadge__titleLink").title;
              let artist = document.querySelector(".playbackSoundBadge__lightLink").title;
              let timePassed = document.querySelector(".playbackTimeline__timePassed span:nth-child(2)").textContent;
              let totalDuration = document.querySelector(".playbackTimeline__duration span:nth-child(2)").textContent;
              let albumLink = document.querySelector(".playbackSoundBadge__titleLink").href;
              wsSend({artwork,title,artist,isPlaying,timePassed,totalDuration,albumLink});

            } else if (hostname == "open.spotify.com") {
              let isPlaying = !!document.querySelector('.player-controls [data-testid="control-button-pause"]');
              let title = document.querySelector('[data-testid="nowplaying-track-link"]').textContent;
              let artist = [...new Set(Array.from(document.querySelectorAll('span[draggable] a[href*="artist"]')).map(i=>i.textContent))].join(", ");
              let artwork = document.querySelector('[data-testid="CoverSlotExpanded__container"] .cover-art-image').style.backgroundImage.slice(5,-2);
              let timePassed = document.querySelectorAll(".playback-bar .playback-bar__progress-time")[0].textContent;
              let totalDuration = document.querySelectorAll(".playback-bar .playback-bar__progress-time")[1].textContent;
              let albumLink = document.querySelector('[data-testid="nowplaying-track-link"]').href;
              wsSend({artwork,title,artist,isPlaying,timePassed,totalDuration,albumLink});
            }

        },1000);

    }

    if (ws.readyState == WebSocket.OPEN) {
       StartFunction();
    } else {
       ws.onopen = ()=>{StartFunction();}
    }

})();