WaniKani Cursor Behavior

change cursor behavior so that it doesn't jump to the end when editing kana

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          WaniKani Cursor Behavior
// @namespace     https://www.wanikani.com
// @description   change cursor behavior so that it doesn't jump to the end when editing kana
// @version       0.1.0
// @include       https://www.wanikani.com/review/session
// @include       https://www.wanikani.com/lesson/session
// @include       http://www.wanikani.com/review/session
// @include       http://www.wanikani.com/lesson/session
// @run-at        document-end
// @grant         none
// ==/UserScript==

/*global console, wanakana*/

/*
source very slightly modified from:
https://github.com/WaniKani/WanaKana
License: The MIT License (MIT)
*/

(function () {
    'use strict';
    wanakana._onInput = function (event) {
        var input, newText, normalizedInputString, range, startingCursor, startingLength;
        input = event.target;
        startingCursor = input.selectionStart;
        startingLength = input.value.length;
        normalizedInputString = wanakana._convertFullwidthCharsToASCII(input.value);
        newText = wanakana.toKana(normalizedInputString, {
            IMEMode: true
        });
        if (normalizedInputString !== newText) {
            input.value = newText;
            if (typeof input.selectionStart === "number") {
                // input.selectionStart = input.selectionEnd = input.value.length; // original line
                input.selectionStart = input.selectionEnd = startingCursor + input.value.length - startingLength;
            } else if (typeof input.createTextRange !== "undefined") {
                input.focus();
                range = input.createTextRange();
                range.collapse(false);
                range.select();
            }
        }
    };
    console.log('WaniKani Cursor Behavior: script load end');
}());