// ==UserScript==
// @name fixupx copy to clipboard button
// @namespace http://tampermonkey.net/
// @version 1.4
// @description add a copy to clipboard button to X posts that set it to fixupx.com instead of twitter.com
// @author Sickerine
// @license MIT
// @match https://x.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant none
// @downloadURL https://update.greasyfork.icu/scripts/493989/fixupx%20copy%20to%20clipboard%20button.user.js
// @updateURL https://update.greasyfork.icu/scripts/493989/fixupx%20copy%20to%20clipboard%20button.meta.js
// ==/UserScript==
(function () {
'use strict';
function apply(article) {
if (article.tagName == 'DIV')
article = article.querySelector('article') || article.closest('article');
if (article.querySelector('.customCopyButton')) return;
if (!article) return;
try {
const SVGStyle = `
width="18"
height="18"
fill="rgb(113, 118, 123)"
`
const defaultSVG = `
`
const clickedSVG = `
`
const firstIcon = article.querySelector('[aria-label="More"]');
const sixthParent = firstIcon?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement;
if (!sixthParent) return;
article.classList.add('marked');
const secondChild = sixthParent.children[1];
const newDiv = document.createElement('div');
newDiv.classList.add('customCopyButton');
newDiv.style.marginLeft = 'auto';
newDiv.style.marginRight = '4px';
newDiv.style.cursor = 'pointer';
newDiv.style.display = 'flex';
newDiv.innerHTML = defaultSVG;
newDiv.onclick = (e) => {
e.preventDefault();
let href = article.querySelector('a[href*="/status/"]').href;
href = href.replace(/\/status\/(\d+).*/, '/status/$1')
.replace('twitter.com', 'fixupx.com')
.replace('x.com', 'fixupx.com');
newDiv.innerHTML = clickedSVG;
setTimeout(() => {
newDiv.innerHTML = defaultSVG;
}, 1000);
navigator.clipboard.writeText(href).then(() => {
console.log('Copied to clipboard: ' + href);
}, (err) => {
console.error('Failed to copy to clipboard: ' + href, err);
});
};
sixthParent.insertBefore(newDiv, secondChild);
}
catch (e) {
console.error(e);
console.log(article)
}
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.tagName == 'ARTICLE' && node || node.tagName == 'DIV' && (node.querySelector('article') || node.closest('article'))) {
apply(node);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();