Fix bullshit upload timeout in Matrix Element

10/11/2025, 9:48:28 PM

当前为 2025-10-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Fix bullshit upload timeout in Matrix Element
// @namespace   Violentmonkey Scripts
// @match       *://app.element.io/*
// @match       *://app.schildi.chat/*
// @match       *://chat.das-labor.org/*
// @match       *://element.envs.net/*
// @match       *://matrix.dropnet.ch/*
// @match       *://element.gnulinux.club/*
// @match       *://element.nope.chat/*
// @match       *://chat.bark.lgbt/*
// @match       *://chat.socialnetwork24.com/*
// @match       *://chat.tchncs.de/*
// @match       *://element.hot-chilli.im/*
// @match       *://chat.pub.solar/*
// @match       *://matrix.glasgow.social/*
// @match       *://matrix.furryrefuge.com/*
// @match       *://element.catgirl.cloud/*
// @match       *://element.private.coffee/*
// @match       *://element.tedomum.fr/*
// @match       *://app.nitro.chat/*
// @match       *://element.arcticfoxes.net/*
// @match       *://chat.archaeo.social/*
// @match       *://element.fachschaften.org/*
// @match       *://*/*
// @grant       none
// @version     1.0.2
// @author      ainofeet
// @description 10/11/2025, 9:48:28 PM
// @license     GPLv2
// ==/UserScript==

(function() {
  let initRetries = 0;
  window.initJob = 0;
  let patchFn = function() {
    if (initRetries > 30) clearInterval(window.initJob);
    if(!window.matrixChat?.stores?.client?.http?.uploadContent) { initRetries++; return; };
    window.origUploadContent = matrixChat.stores.client.http.uploadContent;
    window.origDateNow = Date.now;
    window.newTimeout = 3000000;
    window.matrixChat.stores.client.http.uploadContent = new Proxy(
      window.matrixChat.stores.client.http.uploadContent, {
        apply(thisArg, ...args) {
          Date.now = window.origDateNow === Date.now ? () => {
            let stack = new Error().stack.toString().split(/\n +at /);
            let applyOffset = (stack[3]?.includes(".uploadContent") &&
              stack[7]?.includes(".sendContentToRoom")) || stack?.some((
              i) => i.includes(".upload.onprogress"));
            const actualNow = window.origDateNow();
            const hardcodedShit = 30000;
            const offset = window.newTimeout - hardcodedShit;
            const patchedNow = actualNow + offset;
            if(window.debugPatch && applyOffset) console.trace(
              "Applying patched Date.now");
            return applyOffset ? patchedNow : actualNow;
          } : Date.now;
          return thisArg.apply(...args);
        }
      });
  };
  window.initJob = setInterval(patchFn, 1000);
})();