Greasy Fork 支持简体中文。

Instagram Post Date and Location

Adds post date and location to instagram feed

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

(function() {
    'use strict';

    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;
        }
        .post-location-label {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 5px;
            border-radius: 5px;
            font-size: 12px;
            z-index: 9999;
        }
    `);

    let interceptedJsons = [];

    const originalSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function() {
        this.addEventListener('readystatechange', function() {
            if (this.readyState === 4 && this.responseURL.includes('/graphql/query')) {
                try {
                    const json = JSON.parse(this.responseText);
                    if (json && json.data && json.data.xdt_api__v1__feed__user_timeline_graphql_connection) {
                        interceptedJsons.push(json);
                    }
                } catch (error) {
                    console.error("Errore nel parsing JSON", error);
                }
            }
        });
        originalSend.apply(this, arguments);
    };

    function addDateLabel(postElement, date) {
        if (!postElement.querySelector('.post-date-label')) {
            const label = document.createElement('div');
            label.classList.add('post-date-label');
            label.textContent = "📅"+formatDate(date * 1000);
            postElement.style.position = 'relative';
            postElement.children[0].appendChild(label);
        }
    }

    function addLocationLabel(postElement, locationName, locationPk) {
        if (locationName && !postElement.querySelector('.post-location-label')) {
            const label = document.createElement('a');
            label.classList.add('post-location-label');

            // Tronca il nome della posizione se è più lungo di 30 caratteri
            const truncatedLocationName = locationName.length > 30 ? locationName.substring(0, 30) + "..." : locationName;

            label.textContent = ` ${truncatedLocationName}`; // Aggiunge l'emoji e il nome troncato
            label.href = `https://www.instagram.com/explore/locations/${locationPk}`;
            label.target = "_blank";
            postElement.style.position = 'relative';
            postElement.children[0].appendChild(label);
        }
    }

    function formatDate(timestamp) {
        const date = new Date(timestamp);
        const options = { year: 'numeric', month: 'short', day: 'numeric' };
        return date.toLocaleDateString('en-US', options);
    }

    function processPosts() {
        const postElements = document.querySelectorAll('.x1lliihq.x1n2onr6.xh8yej3.x4gyw5p.x1ntc13c.x9i3mqj.x11i5rnm.x2pgyrj');
        postElements.forEach(postElement => {
            const linkElement = postElement.querySelector('a[href*="/p/"], a[href*="/reel/"]');
            if (linkElement) {
                const href = linkElement.getAttribute('href');
                const parts = href.split('/');
                const postCode = parts[3];

                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) {
                                const timestamp = edge.node.taken_at;
                                addDateLabel(postElement, timestamp);

                                if (edge.node.location) {
                                    const locationName = "📌" + edge.node.location.name;
                                    const locationPk = edge.node.location.pk;
                                    addLocationLabel(postElement, locationName, locationPk);
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    processPosts();

    const observer = new MutationObserver(mutations => {
        processPosts();
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();