Instagram Post Date

Adds post date to instagram feed

当前为 2025-02-23 提交的版本,查看 最新版本

// ==UserScript==
// @name    Instagram Post Date
// @namespace  http://tampermonkey.net/
// @version   1.1
// @description Adds post date to instagram feed
// @author    SH3LL
// @match   https://www.instagram.com/*
// @grant   GM_addStyle
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Aggiunge stili CSS per l'etichetta della data del post.
    GM_addStyle(`
        .post-date-label {
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 5px;
            border-radius: 5px;
            font-size: 12px;
            z-index: 9999;
        }
    `);

    // Inizializza un array per memorizzare i JSON intercettati dalle richieste di rete.
    let interceptedJsons = [];

    // Sovrascrive il metodo `send` di XMLHttpRequest per intercettare le risposte JSON.
    const originalSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function() {
        this.addEventListener('readystatechange', function() {
            // Verifica se la richiesta è completa (readyState === 4) e se l'URL contiene '/graphql/query'.
            if (this.readyState === 4 && this.responseURL.includes('/graphql/query')) {
                try {
                    // Tenta di analizzare la risposta JSON.
                    const json = JSON.parse(this.responseText);
                    // Verifica se il JSON contiene i dati necessari per i post.
                    if (json && json.data && json.data.xdt_api__v1__feed__user_timeline_graphql_connection) {
                        // Aggiunge il JSON intercettato all'array.
                        interceptedJsons.push(json);
                    }
                } catch (error) {
                    // Gestisce eventuali errori durante l'analisi del JSON.
                    console.error("Errore nel parsing JSON", error);
                }
            }
        });
        // Chiama il metodo `send` originale.
        originalSend.apply(this, arguments);
    };

    // Aggiunge un'etichetta con la data al post specificato.
    function addDateLabel(postElement, date) {
        // Verifica se l'etichetta della data non è già presente.
        if (!postElement.querySelector('.post-date-label')) {
            // Crea un elemento div per l'etichetta della data.
            const label = document.createElement('div');
            label.classList.add('post-date-label');
            label.textContent = formatDate(date * 1000); // Converte il timestamp in millisecondi.
            postElement.style.position = 'relative';
            // Aggiunge l'etichetta come figlio del primo elemento figlio del post.
            postElement.children[0].appendChild(label);
        }
    }

    // Formatta un timestamp in una stringa di data leggibile.
    function formatDate(timestamp) {
        const date = new Date(timestamp);
        const options = { year: 'numeric', month: 'long', day: 'numeric' };
        return date.toLocaleDateString('en-US', options);
    }

    // Elabora i post sulla pagina per aggiungere le etichette della data.
    function processPosts() {
        // Seleziona tutti gli elementi che rappresentano i post.
        const postElements = document.querySelectorAll('.x1lliihq.x1n2onr6.xh8yej3.x4gyw5p.x1ntc13c.x9i3mqj.x11i5rnm.x2pgyrj');
        // Itera attraverso ogni elemento del post.
        postElements.forEach(postElement => {
            // Trova l'elemento link all'interno del post.
            const linkElement = postElement.querySelector('a[href*="/p/"], a[href*="/reel/"]');
            if (linkElement) {
                // Estrae il codice del post dall'attributo href del link.
                const href = linkElement.getAttribute('href');
                const parts = href.split('/');
                const postCode = parts[3];

                // Itera attraverso i JSON intercettati per trovare il post corrispondente.
                interceptedJsons.forEach(json => {
                    if (json && json.data && json.data.xdt_api__v1__feed__user_timeline_graphql_connection && json.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges) {
                        json.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges.forEach(edge => {
                            if (edge.node.code === postCode) {
                                // Estrae il timestamp del post e aggiunge l'etichetta della data.
                                const timestamp = edge.node.taken_at;
                                addDateLabel(postElement, timestamp);
                            }
                        });
                    }
                });
            }
        });
    }

    // Esegue l'elaborazione iniziale dei post.
    processPosts();

    // Crea un osservatore di mutazioni per rilevare nuovi post aggiunti dinamicamente.
    const observer = new MutationObserver(mutations => {
        // Riesegue l'elaborazione dei post quando vengono rilevate mutazioni.
        processPosts();
    });

    // Osserva le modifiche al corpo del documento e ai suoi sottoalberi.
    observer.observe(document.body, { childList: true, subtree: true });
})();

QingJ © 2025

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