☰

NHentai custom search

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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