Add button on exhentai searchbox from tag button

Add your favorite keyword/tag on the searchbox from tag button

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add button on exhentai searchbox from tag button
// @description  Add your favorite keyword/tag on the searchbox from tag button
// @license      MIT
// @version      0.4
// @match        https://exhentai.org/g/*
// @run-at       context-menu
// @namespace https://greasyfork.org/users/383371
// ==/UserScript==


function capitalize(str) {
   var tmp = str.toLowerCase().split(' ');
   for(let i = 0; i < tmp.length; i++) {
       tmp[i] = tmp[i].charAt(0).toUpperCase() + tmp[i].substring(1);
   }
   return tmp.join(' ');
}

function add_tag(tag, name){
    let config = JSON.parse(window.localStorage.getItem('config'));
    if(!config){
        config = [];
    }
    config.push([tag, name]);
    window.localStorage.setItem('config', JSON.stringify(config));
    let search_tag_config = JSON.parse(window.localStorage.getItem('search_tag_config'));
    if(!search_tag_config){
        search_tag_config = [];
        if(config){
            for(let ele of config){
                search_tag_config.push(
                    {
                        'key': ele[1],
                        'value': ele[0],
                        'color': '',
                    }
                );
            }
        }
    }else{
        search_tag_config.push(
            {
                'key': name,
                'value': tag,
                'color': '',
            }
        );
    }
    window.localStorage.setItem('search_tag_config', JSON.stringify(search_tag_config));
}

(function() {
    if(document.activeElement.id.substring(0, 2) == 'ta'){
        let tag = document.activeElement.href.substring(document.activeElement.href.indexOf('tag/') + 4).replaceAll('+', ' ');
        let tmp = tag.split(':');
        let tag_namespace = tmp[0];
        let tag_value = tmp[1];
        tag_value += '$';
        if(tag_value.includes(' ')){
            tag_value = `"${tag_value}"`;
        }
        let name = prompt('name', capitalize(document.activeElement.innerText));
        if(!name){
            return;
        }
        add_tag(`${tag_namespace}:${tag_value}`, name);
        alert(`${tag_namespace}:${tag_value} added`);
    }else if(document.activeElement.href.includes('uploader')){
        let uploader = document.activeElement.text;
        let tag = `uploader:${uploader}`;
        let name = prompt('name', capitalize(uploader));
        if(!name){
            return;
        }
        add_tag(`${tag}`, name);
        alert(`${tag} added`);
    }
})();