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

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

// ==UserScript==
// @name         Enhanced Pinterest Page Title with Username and Alt Text
// @version      3.1
// @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(str) {
        const emojiRegex = /[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\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{1FB00}-\u{1FBFF}\u{1FC00}-\u{1FCFF}\u{1FD00}-\u{1FDFF}\u{1FE00}-\u{1FEFF}\u{1FF00}-\u{1FFFF}]/gu;
        return str.replace(emojiRegex, '');
    }

    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) {
                let newTitle = titleParts.join(' - ');
                // Remove emojis from the title
                newTitle = removeEmojis(newTitle);
                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或关注我们的公众号极客氢云获取最新地址