Facebook PageTitle updater

Extract name, location, and username from Facebook profile pages and update the title

当前为 2025-02-12 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Facebook PageTitle updater
// @version      1.8
// @description  Extract name, location, and username from Facebook profile pages and update the title
// @match        https://www.facebook.com/*
// @grant        none
// @namespace    https://greasyfork.org/users/1409709
// ==/UserScript==

(function() {
    'use strict';

    let currentProfileID = '';

    function extractInfo() {
        const nameElement = document.querySelector('h1');
        let name = nameElement ? nameElement.textContent.trim() : '';

        const livesInElement = Array.from(document.querySelectorAll('div[role="main"] span[dir="auto"]')).find(el => el.textContent.includes('Lives in'));
        let livesIn = livesInElement ? livesInElement.querySelector('a span').textContent.trim() : '';

        const fromElement = Array.from(document.querySelectorAll('div[role="main"] span[dir="auto"]')).find(el => el.textContent.includes('From'));
        let from = fromElement ? fromElement.querySelector('a span').textContent.trim() : '';

        const url = window.location.href;
        const profileIDMatch = url.match(/facebook\.com\/(?:profile\.php\?id=|([^/?&]+))/);
        let profileID = '';
        if (profileIDMatch && profileIDMatch[1]) {
            profileID = profileIDMatch[1];
        } else if (profileIDMatch && profileIDMatch[0]) {
            profileID = profileIDMatch[0];
        }

        if (profileID && (name || livesIn || from)) {
            const profileInfo = { name, livesIn, from, profileID };
            localStorage.setItem(`profileInfo_${profileID}`, JSON.stringify(profileInfo));
            currentProfileID = profileID;
            updateTitle(profileInfo);
        } else {
            const storedProfileInfo = localStorage.getItem(`profileInfo_${profileID}`);
            if (storedProfileInfo) {
                updateTitle(JSON.parse(storedProfileInfo));
            }
        }
    }

    function updateTitle(profileInfo) {
        let newTitle = '';
        if (profileInfo.name) newTitle += `${profileInfo.name}`;
        if (profileInfo.livesIn) newTitle += ` - ${profileInfo.livesIn}`;
        if (profileInfo.from) newTitle += ` (From ${profileInfo.from})`;

        const titleElement = document.querySelector('title');
        if (titleElement) {
            const observer = new MutationObserver((mutations) => {
                for (const mutation of mutations) {
                    if (mutation.type === 'childList') {
                        observer.disconnect();
                        titleElement.textContent = newTitle;
                        observer.observe(titleElement, { childList: true });
                    }
                }
            });
            observer.observe(titleElement, { childList: true });
            titleElement.textContent = newTitle;
        }
    }

    function monitorURLChanges() {
        let lastURL = window.location.href;

        setInterval(() => {
            const currentURL = window.location.href;
            if (currentURL !== lastURL) {
                lastURL = currentURL;
                const profileIDMatch = currentURL.match(/facebook\.com\/(?:profile\.php\?id=|([^/?&]+))/);
                let newProfileID = '';
                if (profileIDMatch && profileIDMatch[1]) {
                    newProfileID = profileIDMatch[1];
                } else if (profileIDMatch && profileIDMatch[0]) {
                    newProfileID = profileIDMatch[0];
                }

                if (currentProfileID !== newProfileID) {
                    currentProfileID = newProfileID;
                    extractInfo();
                } else {
                    const storedProfileInfo = localStorage.getItem(`profileInfo_${newProfileID}`);
                    if (storedProfileInfo) {
                        updateTitle(JSON.parse(storedProfileInfo));
                    }
                }
            }
        }, 1000);
    }

    window.addEventListener('load', () => {
        extractInfo();
        monitorURLChanges();
    });
})();