Auto-embed Instagram links.

Find Instagram links and embed them directly into a post.

当前为 2023-04-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Auto-embed Instagram links.
// @author       Joshh
// @namespace    https://tljoshh.com
// @version      0.1.2
// @description  Find Instagram links and embed them directly into a post.
// @match        *://*.websight.blue/thread/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=websight.blue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    let igLoaded = false;
    main();

    function main() {
        const postsContainer = document.querySelector('#messages');
        const posts = document.querySelectorAll('.post');

        // Look for instagram links
        for (const post of posts) {
            handlePost(post);
        };
        // Listen for instagram links in new posts added by livelinks
        startMutationObserver(postsContainer);
    }

    // Add event listener to any posts added to DOM via livelinks
    function startMutationObserver(targetNode) {
        // Options for the observer (which mutations to observe)
        const config = { childList: true };

        // Callback function to execute when mutations are observed
        const callback = (mutationList, observer) => {
            // For all mutations made to the target node, check if any nodes were added...
            for (const mutation of mutationList) {
                handleMutation(mutation);
            }
        };

        // Create an observer instance linked to the callback function
        const observer = new MutationObserver(callback);

        // Start observing the target node for configured mutations
        observer.observe(targetNode, config);
    }

    // Handle any changes made to the messages container.
    function handleMutation(mutation) {
        // For all nodes that were added, check if any where posts made by a user and add a click event listener.
        if(mutation.addedNodes.length) {
            for (const addedNode of mutation.addedNodes) {
                if (!addedNode.tagName) continue; // Not an element
                if(addedNode.classList.contains('post')) {
                    handlePost(addedNode);
                }
            }
            if(typeof instgrm !== 'undefined' && typeof instgrm.Embeds !== 'undefined') {
                instgrm.Embeds.process();
            }
        }
    }

    function handlePost(post) {
        const instagramLinks = post.querySelectorAll('.message-contents a[href*="instagram.com/p/"], .message-contents a[href*="instagram.com/reel/]');
        for (const link of instagramLinks) {
            link.style.display = "none";
            const embedURL = formatEmbedURL(link.href);
            const node = createEmbedNode(embedURL);
            link.parentNode.insertBefore(node, link.nextSibling);
        }
        if(instagramLinks.length && !igLoaded) {
            createInstagramScript();
        }
    }

    function formatEmbedURL(href) {
        const url = new URL(href);
        return url.toString().replace(url.search, '');
    }

    function createEmbedNode(embedURL) {
        const node = document.createElement('blockquote');
        node.classList.add('instagram-media');
        node.dataset.instgrmCaptioned = true;
        node.dataset.instgrmPermalink = `${embedURL}?utm_source=ig_embed&utm_campaign=loading`;
        node.dataset.instgrmVersion = 14;
        node.style = 'background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 5px 1px; max-width:540px; min-width:326px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);';
        return node;
    }

    function createInstagramScript() {
        const script = document.createElement('script');
        script.src = 'https://www.instagram.com/embed.js';
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('async', '');
        document.body.appendChild(script);
        igLoaded = true;
    }
})();