Mouse dictionary helper

Mouse Dictionary で引いた結果をAnkiwebのdeckに登録する君

目前為 2021-07-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Mouse dictionary helper
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Mouse Dictionary で引いた結果をAnkiwebのdeckに登録する君
// @author       @ozero
// @match        *://*/*
// @grant       GM.setValue
// @grant       GM.getValue
// ==/UserScript==

'use strict';

/*
# Mouse Dictionary で引いた結果をAnkiwebのdeckに登録する君

## セットアップ

1. 拡張 Mouse Dictionary のオプションを開く
2. 設定を開く
3. 上級設定を開く
4. 項目「HTML templates」をみる
5. テンプレート「Mouse Dictionary window frame」のdivタグに、クラス名 "mouse_dictonary_helper" を足す

足した結果はこうなる

```
<div class="notranslate mouse_dictonary_helper"
     style="all:initial;
            {{systemStyles}}
            width: {{width}}px;
            height: {{height}}px;
            position: fixed;
            overflow-x: hidden;
            overflow-y: {{scroll}};
            top: 5px;
            background-color: {{backgroundColor}};
            z-index: 2147483646;
            padding: 2px 4px 2px 4px;
            border: 1px solid #A0A0A0;"
>
</div>
```

んで、設定を保存してセットアップ終わり。


## 実際の動作

1. Anki にログインする
2. ページを開いて、Mouse Dictionary 拡張を実行
3. 単語をダブルクリックで選択固定
4. Mouse Dictionary ウィンドウをクリック
5. Anki のデッキ追加ページが開いて、Mouse Dictinary で表示されていた内容がAutofillされる


*/

( () => {
    let mouse_dictonary_helper = {};

    //Mouse dictionaryウィンドウの内容をGM.setValueでGMストレージに保存
    mouse_dictonary_helper.gsetval = () => {
        let mdh_element = document.getElementsByClassName('mouse_dictonary_helper')
        if(!mdh_element[0]){
            return false;
        }
        let content = mdh_element[0].innerText;
        GM.setValue( "mdhtmp", content );
        //console.log("set mdh", content);

        //ついでにAnkiのデッキ追加ウィンドウを開く
        window.open('https://ankiuser.net/edit/', '_blank');

        return;
    };

    //Mouse dictionaryウィンドウの有無をpolling(200ms毎)してクリックイベントを追加
    const mdh_window_poll = () => {
        let mdh_element = document.getElementsByClassName('mouse_dictonary_helper')
        if(mdh_element[0]){
            mdh_element[0].addEventListener('click', mouse_dictonary_helper.gsetval, false);
            console.log("mdh addEventListener");
            return true;
        }else{
            setTimeout(()=>{
                mdh_window_poll();
            }, 200);
            //console.log("mdh polling");
            return false;
        }
    }
    mdh_window_poll();//Pollingの起動

} )();

//Ankiのデッキ追加URLを開いた際の入力欄autofill
(async function(){
    if(window.location.href === "https://ankiuser.net/edit/"){
        const mdhtmp = await GM.getValue( "mdhtmp", "" );
        if(mdhtmp === ""){
            return;
        }
        //console.log("get mdh", mdhtmp);

        //
        const mdhtmp_2 = mdhtmp.split("\n\n");
        const mdhtmp_3 = mdhtmp_2.shift().split("\n");
        const head = mdhtmp_3.shift();
        const body = mdhtmp_3.join("\n");

        let el_front = document.getElementById("f0");
        el_front.innerText = head;

        let el_back = document.getElementById("f1");
        el_back.innerText = body;
    }
})();

//console.log("mdh loaded");