Facebook PageTitle updater

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Facebook PageTitle updater
// @version      2.0
// @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 = '';
    let titleObserver = null;

    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 && titleElement.textContent !== newTitle) {
            if (titleObserver) {
                titleObserver.disconnect();
            } else {
                titleObserver = new MutationObserver((mutations) => {
                    for (const mutation of mutations) {
                        if (mutation.type === 'childList') {
                            titleObserver.disconnect();
                            titleElement.textContent = newTitle;
                            titleObserver.observe(titleElement, { childList: true });
                        }
                    }
                });
            }
            titleObserver.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();
    });
})();