WaniKani Cursor Behavior

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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');
}());