Xbox Cloud Gaming Vibration

Add game force feedback (vibration or rumble) support for Xbox Cloud Gaming

// ==UserScript==
// @name                 Xbox Cloud Gaming Vibration
// @name:zh-CN           Xbox Cloud Gaming 游戏振动支持
// @name:zh-TW           Xbox Cloud Gaming 游戲振動支持
// @namespace            http://tampermonkey.net/
// @version              1.4
// @description          Add game force feedback (vibration or rumble) support for Xbox Cloud Gaming
// @description:zh-CN    让 Xbox Cloud Gaming 支持游戏力反馈(振动)功能
// @description:zh-TW    將 Xbox Cloud Gaming 支援游戲力回饋(振動)功能
// @author               TGSAN
// @match                https://www.xbox.com/*/play*
// @icon                 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAYAAAA9zQYyAAAAAXNSR0IArs4c6QAAHL9JREFUeJztnXl0VEW+gL/b3UmHJJ2E7EsTAklISEgwASSiEnRcIBhEFhkdGZQZl/f0zChHPb43M47nKAyj89wGFZBhUeSpA6g8RCUQHFAiBAHDKkEgISEhZN86vd73RyatTTpb0zHkdn3n5By6b1Xldzsft+vWrfqVFPTHIBmBQCGoBjoAgcCdCKEFikIILVAUQmiBohBCCxSFEFqgKITQAkUhhBYoCiG0QFEIoQWKQggtUBRCaIGiEEILFIUQWqAohNACRSGEFigKIbRAUQihBYpCCC1QFEJogaIQQgsUhRBaoCiE0AJFIYQWKAohtEBRCKEFikIILVAUQmiBohBCCxSFEFqgKITQAkUhhBYoCiG0QFFoBjoAJaGSVIT5hREfGo8+UE+4fzgBPgFo1VpUKhUmiwmD2UB1SzUXmy9ypuYM5+rOYbQYBzp0xSCEdgMR/hHMu2Yed425i/iQeDRqDWqVGrWkRpIkJCR7WVmWscpWbLINi81CU1sTO4p3sOHgBgpKCwbwLJSBJPZYcY2EkARuHHkjuSm5XDf8Ony8fK6oPVmWOVd7jq0ntpJ3Ko8DZQcwmA1uitZzEEL3kRDfEJ6a8hR3pNxBuH84Xmovt7YvyzKNbY0crTzKX/L/wtfnvnZr+0pH7TPZ57mBDmKwkBGdwVNTni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Be4WG9j/qX3f9lclvTCb/dD7+3v5cE30NGTEZ6IP0VDRU8ELeC8xaN8ue7jYrNotIXSQmi4mPj37cZdsdeTpih8aSm5LbYyxrCtfQYGjgWOUxdp/Z7Z4T7IGKxgpWFKzodksNGZlV+1dx01s38cmxT9BqtKRHpZOpzyQuOI7allpe3/M6GS9nXPETzasBRaUxCBoSRLh/OBpJQ52hjsqmyn7rxw5WdFodEboItGqtfWMhJX1Gg7rLcTn1hvpB/2Cgv2kyNnWbnH2wM6i7HALB5QihBYpCCC1QFEJogaIQQgsUhRBaoCiE0AJFIYQWKAohtEBRCKEFikIILVAUQmiBohBCCxSFEFqgKITQAkUhhBYoCiG0QFEIoQWKQggtUBRCaIGiEEILFIUQWqAohNACRSGEFigKIbRAUQihBYpCCC1QFEJogaIQQgsUhRBaoCiE0AJFIYQWKAohtEBR/D9KW2eQ2DFS5wAAAABJRU5ErkJggg==
// @inject-into          page
// @run-at               document-start
// @grant                unsafeWindow
// @grant                GM_setValue
// @grant                GM_getValue
// @grant                GM_registerMenuCommand
// @grant                GM_unregisterMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    const useControllerVibration = true;
    const useMobileVibration = true;
    const lang = navigator.language.toLowerCase();

    let windowCtx = self.window;
    if (self.unsafeWindow) {
        console.log("[Xbox Cloud Gaming Vibration] use unsafeWindow mode");
        windowCtx = self.unsafeWindow;
    } else {
        console.log("[Xbox Cloud Gaming Vibration] use window mode (your userscript extensions not support unsafeWindow)");
    }

    let configList = {
        "XCLOUD_HAPTIC_IMPULSE_TRIGGERS_EMU": {
            "desc": {
                "en": "Impulse Triggers Haptic Emulation",
                "zh": "脈衝發射鍵觸覺回饋仿真",
                "zh-cn": "脉冲扳机触感反馈模拟",
            },
            "value": "1"
        },
        "XCLOUD_HAPTIC_CONTROLLER_ENABLE": {
            "desc": {
                "en": "Gamepad Haptic ",
                "zh": "游戲控制器觸覺回饋",
                "zh-cn": "游戏控制器触感反馈",
            },
            "value": "1"
        },
        "XCLOUD_HAPTIC_DEVICE_ENABLE": {
            "desc": {
                "en": "Device Haptic (Tablet or Mobile)",
                "zh": "裝置觸覺回饋(平板電腦或手機)",
                "zh-cn": "设备触感反馈(平板电脑或手机)",
            },
            "value": "1"
        },
        "XCLOUD_HAPTIC_DEVICE_AUTO_DISABLE": {
            "desc": {
                "en": "Disable Device Haptic When Using Gamepad",
                "zh": "使用游戲控制器時停用裝置觸覺回饋",
                "zh-cn": "使用游戏控制器时禁用设备触感反馈",
            },
            "value": "1"
        }
    }
    let menuItemList = [];

    function checkSelected(key) {
        let value = GM_getValue(key);
        if (value === undefined) {
            GM_setValue(key, configList[key].value);
        }
        return value == "1";
    }

    function registerSwitchMenuItem(key) {
        let configItem = configList[key];
        let name = configItem["desc"]["en"];
        let blurMatch = configItem["desc"][lang.substr(0, 2)];
        let match = configItem["desc"][lang];
        if (match) {
            name = match;
        } else if (blurMatch) {
            name = blurMatch;
        }
        let isSelected = checkSelected(key);
        return GM_registerMenuCommand((isSelected ? "✅" : "🔲") + " " + name, function() {
            GM_setValue(key, isSelected ? "0" : "1");
            loadAndUpdateSwitchMenuItem();
        });
    }

    async function loadAndUpdateSwitchMenuItem() {
        for(let command of menuItemList) {
            await GM_unregisterMenuCommand(command);
        }
        menuItemList = [];
        let configKeys = Object.keys(configList);
        for(let configKey of configKeys) {
            configList[configKey].value = checkSelected(configKey) ? "1" : "0";
            menuItemList.push(await registerSwitchMenuItem(configKey));
        }
        // Apply
        haptic.enableControllerHaptic = checkSelected("XCLOUD_HAPTIC_CONTROLLER_ENABLE");
        haptic.enableDeviceHaptic = checkSelected("XCLOUD_HAPTIC_DEVICE_ENABLE");
        haptic.alwaysEnableDeviceHaptic = !checkSelected("XCLOUD_HAPTIC_DEVICE_AUTO_DISABLE");
    }

    let haptic = null;
    const xinputMaxHaptic = 65535;

    windowCtx.RTCPeerConnection.prototype.originalCreateDataChannelXCGV = windowCtx.RTCPeerConnection.prototype.createDataChannel;
    windowCtx.RTCPeerConnection.prototype.createDataChannel = function (...params) {
        let dc = this.originalCreateDataChannelXCGV(...params);
        if (dc.label == "input") {
            dc.addEventListener("message", function (de) {
                if (typeof(de.data) == "object") {
                    let dataBytes = new Uint8Array(de.data);
                    if (dataBytes[0] == 128) {
                        const leftM = dataBytes[3] / 255;
                        const rightM = dataBytes[4] / 255;
                        const leftT = dataBytes[5] / 255;
                        const rightT = dataBytes[6] / 255;
                        let wLeftMotorSpeed = leftM * xinputMaxHaptic;
                        let wRightMotorSpeed = rightM * xinputMaxHaptic;
                        if (checkSelected("XCLOUD_HAPTIC_IMPULSE_TRIGGERS_EMU")) {
                            wRightMotorSpeed = Math.max(wRightMotorSpeed, leftT * xinputMaxHaptic, rightT * xinputMaxHaptic);
                        }
                        if (haptic) {
                            haptic.SetState(wLeftMotorSpeed, wRightMotorSpeed);
                        }
                    }
                }
            });
            dc.addEventListener("close", function () {
                if (haptic) haptic.SetState(0, 0);
            });
        }
        return dc;
    }

    // WebHaptic.ts Compile with Webpack, using Polify, disable UglifyJS
    var __classPrivateFieldGet = this && this.__classPrivateFieldGet || function (t, e, i, a) {
        if (i === "a" && !a) throw new TypeError("Private accessor was defined without a getter");
        if (typeof e === "function" ? t !== e || !a : !e.has(t)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
        return i === "m" ? a : i === "a" ? a.call(t) : a ? a.value : e.get(t)
    };
    var __classPrivateFieldSet = this && this.__classPrivateFieldSet || function (t, e, i, a, s) {
        if (a === "m") throw new TypeError("Private method is not writable");
        if (a === "a" && !s) throw new TypeError("Private accessor was defined without a setter");
        if (typeof e === "function" ? t !== e || !s : !e.has(t)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
        return a === "a" ? s.call(t, i) : s ? s.value = i : e.set(t, i), i
    };
    var _WebHapticV2_enableControllerHaptic, _WebHapticV2_enableDeviceHaptic;
    class WebHapticV2 {
        set enableControllerHaptic(t) {
            var e;
            if (__classPrivateFieldGet(this, _WebHapticV2_enableControllerHaptic, "f") != t) {
                __classPrivateFieldSet(this, _WebHapticV2_enableControllerHaptic, t, "f");
                if (t) {
                    this.controllerHaptic = new WebControllerHaptic
                } else {
                    (e = this.controllerHaptic) === null || e === void 0 ? void 0 : e.Dispose();
                    this.controllerHaptic = undefined
                }
            }
        }
        get enableControllerHaptic() {
            return __classPrivateFieldGet(this, _WebHapticV2_enableControllerHaptic, "f")
        }
        set enableDeviceHaptic(t) {
            var e;
            if (__classPrivateFieldGet(this, _WebHapticV2_enableDeviceHaptic, "f") != t) {
                __classPrivateFieldSet(this, _WebHapticV2_enableDeviceHaptic, t, "f");
                if (t) {
                    this.deviceHaptic = new WebDeviceHaptic
                } else {
                    (e = this.deviceHaptic) === null || e === void 0 ? void 0 : e.Dispose();
                    this.deviceHaptic = undefined
                }
            }
        }
        get enableDeviceHaptic() {
            return __classPrivateFieldGet(this, _WebHapticV2_enableDeviceHaptic, "f")
        }
        constructor(t = 0) {
            _WebHapticV2_enableControllerHaptic.set(this, false);
            _WebHapticV2_enableDeviceHaptic.set(this, false);
            this.alwaysEnableDeviceHaptic = false;
            this.updateTimeoutMs = t;
            this.enableDeviceHaptic = false;
            this.enableControllerHaptic = false
        }
        SetState(t, e) {
            if (this.updateTimeoutId) {
                clearTimeout(this.updateTimeoutId)
            }
            let i = false;
            if (this.controllerHaptic !== undefined) {
                i = this.controllerHaptic.GetHapticGamepadsCount() > 0;
                this.controllerHaptic.SetState(t, e)
            }
            if (this.deviceHaptic !== undefined) {
                if (this.alwaysEnableDeviceHaptic || !i) {
                    this.deviceHaptic.SetState(t, e)
                } else {
                    this.deviceHaptic.SetState(0, 0)
                }
            }
            if (this.updateTimeoutMs > 0) {
                if (t > 0 || e > 0) {
                    this.updateTimeoutId = setTimeout(() => {
                        this.updateTimeoutId = undefined;
                        this.SetState(0, 0)
                    }, this.updateTimeoutMs)
                }
            }
        }
        Dispose() {
            this.SetState(0, 0);
            this.enableControllerHaptic = false;
            this.enableDeviceHaptic = false
        }
    }
    _WebHapticV2_enableControllerHaptic = new WeakMap, _WebHapticV2_enableDeviceHaptic = new WeakMap;
    class WebDeviceHaptic {
        constructor() {
            this.tickSliceCount = 100;
            this.tickSliceMs = 10;
            this.rangeTirm = 8;
            this.supportDeviceHaptic = false;
            this.pwmTerminateTick = 0;
            this.supportDeviceHaptic = WebDeviceHaptic.IsSupport()
        }
        Dispose() {
            this.SetState(0, 0)
        }
        SetState(t, e) {
            this.SetWebHapticState(t, e)
        }
        getAdvancedVibrateMotorPercent(t) {
            const e = .75;
            const i = -.1;
            const a = 1 / (e + i * t);
            return Math.pow(t, a)
        }
        SetWebHapticState(a, s) {
            if (this.supportDeviceHaptic) {
                let t = .5;
                let e = 65535;
                let i = Math.max(a, s * t);
                if (i > 0) {
                    let t = this.getAdvancedVibrateMotorPercent(i / e);
                    this.pwmTerminateTick = Math.round(this.tickSliceCount / this.rangeTirm * t);
                    const n = this.tickSliceCount * this.tickSliceMs * this.rangeTirm;
                    if (this.hapticPwmIntervalId === undefined) {
                        let t = 0;
                        this.hapticPwmIntervalId = setInterval(() => {
                            if (t == 0) {
                                window.navigator.vibrate(n)
                            }
                            if (t < this.pwmTerminateTick) {
                                t++
                            } else {
                                t = 0
                            }
                        }, this.tickSliceMs)
                    }
                } else {
                    if (this.hapticPwmIntervalId !== undefined) {
                        clearInterval(this.hapticPwmIntervalId);
                        this.hapticPwmIntervalId = undefined
                    }
                    window.navigator.vibrate(0)
                }
            }
        }
        static IsSupport() {
            if (!!window.navigator.vibrate) {
                return true
            } else {
                return false
            }
        }
    }
    class WebControllerHaptic {
        constructor() {
            this.magnitudeDurationMs = 1e3;
            this.supportControllerHaptic = false;
            this.gamepads = [];
            this.hapticGamepadsCount = 0;
            this.supportControllerHaptic = WebControllerHaptic.IsSupport();
            this.onGamepadConnected = t => {
                console.log("A gamepad was connected:" + t.gamepad.id);
                this.UpdateGamepads()
            };
            this.onGamepadDisonnected = t => {
                console.log("A gamepad was disconnected:" + t.gamepad.id);
                this.UpdateGamepads()
            };
            if (this.supportControllerHaptic) {
                window.addEventListener("gamepadconnected", this.onGamepadConnected);
                window.addEventListener("gamepaddisconnected", this.onGamepadDisonnected);
                this.UpdateGamepads()
            }
        }
        GetHapticGamepadsCount() {
            return this.hapticGamepadsCount
        }
        Dispose() {
            this.SetState(0, 0);
            if (this.supportControllerHaptic) {
                window.removeEventListener("gamepadconnected", this.onGamepadConnected);
                window.removeEventListener("gamepaddisconnected", this.onGamepadDisonnected)
            }
        }
        SetState(t, e) {
            this.SetControllerState(t, e)
        }
        SetControllerState(a, s) {
            var n, o, r;
            if (this.hapticTimeoutId != undefined) {
                clearTimeout(this.hapticTimeoutId);
                this.hapticTimeoutId = undefined
            }
            if (this.supportControllerHaptic) {
                let t = 65535;
                let e = a / t;
                let i = s / t;
                for (const [c, l] of Object.entries(this.gamepads)) {
                    if (l != null) {
                        (n = l === null || l === void 0 ? void 0 : l.vibrationActuator) === null || n === void 0 ? void 0 : n.playEffect("dual-rumble", {
                            duration: this.magnitudeDurationMs,
                            strongMagnitude: e,
                            weakMagnitude: i
                        });
                        if (l.hapticActuators != null) {
                            (o = l.hapticActuators[0]) === null || o === void 0 ? void 0 : o.pulse(e, this.magnitudeDurationMs);
                            (r = l.hapticActuators[1]) === null || r === void 0 ? void 0 : r.pulse(i, this.magnitudeDurationMs)
                        }
                    }
                }
                if (a > 0 || s > 0) {
                    this.hapticTimeoutId = setTimeout(() => {
                        this.hapticTimeoutId = undefined;
                        this.SetControllerState(a, s)
                    }, this.magnitudeDurationMs + 15)
                }
            }
        }
        UpdateGamepads() {
            this.gamepads = navigator.getGamepads();
            let e = 0;
            this.gamepads.forEach(t => {
                if (t != null) {
                    if (t.vibrationActuator != null) {
                        e++
                    } else if (t.hapticActuators != null && t.hapticActuators.length > 0) {
                        e++
                    }
                }
            });
            this.hapticGamepadsCount = e
        }
        static IsSupport() {
            var t, e, i, a;
            if (!!window.Gamepad && (((e = (t = window.GamepadHapticActuator) === null || t === void 0 ? void 0 : t.prototype) === null || e === void 0 ? void 0 : e.hasOwnProperty("playEffect")) || ((a = (i = window.GamepadHapticActuator) === null || i === void 0 ? void 0 : i.prototype) === null || a === void 0 ? void 0 : a.hasOwnProperty("pulse")))) {
                return true
            } else {
                return false
            }
        }
    }

    windowCtx.xcloudHaptic = new WebHapticV2();
    haptic = windowCtx.xcloudHaptic;

    loadAndUpdateSwitchMenuItem();
})();