theHandy support for FapInstructor.com

Adds support for theHandy for FapInstructor.com

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         theHandy support for FapInstructor.com
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds support for theHandy for FapInstructor.com
// @author       notSafeForDev
// @match        https://fapinstructor.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const inputText = document.createElement("input");
    inputText.type = "text";
    inputText.placeholder = "Enter connection key...";
    inputText.style.position = "absolute";
    inputText.style.top = "calc(100vh - 100px)";

    let stroking = false;
    let strokeLength = 0.4;
    let lastStrokesPerSecond = 0;

    const sendRequest = (url, onResponse) => {
        const request = new XMLHttpRequest();
        request.open("GET", url);
        request.send();

        request.onreadystatechange = () => {
            if (request.readyState == 4 && onResponse != undefined) {
                onResponse(request);
            }
        }
    }

    const lerp = (value1, value2, amount) => {
        amount = amount < 0 ? 0 : amount;
        amount = amount > 1 ? 1 : amount;
        return value1 + (value2 - value1) * amount;
    }

    const updateStrokeLength = () => {
        if (inputText.value == "") {
            return;
        }

        const api = "https://www.handyfeeling.com/api/v1/" + inputText.value;

        sendRequest(api + "/getSettings", (request) => {
            var newStrokeLength = JSON.parse(request.responseText).stroke / 100;
            if (newStrokeLength != strokeLength) {
                strokeLength = newStrokeLength;
                updateStrokeSpeed(lastStrokesPerSecond);
            }
        });
    }

    const updateStrokeSpeed = (strokesPerSecond) => {
        if (stroking == false && strokesPerSecond == 0) {
            return;
        }

        let toySpeed = Math.round(strokesPerSecond * lerp(10, 45, strokeLength));

        const api = "https://www.handyfeeling.com/api/v1/" + inputText.value;

        if (stroking == false && strokesPerSecond > 0) {
            stroking = true;
            sendRequest(api + "/setMode?mode=1", () => {
                sendRequest(api + "/setSpeed?speed=" + toySpeed + "&type=%25");
            });
        } else if (stroking == true && strokesPerSecond == 0) {
            stroking = false;
            sendRequest(api + "/setMode?mode=0");
        } else {
            sendRequest(api + "/setSpeed?speed=" + toySpeed + "&type=%25");
        }

        lastStrokesPerSecond = strokesPerSecond;
    }

    const getSpeedText = () => {
        return document.querySelector(
            `#root > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)
            > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > p:nth-child(1)`
        );
    }

    const onRequestAnimationFrame = (ms) => {
        requestAnimationFrame(onRequestAnimationFrame);

        const speedText = getSpeedText();

        if (speedText == null) {
            if (stroking == true) {
                updateStrokeSpeed(0);
            }
            return;
        }

        if (inputText.parentNode == null) {
            document.body.appendChild(inputText);
        }

        if (inputText.value == "") {
            return;
        }

        const strokesPerSecond = parseFloat(speedText.textContent);

        if (strokesPerSecond != lastStrokesPerSecond) {
            updateStrokeSpeed(strokesPerSecond);
        }
    }

    setInterval(() => {
        updateStrokeLength();
    }, 5000);

    requestAnimationFrame(onRequestAnimationFrame);
})();