Twitter to Embed Link Converter

Convert Twitter status links to embedded tweet links

当前为 2024-07-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitter to Embed Link Converter
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Convert Twitter status links to embedded tweet links
// @author       w4t3r1ily
// @match        *://*/*
// @include      *
// @grant        none
// @icon         https://abs.twimg.com/favicons/twitter.ico

// ==/UserScript==

(function() {
    'use strict';

    // Function to create the new embed tweet link element with an arrow sign
    function createEmbedLink(tweetId) {
        const embedLink = document.createElement('a');
        embedLink.href = `https://platform.twitter.com/embed/Tweet.html?id=${tweetId}`; // Construct the embed URL using the tweet ID
        embedLink.textContent = `⇒ https://platform.twitter.com/embed/Tweet.html?id=${tweetId}`; // Set the text content of the link with an arrow sign
        return embedLink; // Return the newly created link element
    }

    // Get all anchor elements on the page
    const links = document.querySelectorAll('a');

    // Iterate over each link
    links.forEach(link => {
        // Match the URL against the specific Twitter status pattern and extract the tweet ID
        const match = link.href.match(/https:\/\/twitter\.com\/i\/status\/(\d+)/);
        if (match) {
            const tweetId = match[1]; // Extract the tweet ID from the matched pattern
            const embedLink = createEmbedLink(tweetId); // Create an embed link using the extracted tweet ID

            // Create a line break element
            const lineBreak = document.createElement('br');

            // Insert the line break and then the new link below the original link
            link.insertAdjacentElement('afterend', lineBreak); // Insert the line break after the original link
            lineBreak.insertAdjacentElement('afterend', embedLink); // Insert the new embed link after the line break
        }
    });
})();