Discourse: Disable Touch Detection

Overrides browser APIs to disable touch-based UI adjustments in Discourse forums.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Discourse: Disable Touch Detection
// @namespace   https://musicbrainz.org/user/chaban
// @version     1.0
// @description Overrides browser APIs to disable touch-based UI adjustments in Discourse forums.
// @tag         ai-created
// @author      chaban
// @license     MIT
// @match       https://community.metabrainz.org/*
// @run-at      document-start
// @grant       none
// ==/UserScript==

const SCRIPT_NAME = GM.info.script.name;

(function() {
    'use strict';

    // --- Shim 1: Modern Touch Detection (any-pointer: coarse) ---
    // This is the primary method used in recent Discourse versions.

    const TARGETED_QUERY = '(any-pointer: coarse)';
    const originalMatchMedia = window.matchMedia;

    window.matchMedia = function(query) {
        if (query === TARGETED_QUERY) {
            console.log(`[${SCRIPT_NAME}] Spoofing result for modern touch query: '${query}'`);
            return {
                matches: false,
                media: query,
                addListener: () => {},
                removeListener: () => {},
                addEventListener: () => {},
                removeEventListener: () => {},
                dispatchEvent: () => true,
            };
        }
        return originalMatchMedia.call(this, query);
    };

    // --- Shim 2: Legacy Touch Detection (maxTouchPoints & ontouchstart) ---
    // This provides backward compatibility for older Discourse versions.

    try {
        Object.defineProperty(navigator, 'maxTouchPoints', {
            get: () => 0,
            configurable: true,
        });

        if ('ontouchstart' in window) {
            delete window.ontouchstart;
        }

        console.log(`[${SCRIPT_NAME}] Applied legacy touch detection shims (maxTouchPoints, ontouchstart).`);

    } catch (e) {
        console.error(`[${SCRIPT_NAME}] Failed to apply legacy shims.`, e);
    }

})();