Pinterest - Enhanced 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

目前為 2025-03-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Pinterest - Enhanced Page Title with Username and Alt Text
// @version      2.8
// @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 updateTitle() {
    const scriptElement = document.getElementById('__PWS_INITIAL_PROPS__');
    if (!scriptElement) return;

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

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

        let fullName, username;

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

        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) {
            document.title = titleParts.join(' - ');
        }
    } 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或关注我们的公众号极客氢云获取最新地址