Skip to content

Instantly share code, notes, and snippets.

@stefafafan
Last active July 14, 2020 14:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save stefafafan/88e9caa74bb4268d44df3cd072ccd0b5 to your computer and use it in GitHub Desktop.
Some Userscript to play soundcloud tracks in stream in reverse order
// ==UserScript==
// @name SoundCloud Stream Player
// @namespace https://stefafafan.hatenablog.com/
// @version 0.1
// @description play songs from the stream, in oldest to newest order.
// @author stefafafan
// @match https://soundcloud.com/stream
// @grant none
// ==/UserScript==
window.onload = function () {
main();
}
// 指定した要素を最後まで再生してくれる関数
// 再生中の曲のURLと状態はlocalStorageに保存する
function playElement(element) {
let playControls = document.getElementsByClassName('playControls__timeline')[0];
let button = element.getElementsByClassName('sc-button-play')[0];
button.click();
let link = element.getElementsByClassName('soundTitle__title')[0].href;
localStorage.setItem('nowPlaying', link);
localStorage.setItem('playState', 'playing');
let timer = setInterval(() => {
let timePassed = playControls.getElementsByClassName('playbackTimeline__timePassed')[0].children[1].innerText;
let timeDuration = playControls.getElementsByClassName('playbackTimeline__duration')[0].children[1].innerText;
let timePassedInt = parseInt(timePassed.replace(':', ''));
let timeDurationInt = parseInt(timeDuration.replace(':', ''));
let songEnd = timePassedInt + 1 >= timeDurationInt;
if (songEnd) {
button.click();
localStorage.setItem('playState', 'done');
clearInterval(timer);
}
}, 500);
};
function main() {
// playStateを初期化
localStorage.setItem('playState', 'stopped');
// 前回再生した曲が上位5要素の中にあるかどうかを探し出す
// ない場合は最後の要素を使う
let nowPlaying = localStorage.getItem('nowPlaying');
let topFiveElements = document.getElementsByClassName('soundList__item');
let indexFound = 5; // デフォルトでは最後の曲からスタート
for (let i = 0; i < 5; i++) {
let currentElement = topFiveElements[i];
let link = currentElement.getElementsByClassName('soundTitle__title')[0].href;
if (nowPlaying == link) {
indexFound = i;
break;
}
}
// 0番目の要素が最後に再生した曲の場合はとりあえずリロードする
// ある場合は見つけたindexの直前の要素から逆順に再生していく
if (indexFound != 0) {
let j = indexFound - 1;
let timer = setInterval(() => {
if (j < 0) {
// j が0以下ということはStreamの最初の曲までやってきたので、再生状態を確認して、再生中でなければtimerストップしてリロード
let playState = localStorage.getItem('playState');
if (playState != 'playing') {
clearInterval(timer);
setTimeout(() => {
location.reload();
}, 5000);
}
} else {
let currentElement = topFiveElements[j];
let link = currentElement.getElementsByClassName('soundTitle__title')[0].href;
let nowPlaying = localStorage.getItem('nowPlaying');
let playState = localStorage.getItem('playState');
if (link != nowPlaying && playState != 'playing') {
playElement(currentElement);
j--;
}
}
}, 500);
} else {
setTimeout(() => {
location.reload();
}, 60000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment