Kanji Koohii Quick Add

Add a 'Quick Add' button

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Kanji Koohii Quick Add
// @namespace    https://www.fiverr.com/josefandersson0
// @version      1.0
// @description  Add a 'Quick Add' button
// @author       Josef Andersson
// @match        https://kanji.koohii.com/study/kanji/*
// @run-at       document-end
// ==/UserScript==

const menuBtn = document.querySelector('.JsEditFlashcard');

let ucs;

try {
    ucs = JSON.parse(menuBtn.dataset.param).ucs;
} catch (e) {
    console.warn('Invalid ucs!');
}

if (ucs) {
    const quickBtn = Object.assign(document.createElement('a'), {
        className:'uiGUI btn btn-success is-0', innerHTML:'<i class="fa fa-plus"></i>Quick Add', href:'javascript:void(0);' });
    quickBtn.style.marginLeft = '5px';
    
    let busy = false;
    
    quickBtn.addEventListener('click', ev => {
        ev.preventDefault();
        if (busy) return;
        busy = true;

        console.log('Quick adding...');

        fetch('/flashcards/dialog', {
            credentials: 'include',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `ucs=${ucs}&menu=add`
        }).then(r => r.json()).then(json => {
            if (json.status === 2) {
                menuBtn.parentElement.innerHTML = json.html;
            } else {
                console.warn('Unknown response:', json);
            }
        }).catch(err => {
            console.error(err);
            busy = false;
        });
    });

    menuBtn.parentElement.insertBefore(quickBtn, menuBtn);
    menuBtn.parentElement.insertBefore(menuBtn, quickBtn);
}