What.CD: YAVAH

Yet Another Various Artists Helper

目前為 2018-09-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           What.CD: YAVAH
// @namespace      hateradio)))
// @author         hateradio
// @version        4
// @description    Yet Another Various Artists Helper
// @icon           data:image/pngbase64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPS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QxQLGbDsWDFOX5sbSuE2rRETmujB9b3bdEcGaARAx66NXoq3Gwq7H8WdwD+XwB+lPj8PwHgOK3fSnj4r2n9TB7wPa1v0ko0eF2Sw39J60fY/1uAAQDAo7S7+Gt3nAAAAABJRU5ErkJggg==
// @include        /https://redacted\.ch/(torrents\.php(\?|\?page=\d+&)id=\d+|upload\.php)/
// @include        /https://apollo\.rip/(torrents\.php(\?|\?page=\d+&)id=\d+|upload\.php)/
// @include        /https://notwhat\.cd/(torrents\.php(\?|\?page=\d+&)id=\d+|upload\.php)/
// #updated        09 Sep 2018
// #since          18 Jun 2010
// ==/UserScript==

/* jshint esversion: 6, asi: true */
(() => {
    'use strict'

    const css = {
        add: function (t) {
            if (!this.style) {
                this.style = document.createElement('style')
                this.style.type = 'text/css'
                document.body.appendChild(this.style)
            }
            this.style.appendChild(document.createTextNode(t + '\n'))
        }
    }

    function embedScript(func) {
        const script = document.createElement('script')
        script.type = 'application/javascript'
        script.textContent = '(' + func + ')();'
        document.body.appendChild(script)
        document.body.removeChild(script)
    }

    function debounce(func, wait) {
        let timeout
        return function () {
            const context = this, args = arguments
            const run = () => {
                timeout = null
                func.apply(context, args)
            }

            clearTimeout(timeout)
            timeout = setTimeout(run, wait)
        }
    }

    class YavaMenu {

        constructor() {
            this.types = ['Main', 'Guest', 'Remixer', 'Composer', 'Conductor', 'DJ / Compiler', 'Producer']
            this.sibling = document.querySelector('.box_addartists, #dynamic_form')
            if (!this.sibling) return false

            this.boxSetup()
        }

        boxSetup() {
            const box = document.createElement('div')
            box.className = 'box'
            box.id = 'YAVAH'
            box.innerHTML = '<div class="head"><strong><abbr title="Yet Another Various Artists Helper">YAVAH</abbr></strong></div>'
            box.appendChild(this.menu())

            this.box = box
            this.sibling.parentElement.insertBefore(this.box, this.sibling)
        }

        menu() {
            const m = document.createElement('div')
            m.setAttribute('style', 'padding: 3px 6px 6px')
            m.innerHTML = this.generateBoxes()
            m.querySelector('textarea').className = 'noWhutBB yavahtext'
            return m
        }

        addEvent(cb) {
            return this.box && !this.box.addEventListener('input', debounce(cb, 250), false)
        }

        generateBoxes() {
            const t = ['<p>Enter artists, one per line. (Toggle.)</p>']

            this.types.forEach(type => {
                t.push('<p onclick="$(this).nextElementSibling().toggleClass(\'hidden\')"><a href="#" onclick="return false">' +
                    type + '</a></p><textarea class="noWhutBB yavahtext hidden"></textarea>')
            })

            t.push('<p>Review the changes below.</p>')
            return t.join('')
        }

    }

    class Yavah {

        constructor() {
            this.selector = 'input[name="aliasname[]"]:last-of-type, input[name="artists[]"]:last-of-type'
            this.a = document.querySelector('.box_addartists a, #artistfields a.brackets')
            this.inputs = document.querySelector('#AddArtists, #artistfields')

            css.add('#YAVAH p a { display:block } .yavahtext{max-width: 90% height: 50px}')

            this.stored = this.inputs.innerHTML
            this.event = this.event.bind(this)
        }

        /**
         *
         * @param {HTMLTextAreaElement} textarea
         * @param {number} index Index of HTMLOptionElement within HTMLSelectElement to set
         */
        fill(textarea, index) {
            const lines = textarea.value.match(/[^\r\n]+/g) || []

            lines.map(line => line && line.trim()).forEach(line => {
                if (line) {
                    this.a.click()
                    this.inputs.querySelector(this.selector).value = line
                    this.inputs.querySelector('select:last-of-type').value = index
                }
            })
        }

        event() {
            // Reset the artist box
            this.inputs.innerHTML = this.stored
            embedScript(() => window.ArtistFieldCount = 0)

            const textareas = document.querySelectorAll('#YAVAH textarea')
            Array.from(textareas).forEach((e, i) => this.fill(e, i + 1))
        }

        static main() {
            const yava = new Yavah()
            const menu = new YavaMenu()
            menu.addEvent(yava.event)
        }

    }

    Yavah.main()

})()