Greasy Fork镜像 支持简体中文。

Enhanced Pinterest Page Title with Username and Alt Text

Replace the page title with the user profile name, username, post title and auto alt text on Pinterest

// ==UserScript==
// @name         Enhanced Pinterest Page Title with Username and Alt Text
// @version      3.2
// @description  Replace the page title with the user profile name, username, post title and auto alt text on Pinterest
// @author       wolffgang
// @match        *://*.pinterest.com/*
// @grant        none
// @namespace 
// ==/UserScript==

(function() {
    'use strict';

    // Function to recursively search for a key in nested objects
    function findKey(obj, targetKey) {
        if (typeof obj !== 'object' || obj === null) return undefined;
        if (obj.hasOwnProperty(targetKey)) return obj[targetKey];
        for (const key of Object.keys(obj)) {
            const result = findKey(obj[key], targetKey);
            if (result !== undefined) return result;
        }
        return undefined;
    }

    // Function to remove emojis from a string
function removeEmojis(text) {
    if (!text) return text;

    // Comprehensive regex to match almost all emojis
    const emojiRegex = /[\u{1F300}-\u{1F5FF}]|[\u{1F600}-\u{1F64F}]|[\u{1F680}-\u{1F6FF}]|[\u{1F700}-\u{1F77F}]|[\u{1F780}-\u{1F7FF}]|[\u{1F800}-\u{1F8FF}]|[\u{1F900}-\u{1F9FF}]|[\u{1FA00}-\u{1FA6F}]|[\u{1FA70}-\u{1FAFF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]|[\u{2300}-\u{23FF}]|[\u{2B50}]|[\u{2B55}]|[\u{200D}]|[\u{FE0F}]/gu;

    // Remove emojis
    let cleanedText = text.replace(emojiRegex, '');

    // Remove zero-width characters and variation selectors
    const invisibleCharsRegex = /[\u200B-\u200D\uFEFF\u00AD\u2060\u200C\u200E\u200F\u202A-\u202E]/gu;
    cleanedText = cleanedText.replace(invisibleCharsRegex, '');

    return cleanedText;
}

    function updateTitle() {
        const scriptElement = document.getElementById('__PWS_INITIAL_PROPS__');
        if (!scriptElement) return;

        try {
            const data = JSON.parse(scriptElement.textContent);

            // Directly access the native_creator object
            const nativecreator = data.initialReduxState?.pins?.[Object.keys(data.initialReduxState.pins)[0]]?.native_creator;

            let fullName, username;

            if (nativecreator) {
                fullName = nativecreator.full_name;
                username = nativecreator.username;
            }

            // Remove emojis from the fullName
            if (fullName) {
                fullName = removeEmojis(fullName);
            }

            const closeupTitle = data.initialReduxState?.pins?.[Object.keys(data.initialReduxState.pins)[0]]?.closeup_unified_title;
            const altText = data.initialReduxState?.pins?.[Object.keys(data.initialReduxState.pins)[0]]?.auto_alt_text;

            // Build title components
            const titleParts = [];
            if (fullName && username) titleParts.push(`${fullName} (${username})`);
            if (closeupTitle) titleParts.push(closeupTitle);
            if (altText) titleParts.push(altText);

            // Update document title if we have any components
            if (titleParts.length > 0) {
                let newTitle = titleParts.join(' - ');
                document.title = newTitle;
            }
        } catch (error) {
            console.error('Error updating Pinterest title:', error);
        }
    }

    // MutationObserver to detect when the JSON script element is added
    const observer = new MutationObserver((mutations, obs) => {
        if (document.getElementById('__PWS_INITIAL_PROPS__')) {
            updateTitle();
            obs.disconnect(); // Stop observing once we've found the element
        }
    });

    // Start observing the document body for changes
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Try immediately in case the element is already present
    if (document.getElementById('__PWS_INITIAL_PROPS__')) {
        updateTitle();
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址