Gelbooru Favorites Search

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

As of 2020-05-24. See the latest version.

// ==UserScript==
// @name         Gelbooru Favorites Search
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @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
    let 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';

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

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

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

    let 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');
    searchBar.setAttribute('autocomplete', 'off');

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

    searchForm.onsubmit = function() {
        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]);

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

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

    document.head.appendChild(css);
}

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