typing-practice

15.07.2021, 22:09:13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        typing-practice
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       GM_openInTab
// @version     1.0
// @author      -
// @description 15.07.2021, 22:09:13
// ==/UserScript==

(function () {
    'use strict';

    let gText;
    function openTypingPractice(text) {
        const baseUrl = "https://10fastfingers.com/widget/typingtest";
        const params = new URLSearchParams({
            dur: "60000",
            rand: "0",
            words: text,
        });
        const url = `${baseUrl}?${params.toString()}`.replace(/\+/g, "%20"); // 10fastfingers doesn't consider "+" as space
        GM_openInTab(url);
    }
    // Alt-T
    function detectHotKey(e) {
        // Use "KeyboardEvent.code" to support the same key position regardless of keyboard layout
        return e.altKey && e.code === "KeyT";
    }
    function onKeyDown(e) {
        if (gText && detectHotKey(e)) {
            const cleanText = gText.toLowerCase().split(/\s/).join(" ");
            openTypingPractice(cleanText);
        }
    }
    function onSelectionChange() {
        // Enable hotkey only when there's a selection
        document.removeEventListener("keydown", onKeyDown);
        const selection = document.getSelection();
        gText = selection === null || selection === void 0 ? void 0 : selection.toString().trim();
        if (!gText) {
            return;
        }
        document.addEventListener("keydown", onKeyDown);
    }
    function register() {
        document.addEventListener("selectionchange", onSelectionChange);
    }
    function main() {
        register();
    }
    main();

}());