Twitter fast clean link copy

Copy tweet links with one click and strip query parameters

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Twitter fast clean link copy
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Copy tweet links with one click and strip query parameters
// @author       cro
// @match        https://*.twitter.com/*
// @match        https://*.x.com/*
// @icon         https://www.google.com/s2/favicons?domain=twitter.com
// @grant        none
// @license      MIT
// ==/UserScript==
/* jshint esversion: 6 */

(function()
{
    'use strict';
    let id = 'cro-copy-id';

    let make_button = function()
    {
        let button = document.createElement('button');
        button.textContent = 'copy link';
        button.onclick = function ()
        {
            let url = new URL(button.closest('article').querySelector('[href*="status"]').href);
            url.pathname = url.pathname.split('/').slice(0,4).join('/');
            navigator.clipboard.writeText(url.toString());
        };
        button.id = id;
        return button;
    };

    setInterval(function()
    {
        let nodes = document.querySelectorAll('article div[role="group"]');
        for (let node of nodes)
        {
            if (!node.querySelector(`#${id}`))
            {
                node.lastChild.append(make_button());
            }
        }
    }, 250);
})();