// ==UserScript== // @name Imgur Custom Link Copier // @namespace http://tampermonkey.net/ // @version 0.1 // @description Add a button to copy modified imgur link to clipboard // @author Jigao Fu // @include /^https?://imgur.com/a/.*/ // @grant GM_setClipboard // ==/UserScript==
(function() { 'use strict';
// Function to create the new button functioncreateCustomButton(imageUrl) { const button = document.createElement('button'); button.type = 'button'; button.className = 'customCopyLink Button'; // Add classes for styling button.innerHTML = '<span class="Button-label">Copy custom link</span>';
// Add margin to the button to create space between the new button and existing buttons button.style.marginRight = '10px';
button.onclick = function() { // Replace the domain in the image URL const customUrl = imageUrl.replace('i.imgur.com', 'imgur.ihainan.me'); // Copy the new URL to the clipboard GM_setClipboard(customUrl); alert('Copied to clipboard: ' + customUrl); };
return button; }
// Function to add the new button next to existing "Copy link" buttons functionaddCustomButtons() { const copyLinkButtons = document.querySelectorAll('.PostContentMenu .copyLink.Button');
copyLinkButtons.forEach(button => { // Find the image URL const imageUrl = button.closest('.PostContent').querySelector('.PostContent-imageWrapper-rounded img').src; if (!button.parentElement.querySelector('.customCopyLink')) { // Create the custom button const customButton = createCustomButton(imageUrl); // Insert the custom button before the existing "Copy link" button button.before(customButton); } }); }
functionsetupObserver() { const targetNode = document.querySelector('.UploadPost-files'); if (targetNode) { addCustomButtons(); const observer = newMutationObserver(addCustomButtons); observer.observe(targetNode, { childList: true, subtree: true }); } else { console.log("Target node not found, retrying in 1 second..."); setTimeout(setupObserver, 1000); // Retry after 1 second } }