Gelbooru Favorites Search

Adds a search bar to Favorites pages to search for tags.

目前為 2020-11-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Gelbooru Favorites Search
// @namespace    http://tampermonkey.net/
// @version      1.2.1
// @description  Adds a search bar to Favorites pages to search for tags.
// @author       Xerodusk
// @homepage     https://greasyfork.org/en/users/460331-xerodusk
// @include      https://gelbooru.com/index.php*page=favorites*
// @grant        none
// @icon         https://gelbooru.com/favicon.png
// ==/UserScript==
/* jshint esversion: 6 */
function GetUserID() {
    'use strict';

    // Get all paremeters for current URL
    const searchParams = new URLSearchParams(window.location.search);
    if (searchParams.has('id')) { // Get the user id if exists
        return searchParams.get('id');
    } else { // otherwise cancel
        return -1;
    }
}

function CreateSearchField(userID) {
    'use strict';

    const searchForm = document.createElement('form');
    searchForm.classList.add('searchform');
    searchForm.setAttribute('action', '/index.php');
    searchForm.setAttribute('method', 'get');

    const searchParameterPage = document.createElement('input');
    searchParameterPage.setAttribute('type', 'hidden');
    searchParameterPage.setAttribute('name', 'page');
    searchParameterPage.setAttribute('value', 'post');

    const searchParameterS = document.createElement('input');
    searchParameterS.setAttribute('type', 'hidden');
    searchParameterS.setAttribute('name', 's');
    searchParameterS.setAttribute('value', 'list');

    const searchBar = document.createElement('input');
    searchBar.classList.add('searchbar');
    searchBar.setAttribute('id', 'tags-search');
    searchBar.setAttribute('name', 'tags');
    searchBar.setAttribute('type', 'text');
    searchBar.setAttribute('placeholder', 'Ex: blue_sky cloud 1girl');
    searchBar.setAttribute('required', 'required');

    const searchButton = document.createElement('input');
    searchButton.classList.add('searchbutton');
    searchButton.setAttribute('type', 'submit');
    searchButton.setAttribute('value', 'Search');

    searchForm.onsubmit = function() {
        searchBar.classList.add('submitted');
        searchBar.value = "fav:" + userID + " " + searchBar.value;
    }

    searchForm.appendChild(searchParameterPage);
    searchForm.appendChild(searchParameterS);
    searchForm.appendChild(searchBar);
    searchForm.appendChild(searchButton);
    document.body.insertBefore(searchForm, document.body.getElementsByClassName('thumb')[0]);

    const css = document.createElement('style');

    css.innerHTML = `
        .searchform {
            padding: 0 15px 15px 15px;
            width: 100%;
            box-sizing: border-box;
        }
        input[type='hidden'] {
            padding: 0;
        }
        .searchbar {
            padding: 7px;
            width: calc(100% - 175px);
            border: 1px solid #e0e0e0;
            box-sizing: border-box;
        }
        .searchbar:focus {
            background-color: initial !important;
        }
        .searchbar.submitted {
            color: transparent;
        }
        .searchbutton {
            cursor: pointer;
            padding: 8px 15px;
            margin: 0 4px;
            width: 120px;
            color: #fff;
            font-weight: bold;
            border: 0px;
            background: #0773fb;
            box-sizing: border-box;
        }
        .searchbutton:hover {
            background: #fbb307;
        }
    `;

    document.head.appendChild(css);
}

(function() {
    'use strict';
    const userID = GetUserID();
    if (userID >= 0) {
        CreateSearchField(userID);
    }
})();