|
| 1 | +// ==UserScript== |
| 2 | +// @name SoundCloud Remote Controller |
| 3 | +// @namespace http://tampermonkey.net/ |
| 4 | +// @version 0.1 |
| 5 | +// @description |
| 6 | +// @author Decode |
| 7 | +// @match https://soundcloud.com/* |
| 8 | +// @grant none |
| 9 | +// @require http://code.jquery.com/jquery-3.3.1.min.js |
| 10 | +// ==/UserScript== |
| 11 | + |
| 12 | +(function () { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + var socket = new WebSocket('ws://127.0.0.1:15769/soundcloud/desktop'); |
| 16 | + setupSocket(socket); |
| 17 | + $("<div id=\"desktop\" style=\"text-align: center; top: 0; z-index: 99999; display: inline-block; position: fixed; right: 15px; height: 40px; line-height: 40px; color: red; text-align: center; font-weight: bold;\"><span id=\"desktoponline\" style=\"top: 50%; transform: translateY(-50%);\">Offline</span></div>").appendTo('body'); |
| 18 | + |
| 19 | + function getCookie(name) { |
| 20 | + var matches = document.cookie.match(new RegExp( |
| 21 | + "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" |
| 22 | + )); |
| 23 | + return matches ? decodeURIComponent(matches[1]) : undefined; |
| 24 | + } |
| 25 | + |
| 26 | + function setCookie(name, value, options) { |
| 27 | + options = options || {}; |
| 28 | + |
| 29 | + var expires = options.expires; |
| 30 | + |
| 31 | + if (typeof expires == "number" && expires) { |
| 32 | + var d = new Date(); |
| 33 | + d.setTime(d.getTime() + expires * 1000); |
| 34 | + expires = options.expires = d; |
| 35 | + } |
| 36 | + if (expires && expires.toUTCString) { |
| 37 | + options.expires = expires.toUTCString(); |
| 38 | + } |
| 39 | + |
| 40 | + value = encodeURIComponent(value); |
| 41 | + |
| 42 | + var updatedCookie = name + "=" + value; |
| 43 | + |
| 44 | + for (var propName in options) { |
| 45 | + updatedCookie += "; " + propName; |
| 46 | + var propValue = options[propName]; |
| 47 | + if (propValue !== true) { |
| 48 | + updatedCookie += "=" + propValue; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + document.cookie = updatedCookie; |
| 53 | + } |
| 54 | + |
| 55 | + var id = getCookie('soundcloudcontroller'); |
| 56 | + |
| 57 | + function setupSocket(s){ |
| 58 | + s.onopen = function() { |
| 59 | + console.log('Connected'); |
| 60 | + $('#desktoponline').css('color', 'green'); |
| 61 | + $('#desktoponline').html('Online'); |
| 62 | + }; |
| 63 | + |
| 64 | + s.onclose = function() { |
| 65 | + console.log('Disconnected'); |
| 66 | + $('#desktoponline').css('color', 'red'); |
| 67 | + $('#desktoponline').html('Offline'); |
| 68 | + setTimeout(function () { |
| 69 | + socket = new WebSocket('ws://127.0.0.1:15769/soundcloud/desktop'); |
| 70 | + setupSocket(socket); |
| 71 | + }, 1000); |
| 72 | + }; |
| 73 | + |
| 74 | + s.onmessage = function (message) { |
| 75 | + var text = message.data; |
| 76 | + if (text === 'state') { |
| 77 | + socket.send('playing:' + ($('.playing').length > 0)); |
| 78 | + socket.send('curtime:' + $('.playbackTimeline__timePassed span').get(1).innerHTML); |
| 79 | + socket.send('wholetime:' + $('.playbackTimeline__duration span').get(1).innerHTML); |
| 80 | + socket.send('valuemax:' + $('.playbackTimeline__progressWrapper').attr('aria-valuemax')); |
| 81 | + socket.send('valuenow:' + $('.playbackTimeline__progressWrapper').attr('aria-valuenow')); |
| 82 | + socket.send('songtitle:' + $('.playbackSoundBadge__lightLink').attr('title')); |
| 83 | + socket.send('songname:' + $('.playbackSoundBadge__title a').attr('title')); |
| 84 | + socket.send('picture:' + $('.listenArtworkWrapper__artwork div span').css('background-image')); |
| 85 | + |
| 86 | + } |
| 87 | + if (text.startsWith('time')) { |
| 88 | + $('.playbackTimeline__progressWrapper').attr('aria-valuenow', text.split('1')[1]); |
| 89 | + } |
| 90 | + if (text.startsWith('left')) { |
| 91 | + $('.playControls__prev').click(); |
| 92 | + } |
| 93 | + if (text.startsWith('play')) { |
| 94 | + $('.playControls__play').click(); |
| 95 | + } |
| 96 | + if (text.startsWith('pause')) { |
| 97 | + $('.playControls__play').click(); |
| 98 | + } |
| 99 | + if (text.startsWith('right')) { |
| 100 | + $('.playControls__next').click(); |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +}) |
| 107 | +(); |
0 commit comments