☰

NHentai custom search

A fork of Dar9586's Nhentai Only english to include a changeable custom search.

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 or Violentmonkey 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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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         NHentai custom search
// @namespace    mr feelgood
// @version      1.3.2
// @description  A fork of Dar9586's Nhentai Only english to include a changeable custom search.
// @author       h8code
// @license      https://opensource.org/licenses/MIT
// @match        *://nhentai.net/*
// @run-at       document-end
// @grant        none
// @license      https://opensource.org/licenses/MIT
// ==/UserScript==

'use strict';

console.log("Tampermonkey script loaded");

const search = [
    {
        val: 'language:english',
        simplified: 'english'
    },
    {
       val: '-tag:"ai generated"',
       simplified: 'ai generated'
    },
    // EXTRA SEARCH TERMS
    // {
    //    val: "tag:some_tag",
    //    simplified: "some_tag",
    // },
];

function getSearchString() {
    let s = '';
    for (let v of search) {
        s += ' ';
        s += v.val;
    }
    return s.trim();
}

function hideSearch(){
    let textBox=document.getElementsByName('q')[0];
    for (let v of search) {
        console.log({textboxValue: textBox.value});
        if(textBox.value.includes(v.val))textBox.value=textBox.value.replace(v.val,"").trim();
    }
}

function setSearch(){
    let textBox=document.getElementsByName('q')[0];
    for (let v of search) {
        if(!textBox.value.includes(v.simplified))textBox.value+=` ${v.val}`;
    }
}

function redirectToSearch(list){
    let searchStr = encodeURIComponent(getSearchString());
    for (let item of list) {
        let old = item.href
        //                                 domain           type  name
        item.href=item.href.replace(/https:\/\/nhentai.net\/(.*)\/(.*)\//,'https://nhentai.net/search/?q=$1:"$2" '+searchStr);
        let newI = item.href;
        console.log({old, new: newI});
    }
}




(function() {
    'use strict';

    function runScript() {
        console.log("Running script on:", location.href);

            //remove english tag because it's ugly
            hideSearch();
            //redirect if going to main page
            if(window.location.pathname=="/")window.location.replace("https://nhentai.net/search/?q="+encodeURIComponent(getSearchString()))
            //append english to end of query
            document.getElementsByTagName('form')[0].onsubmit=setSearch
            //replace logo location
            document.getElementsByTagName('nav')[0].childNodes[0].href='/search/?q='+encodeURIComponent(getSearchString())
            //when using tab in navbar
            if(document.getElementById("tag-container")!=null)redirectToSearch(document.getElementById("tag-container").getElementsByTagName("a"))
            //on galleries
            if(document.getElementById("tags")!=null)redirectToSearch(document.getElementById("tags").getElementsByTagName("a"))

    }

    // Run once on load
    runScript();

    // Detect changes
    new MutationObserver(() => {
        console.log("Observed");
        console.log(location.href);

        runScript();
    }).observe(document, { subtree: true, childList: true});
})();