☰

NHentai custom search

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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});
})();