Duolingo Auto IME On-Off

Forces IME to switch between English and Japanese automatically for each typing question.

目前為 2017-11-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Duolingo Auto IME On-Off
// @namespace    mog86uk-duo-autoime
// @version      1.0
// @description  Forces IME to switch between English and Japanese automatically for each typing question.
// @author       mog86uk (aka. testmoogle)
// @match        https://www.duolingo.com/*
// @exclude      https://www.duolingo.com/discussion
// @exclude      https://www.duolingo.com/topic/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @grant        none
// @run-at       document-idle
// ==/UserScript==


jQuery.noConflict();
jQuery(document).ready(function($) {
    'use strict';

    var typeLang = "";
    var typeLangPrevious = "";

    if (typeof $('html').css('ime-mode') === 'undefined' && typeof $('html').prop('inputMode') === 'undefined') {
        alert("If you're using Chrome or Opera, you'll need to enable the following browser flag:\n\n#enable-experimental-web-platform-features\n\nFirefox should work fine as it is, but Chrome and Opera need to enable this flag to be able to make use of the 'inputmode' experimental html attribute. (See the userscript's page on greasyfork if you need more help with enabling this browser flag.) ^^");
    }

    function Start(mutationRecords) {
        if (/^https:\/\/www\.duolingo\.com.*\/practice$/.test(window.location.href) || /^https:\/\/www\.duolingo\.com\/skill\/ja\/(.)*$/.test(window.location.href)) {
            mutationRecords.forEach (function (mutation) {
                typeLang = $('textarea[data-test="challenge-translate-input"]').attr('lang');
                if (typeLang !== typeLangPrevious || (typeLang === 'en' && $('textarea[data-test="challenge-translate-input"]').css('ime-mode') === 'auto')) {
                    if (typeLang === 'en') {
                        $('textarea[data-test="challenge-translate-input"]').attr('inputmode', 'latin').css('ime-mode', 'disabled').blur().focus();
                    }
                    else if (typeLang === 'ja') {
                        $('textarea[data-test="challenge-translate-input"]').attr('inputmode', 'kana').css('ime-mode', 'active').blur().focus();
                    }
                    typeLangPrevious = typeLang;
                }
            });
        }
    }

    if (window.top == window.self) {
        var observerOfStuff = new MutationObserver(Start);

        observerOfStuff.observe(document.body, {
            attributes: true,
            subtree: true
        });
    }
});