FBTitle Fix

Replaces generic Facebook tab title with the actual page or group name

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         FBTitle Fix
// @namespace    https://github.com/2u841r/FBTitle-Fix
// @version      1.0.0
// @description  Replaces generic Facebook tab title with the actual page or group name
// @author       Zubair Ibn Zamir
// @license      MIT 
// @match        *://*.facebook.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  function getPageName() {
    const h1btn = document.querySelector('h1 div[role="button"]');
    if (h1btn) {
      const text = h1btn.childNodes[0]?.textContent?.trim();
      if (text) return text;
    }
    const h1 = document.querySelector('h1');
    if (h1) {
      const text = h1.textContent?.trim();
      if (text) return text;
    }
    return null;
  }

  function updateTitle() {
    const name = getPageName();
    if (name && !document.title.startsWith(name)) {
      document.title = name + ' | Facebook';
    }
  }

  const observer = new MutationObserver(() => updateTitle());
  observer.observe(document.body, { childList: true, subtree: true });

  const _pushState = history.pushState.bind(history);
  history.pushState = function (...args) {
    _pushState(...args);
    setTimeout(updateTitle, 500);
  };

  window.addEventListener('popstate', () => setTimeout(updateTitle, 500));

  updateTitle();
})();