Memrise Dynamic Font for Kanji v3

Force the Memrise website to change font dynamicly in choice boxes.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Memrise Dynamic Font for Kanji v3
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Force the Memrise website to change font dynamicly in choice boxes.
// @author       superpawko
// @match        *://www.memrise.com/course/*
// @grant        none
// ==/UserScript==

// Add some fancy fonts first.
function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}
addGlobalStyle('@import url("https://fonts.googleapis.com/css?family=Lato|Playfair+Display")');



$(document).ready(function() {
    MEMRISE.garden.boxes.load = (function() {
        var cached_function = MEMRISE.garden.boxes.load;
        return function() {
            MEMRISE.garden.boxes.activate_box = (function() {
                var cached_function = MEMRISE.garden.boxes.activate_box;
                return function() {
                    var result = cached_function.apply(this, arguments);

                    $("#boxes > div > ol > li").each(function( index ) {

                        var $index = index + 1;
                        var $quote = $("#boxes > div > ol > li:nth-child("+ $index +") > span.val");
                        var $questionTrimed = $.trim($quote.text());
                        var $numWords = $questionTrimed.split(" ").length;

                        var $questionCharNum = $questionTrimed.length;
                        var qsize = (3.3 - Math.sqrt($questionCharNum) * 0.4) * (1.2 - $numWords * 0.1); // Function for text size in REM
                        //console.log("------------ FONT CHANGED");
                        //console.log("idx: " + index + " qtrimmed: " + $questionTrimed + " qtrimmedLENGTH: " + $questionCharNum + " numWords: " + $numWords + " ftsize: " + qsize);

                        if (($numWords <= 4) && ($questionCharNum < 28)) {

                            $("#boxes > div > ol > li:nth-child(" + $index + ") > span.val").css({
                                fontSize: qsize +"rem",
                                'line-height': qsize +"rem"
                            });
                        } else {
                            $("#boxes > div > ol > li").css({
                                'float': 'left',
                                'width': '570px'
                            });
                        }

                        return result;

                    });
                };
            }());

            return cached_function.apply(this, arguments);
        };
    }());
});