Discord Devmode Copy ID Fix (Firefox)

Fix broken Discord channel/thread/message devmode "Copy ID" buttons in Firefox.

目前為 2025-03-26 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Discord Devmode Copy ID Fix (Firefox)
// @description  Fix broken Discord channel/thread/message devmode "Copy ID" buttons in Firefox.
// @author       You
// @version      0.2
// @namespace    https://greasyfork.org/users/1376767
// @match        https://discord.com/*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
  'use strict';

  function addClickEventCopyId(node) {
    const match = node.id.match(/(\d+)$/); // id="channel-context-devmode-copy-id-123456" > extract end digits
    if (match) {
      const channelId = match[1];
      node.addEventListener(
        'click',
        function(event) { GM_setClipboard(channelId); },
        { once: true }
      );
    }
  }

  const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { if (node.nodeType === Node.ELEMENT_NODE) {
      // console.log(node);
      // Right click popup menu identifiable by <div class="clickTrapContainer_ trapClicks_">
      if (node.className && /clickTrapContainer_.*trapClicks_/.test(node.className)) {
        const menu = node;
        menu.querySelectorAll('[role="menuitem"][id*="-devmode-copy-id-"]').forEach(menuItem => {
            setTimeout(() => addClickEventCopyId(menuItem), 100);
        });
        // menu.querySelectorAll('[role="menuitem"]').forEach(menuItem => {
        //     console.log(menuItem)
        // });
      }
    }});});
  });

  observer.observe(document.body, { childList: true, subtree: true });
})();