Better Danbooru Artist Search

Improve URL handling within the Danbooru artist search engine and automatically navigate to the wiki page if indubitable.

As of 2017-02-03. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Better Danbooru Artist Search
// @version      3.2.0
// @description  Improve URL handling within the Danbooru artist search engine and automatically navigate to the wiki page if indubitable.
// @author       ForgottenUmbrella
// @match        https://danbooru.donmai.us/artists?*
// @namespace    https://greasyfork.org/users/83187
// ==/UserScript==

(function() {
    'use strict';
    console.log('(arsearch) Running');  //I'm very good with names.
    var query = document.getElementById("search_name").value;
    console.log('(arsearch) query = ' + query);
    var old_query = query;
    var domains = [/.com(\/|$)/, /.net(\/|$)/, /.jp(\/|$)/];

    if (domains.some(function(elem){ return ~query.search(elem); }) ||
      (query.match(/\./)
      && query.split(".").every(function(elem){ return elem.length > 1; }))) {
        if (!query.startsWith('http://') && !query.startsWith('https://')) {
            query = "http://" + query;
            console.log('(arsearch) Does not begin with protocol');
        }

        if (query.includes('pixiv')) {
            console.log('(arsearch) Is pixiv');
            if (query.includes('/whitecube/user/')) {
                query = query.replace("/whitecube/user/", "/member.php?id=");
                console.log('(arsearch) whitecube replaced');
            }
            if (query.includes('/illust/')) {
                query = query.replace(/\/illust\/.*/, "");
                console.log('(arsearch) illust removed');
            }
            if (query.includes('#_=_')) {
                query = query.replace("A#_=_", "");
                console.log('(arsearch) weird stuff removed');
            }
            if (query.includes('/member_illust')) {
                query = query.replace('/member_illust', '/member');
                console.log('(arsearch) member_illust replaced');
            }
        }

        if (query.includes('twitter')) {
            console.log('(arsearch) Is twitter');
            if (query.includes('/mobile.')) {
                query = query.replace("/mobile.", "");
                console.log('(arsearch) Is mobile');
            }
            if (query.includes('?p=s')) {
                query = query.replace("?p=s", "");
                console.log('(arsearch) weird stuff removed');
            }
            if (query.includes('/status/')) {
                query = query.replace(/\/status\/.*/, "");
                console.log('(arsearch) Navigated to twitter account home');
            }
        }

        if (query.includes('tumblr')) {
            console.log('(arsearch) Is tumblr');
            if (!query.endsWith('com')) {
                console.log('(arsearch) Does not end with communism');
                query = query.substr(0, query.search("com") + "com".length);
            }
        }

        if (query.includes('nicovideo')) {
            console.log('(arsearch) Is nico');
            if (query.includes('sp')) {
                query = "http://seiga.nicovideo.jp/seiga/" + query.substr(
                    query.length - "imXXXXXXX".length);
                console.log('(arsearch) Contains sp');
            }
        }

        if (query !== old_query) {
            document.getElementById("search_name").value = query;
            document.getElementsByName("commit")[0].click();
        }
    }

    var artists_list = document.getElementsByTagName("tbody")[1];
    if (artists_list.childElementCount === 1) {
        //Redundant variables because it would otherwise be too unwieldy.
        var first_artist = artists_list.firstElementChild;
        var artist_table_data = first_artist.getElementsByTagName("td")[0];
        var url = artist_table_data.getElementsByTagName("a")[0].href;
        window.location.href = url;
    }
})();