FMARandomPlayer

freemusicarchive random player genre list. Please press the "Random Play" button, url e.g. https://freemusicarchive.org/genre/Blues

当前为 2022-04-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         FMARandomPlayer
// @license      Apache-2.0 License
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  freemusicarchive random player genre list. Please press the "Random Play" button, url e.g. https://freemusicarchive.org/genre/Blues
// @author       bestcondition
// @match        https://freemusicarchive.org/genre/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=freemusicarchive.org
// @grant        none
// ==/UserScript==
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

async function promise_get(get_func, polling_time) {
    let it = get_func()
    while (!it) {
        await sleep(polling_time)
        it = get_func()
    }
    return it
}

function rand_int(begin, end) {
    return Math.round(Math.random() * (end - begin) + begin)
}

const elem_factory = {
    elem_num: 0,
    width: 150,
    height: 50,

    get_step: () => Math.ceil(elem_factory.height * 1.1),

    get_px: (px_num) => px_num + "px",
    get_width: () => elem_factory.get_px(elem_factory.width),
    get_height: () => elem_factory.get_px(elem_factory.height),
    get_top: () => elem_factory.get_px((elem_factory.elem_num + 1) * elem_factory.get_step()),
    create_elem: (tag_name = "button", click_func = null, text = null, type = null) => {
        let elem = document.createElement(tag_name)
        if (text) {
            elem.innerText = text
        }
        if (click_func) {
            elem.onclick = click_func
        }
        if (type) {
            elem.type = type
        }
        elem.style.cssText = "position: fixed; top: 100px; right: 100px; width: 100px; height: 100px; z-index: 9999999999;"
        elem.style.height = elem_factory.get_height()
        elem.style.width = elem_factory.get_width()
        elem.style.top = elem_factory.get_top()
        elem_factory.elem_num += 1
        document.body.appendChild(elem)
        return elem
    },
}

function FMARandomPlayer() {
    this.PAGE_SIZE_NAME = "pageSize"
    this.DEFAULT_PAGE_SIZE = 20
    this.INDEX_NAME = "frp_index"
    this.PAGE_NAME = "page"
    this.POLLING_TIME = 100

    this.get_next_music = () => {
        const music_index = rand_int(0, this.count)
        const page = Math.floor(music_index / this.page_size)
        const index = music_index % this.page_size
        return {page: page, index: index}
    }

    this.to_music = (music) => {
        this.url.searchParams.set(this.PAGE_NAME, music.page)
        this.url.searchParams.set(this.INDEX_NAME, music.index)
        window.location.href = this.url.toString()
    }

    this.next = () => {
        const music = this.get_next_music()
        this.to_music(music)
    }

    this.listen = async () => {
        while (true) {
            let progress_text = document.querySelector("body > div.c-player-container > div > div > div.c-player__time")
            if (progress_text) {
                progress_text = progress_text.innerText
                progress_text = progress_text.replaceAll(" ", "")
                let split_array = progress_text.split('/')
                let begin = split_array[0]
                let end = split_array[1]
                if (end !== "0:00" && begin === end) {
                    this.next()
                    break
                }
            }
            await sleep(this.POLLING_TIME)
        }
    }


    this.url = new URL(window.location.href)

    const count_el = document.querySelector("#content > div.colset-full > div.pagination-full > span:nth-child(1) > b:nth-child(3)")
    this.count = parseInt(count_el.innerText)

    this.page_size = 0
    const maybe_page_size = this.url.searchParams.get(this.PAGE_SIZE_NAME)
    if (maybe_page_size) {
        this.page_size = maybe_page_size
    } else {
        this.page_size = this.DEFAULT_PAGE_SIZE
        this.url.searchParams.set(this.PAGE_SIZE_NAME, this.page_size)
    }

    this.play_bt = elem_factory.create_elem("button", this.next, "Random Play", null)

    let maybe_index = this.url.searchParams.get(this.INDEX_NAME)
    if (maybe_index) {
        maybe_index = parseInt(maybe_index)
        const all_bt = document.querySelectorAll("#content > div.colset-full > div.box-stnd-10pad-20marg.play-lrg-list > div.playlist.playlist-lrg > div span.doplay > a")
        all_bt[maybe_index].click()
    }

}

const frp = new FMARandomPlayer()
frp.listen()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址