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              0.3.0
// @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+0pH7TPZ57mBDmKwkBGdwVN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KVBe4WG9j/qX3f9lclvTCb/dD7+3v5cE30NGTEZ6IP0VDRU8ELeC8xaN8ue7jYrNotIXSQmi4mPj37cZdsdeTpih8aSm5LbYyxrCtfQYGjgWOUxdp/Z7Z4T7IGKxgpWFKzodksNGZlV+1dx01s38cmxT9BqtKRHpZOpzyQuOI7allpe3/M6GS9nXPETzasBRaUxCBoSRLh/OBpJQ52hjsqmyn7rxw5WdFodEboItGqtfWMhJX1Gg7rLcTn1hvpB/2Cgv2kyNnWbnH2wM6i7HALB5QihBYpCCC1QFEJogaIQQgsUhRBaoCiE0AJFIYQWKAohtEBRCKEFikIILVAUQmiBohBCCxSFEFqgKITQAkUhhBYoCiG0QFEIoQWKQggtUBRCaIGiEEILFIUQWqAohNACRSGEFigKIbRAUQihBYpCCC1QFEJogaIQQgsUhRBaoCiE0AJFIYQWKAohtEBR/D9KW2eQ2DFS5wAAAABJRU5ErkJggg==
// @run-at               document-start
// @grant                unsafeWindow
// ==/UserScript==

(function() {
    'use strict';

    const useControllerVibration = true;
    const useMobileVibration = true;

    let haptic = null;
    let xinputMaxHaptic = 65535;

    RTCPeerConnection.prototype.createDataChannelOriginal = RTCPeerConnection.prototype.createDataChannel;
    RTCPeerConnection.prototype.createDataChannel = function (...params) {
        let dc = this.createDataChannelOriginal(...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) {
                        let leftM = dataBytes[3] / 255;
                        let rightM = dataBytes[4] / 255;
                        let leftT = dataBytes[5] / 255;
                        let rightT = dataBytes[6] / 255;
                        if (haptic) haptic.SetState(leftM * xinputMaxHaptic, rightM * xinputMaxHaptic);
                    }
                }
            });
            dc.addEventListener("close", function () {
                if (haptic) haptic.SetState(0, 0);
            });
        }
        return dc;
    }

    // Compile with Webpack, disable UglifyJS
    class WebHaptic {
        constructor(t = !0, e = !0) {
            this.isRunning = !1,
                this.lms = 0,
                this.mp = 0,
                this.supportch = !1,
                this.enablech = !0,
                this.supportwvh = !1,
                this.enablewvh = !0,
                this.gamepads = [],
                this.enablewvh = t,
                this.enablewvh && (this.supportwvh = WebHaptic.IsSupportWebVibrateHaptic()),
                this.enablech = e,
                this.enablech && (this.supportch = WebHaptic.IsSupportControllerHaptic()),
                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() }),
                this.supportch && (window.addEventListener("gamepadconnected", this.onGamepadConnected), window.addEventListener("gamepaddisconnected", this.onGamepadDisonnected), this.UpdateGamepads())
        }
        static IsSupportControllerHaptic() {
            var t, e;
            return !!(window.Gamepad && window.GamepadHapticActuator && (null === (e = null === (t = window.GamepadHapticActuator) || void 0 === t ? void 0 : t.prototype) || void 0 === e ? void 0 : e.hasOwnProperty("playEffect")))
        }
        static IsSupportWebVibrateHaptic() {
            return !!window.navigator.vibrate
        }
        static IsSupport() {
            return WebHaptic.IsSupportControllerHaptic() || WebHaptic.IsSupportWebVibrateHaptic()
        }
        EnableControllerHaptic() {
            this.enablech = !0
        }
        DisableControllerHaptic() {
            this.enablech = !1
        }
        EnableWebVibrateHaptic() {
            this.enablewvh = !0
        }
        DisableWebVibrateHaptic() {
            this.enablewvh = !1
        }
        Dispose() {
            this.SetState(0, 0),
                this.supportch && (window.removeEventListener("gamepadconnected", this.onGamepadConnected), window.removeEventListener("gamepaddisconnected", this.onGamepadDisonnected))
        }
        SetState(t, e) {
            this.updateTimeoutId && clearTimeout(this.updateTimeoutId), (this.enablewvh || 0 == t || 0 == e) && this.SetWebHapticState(t, e), (this.enablech || 0 == t || 0 == e) && this.SetControllerState(t, e)
        }
        SetWebHapticState(t, e) {
            if (this.supportwvh) {
                let i = .5, a = 65535, o = Math.max(t, e * i);
                o != this.lms && (this.lms = o, this.mp = o / a, o > 0 ? 0 == this.isRunning && (this.isRunning = !0, this.OnVibrateTick(this)) : (this.isRunning = !1, window.navigator.vibrate(0)))
            }
        }
        SetControllerState(t, e) {
            var i, a;
            if (this.supportch) {
                let o = 65535, r = 1e3, n = t / o, s = e / o;
                for (const [t, e] of Object.entries(this.gamepads)) null != e && (null === (a = null === (i = e) || void 0 === i ? void 0 : i.vibrationActuator) || void 0 === a || a.playEffect("dual-rumble", {
                    duration: r,
                    strongMagnitude: n,
                    weakMagnitude: s
                }))
            }
        }
        UpdateGamepads() {
            this.gamepads = navigator.getGamepads()
        }
        OnVibrateTick(t) {
            t.lms > 0 && 1 == t.isRunning && (t.mp < .075 ? t.LowVibrate(t, t.mp) : this.mp < .88 ? t.MidVibrate(t, t.mp) : t.HighVibrate(t, t.mp))
        }
        HighVibrate(t, e) {
            let i = 100 * e;
            window.navigator.vibrate(i), setTimeout(() => { t.OnVibrateTick(t) }, i)
        }
        MidVibrate(t, e) {
            let i = 50 * e;
            window.navigator.vibrate(i), setTimeout(() => { t.OnVibrateTick(t) }, i)
        }
        LowVibrate(t, e) {
            let i = 100 - e / .025 * 100;
            window.navigator.vibrate(2), setTimeout(() => { t.OnVibrateTick(t) }, i)
        }
    }

    haptic = new WebHaptic(useMobileVibration, useControllerVibration);
})();