Soundcloud - stop autoplay tracks

when track goes into last seconds, it will stop

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Soundcloud - stop autoplay tracks
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  when track goes into last seconds, it will stop
// @author       https://puvox.software
// @match        https://*.soundcloud.com/*
// @icon         https://www.google.com/s2/favicons?domain=soundcloud.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
     const stopWithinSecondsLast = 3;

    function hmsToSecondsOnly(str) {
        var p = str.split(':'),
            s = 0, m = 1;

        while (p.length > 0) {
            s += m * parseInt(p.pop(), 10);
            m *= 60;
        }

        return s;
    }

    const intervalCheck = setInterval ( ()=> {
        const playingState = document.querySelector('.playControls__play.playing');
        if (playingState) {
            const currentTimeElement = document.querySelector('.playbackTimeline__timePassed [aria-hidden="true"]');
            const currentTimeSeconds = hmsToSecondsOnly(currentTimeElement.textContent);
            const fullDurationElement = document.querySelector('.playbackTimeline__duration [aria-hidden="true"]');
            const fullTimeSeconds = hmsToSecondsOnly(fullDurationElement.textContent);
            if (fullTimeSeconds-currentTimeSeconds < stopWithinSecondsLast) {
                try {
                    playingState.click();
                } catch(e){}
            }
        }
    }, 1000);
})();