8uny87n76gyji / Femdomcult - add tag autocompletion/search to pages that don't have it

// ==UserScript==
// @name         Femdomcult - add tag autocompletion/search to pages that don't have it
// @namespace    https://femdomcult.org
// @version      0.0.2
// @description  Add tag autocompletion to the torrent, request, collage, top10 pages
// @author       Convenience
// @icon         https://femdomcult.org/favicon.ico
// @match        https://femdomcult.org/*
// @grant        none
// @license      MIT
// ==/UserScript==
(function () {
    'use strict'
    var tag_list_id = 'tagname'
    var html_location_to_insert_tag_search
    switch (window.location.pathname) {
        case '/torrents.php':
            if (!window.location.search)
                return
            html_location_to_insert_tag_search = document.querySelector("#form_addtag")
            break
        case '/requests.php':
            if (window.location.search === '?action=new') {
                tag_list_id = 'tags'
                html_location_to_insert_tag_search = document.querySelector("#request_form > table > tbody > tr:nth-child(6) > td:nth-child(2)")
            } else {
                document.getElementsByName('tags')[0].id = tag_list_id
                html_location_to_insert_tag_search = document.querySelector("#content > div > form > div.box.pad > table:nth-child(3) > tbody > tr:nth-child(2) > td:nth-child(2)")
            }
            break
        case '/collages.php':
            if (window.location.search === '?action=new') {
                tag_list_id = 'tags'
                html_location_to_insert_tag_search = document.querySelector("#content > div > form > table > tbody > tr:nth-child(5) > td:nth-child(2)")
            } else {
                document.getElementsByName('tags')[0].id = tag_list_id
                html_location_to_insert_tag_search = document.querySelector("#content > div > form > table > tbody > tr:nth-child(2) > td:nth-child(2)")
            }
            break
        case '/top10.php':
            document.getElementsByName('tags')[0].id = tag_list_id
            html_location_to_insert_tag_search = document.querySelector("#content > div > form > table > tbody > tr:nth-child(1) > td:nth-child(2)")
            break
        default:
            return
    }
    if (!html_location_to_insert_tag_search || !document.getElementById(tag_list_id))
        return
    function add_html_script_tag(src, innerHTML, onload) {
        var script = document.createElement('script')
        if (src)
            script.src = src
        if (innerHTML)
            script.appendChild(document.createTextNode(innerHTML))
        if (onload)
            script.setAttribute('onload', onload)
        document.head.appendChild(script)
    }
    var tag_search_html = `
    <div class="autoresults">
        <input type="text" id="tag_search" value="search tags" onfocus="if (this.value == 'search tags') this.value='';"
            onblur="if (this.value == '') this.value='search tags';" onkeyup="return autocomp.keyup(event);"
            onkeydown="return autocomp.keydown(event);" autocomplete="off"
            title="enter text to search for tags, click (or enter) to select a tag from the drop-down (BETA)">
        <ul id="tag_complete" style="visibility: hidden;"></ul>
    </div>`
    html_location_to_insert_tag_search.insertAdjacentHTML('beforeEnd', tag_search_html)
    add_html_script_tag('', `
    function Start_Tags() {
        autocomp.start('tags', 'tag_')
    }
    function clicked(tag) {
        var tag_list_id = '${tag_list_id}'
        var tag_list = document.getElementById(tag_list_id)
        if (!tag) return;
        if (tag_list.value == '') {
            tag_list.value = tag
        } else {
            tag_list.value += ' ' + tag
        }
        resize(tag_list_id)
        var tag_search = document.getElementById('${'tag_search'}')
        tag_search.value = ''
        tag_search.focus()
    }`, '')
    add_html_script_tag('static/functions/autocomplete.js', '', 'addDOMLoadEvent(Start_Tags)')
})()