Twitter X Title

Change Twitter X Title

当前为 2023-07-30 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Twitter X Title
// @namespace   TwitterX
// @match       https://twitter.com/*
// @grant       none
// @version     0.1.1
// @author      CY Fung
// @description Change Twitter X Title
// @run-at      document-start
// @license     MIT
// ==/UserScript==


(function () {
  'use strict';

  function customTitle (p){
    let q = p;
    if (p.trim() === 'X') q = p.replace('X', 'Twitter');
    else if (p.endsWith(' / X')) q = p.substring(0, p.length - ' / X'.length) + ' / Twitter';
    return q;
  }

  const map = new Map();

  function fixTitle() {

    let p = document.title;
    if (!p) return;

    let q = map.get(p)
    if (q) {
      if (p !== q) {
        document.title = q;
      }
      return;
    }

    q = customTitle(p);

    if (map.size > 96) map.clear();
    map.set(p, q)

    if (p !== q) {
      map.set(q, q)
      document.title = q;
    }

  }


  function handleTitleChange(mutationsList) {
    let b = false;
    for (const mutation of mutationsList) {
      if (mutation.type === 'childList') {
        // Title has changed, do something here
        b = true;
        break;
      }
    }

    if (b) {
      // console.log('Title changed:', document.title);
      fixTitle();
    }
  }
  
  let observer = null;

  function doActionFn() {

    // Check if the title element has appeared
    const titleElement = document.querySelector('title');
    if (titleElement) {
      // Title element found, stop observing
      if(observer) observer.disconnect();

      // Now, create a new observer to monitor the title changes
      const titleObserver = new MutationObserver(handleTitleChange);

      // Configure the observer to monitor the title text changes
      const config = { childList: true, subtree: true };

      // Start observing the title element
      titleObserver.observe(titleElement, config);

      // Log the initial title
      // console.log('Initial Title:', titleElement.textContent);
      fixTitle()
    }

  }

  // Function to handle the title changes
  function mutCallback(mutationsList, observer) {
    let doAction = false;
    for (const mutation of mutationsList) {
      if (mutation.type === 'childList') {
        doAction = true;
        break;
      }
    }
    if (doAction) doActionFn();
  }

  if( document.querySelector('title')){

    doActionFn();

  }else{


    // Create a new MutationObserver to monitor the document for the title element
    observer = new MutationObserver(mutCallback);

    // Configure the observer to monitor childList changes (new elements)
    const config = { childList: true, subtree: true };

    // Start observing the document
    observer.observe(document, config);

  }

})();