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-04-12. See the latest version.

// ==UserScript==
// @name         Better Danbooru Artist Search
// @version      3.5.3
// @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');
            query = query.replace("/whitecube/user/", "/member.php?id=");
            query = query.replace(/\/illust\/.*/, "");
            query = query.replace("#_=_", "");
            query = query.replace('/member_illust', '/member');
            query = query.replace(/&.*/, '');
        }

        else if (query.includes('twitter')) {
            console.log('(arsearch) Is twitter');
            query = query.replace("mobile.", "");
            query = query.replace(/\?.*/, "");
            query = query.replace(/\/status\/.*/, "");
        }

        else 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.lastIndexOf("com") + "com".length);
            }
        }

        else 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) Is mobile');
            }
        }
        
        else if (query.includes('deviantart')) {
            console.log('(arsearch) Is deviant');
            if (!query.endsWith('com')) {
                console.log('(arsearch) Does not end with communism');
                query = query.substr(0, query.lastIndexOf('com') + 'com'.length);
            }
        }

        if (query !== old_query) {
            console.log('(arsearch) new query = ' + 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;
    }
})();