Facebook PageTitle updater

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

目前为 2025-02-12 提交的版本。查看 最新版本

// ==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();
    });
})();