Last active
March 18, 2026 09:26
-
-
Save lalibi/8069ba479069e1b2071b1fb779e4bfdc to your computer and use it in GitHub Desktop.
Copy Site Reference - Tampermonkey script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Copy Site Reference | |
| // @namespace https://lalibi.org/ | |
| // @version 0.5.0 | |
| // @description Copy title and shorten URL from site | |
| // @author lalibi | |
| // @match *://*/* | |
| // @updateURL https://gist.githubusercontent.com/lalibi/8069ba479069e1b2071b1fb779e4bfdc/raw/ | |
| // @downloadURL https://gist.githubusercontent.com/lalibi/8069ba479069e1b2071b1fb779e4bfdc/raw/ | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=tinyurl.com | |
| // @grant GM_setClipboard | |
| // @grant GM_notification | |
| // @grant GM.xmlHttpRequest | |
| // @grant GM_setValue | |
| // @grant GM_getValue | |
| // @grant GM_registerMenuCommand | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| const TOKEN_KEY = 'tinyurl_api_token'; | |
| function notify(title, text) { | |
| GM_notification({ | |
| title, | |
| text, | |
| timeout: 5000 | |
| }); | |
| } | |
| function getToken() { | |
| return GM_getValue(TOKEN_KEY, ''); | |
| } | |
| function setToken() { | |
| const current = getToken(); | |
| const token = prompt('Enter your TinyURL API token:', current || ''); | |
| if (token === null) return; // user cancelled | |
| const trimmed = token.trim(); | |
| if (!trimmed) { | |
| GM_setValue(TOKEN_KEY, ''); | |
| notify('TinyURL token cleared', 'Stored API token was removed.'); | |
| return; | |
| } | |
| GM_setValue(TOKEN_KEY, trimmed); | |
| notify('TinyURL token saved', 'Token stored locally in Tampermonkey.'); | |
| } | |
| GM_registerMenuCommand('Set TinyURL API token', setToken); | |
| GM_registerMenuCommand('Clear TinyURL API token', () => { | |
| GM_setValue(TOKEN_KEY, ''); | |
| notify('TinyURL token cleared', 'Stored API token was removed.'); | |
| }); | |
| async function createTinyUrl(longUrl) { | |
| return new Promise((resolve, reject) => { | |
| const token = getToken(); | |
| if (!token) { | |
| reject(new Error('No TinyURL API token configured. Use the Tampermonkey menu.')); | |
| return; | |
| } | |
| GM.xmlHttpRequest({ | |
| method: 'POST', | |
| url: 'https://api.tinyurl.com/create', | |
| headers: { | |
| 'Authorization': `Bearer ${token}`, | |
| 'Content-Type': 'application/json', | |
| 'Accept': 'application/json' | |
| }, | |
| data: JSON.stringify({ | |
| url: longUrl, | |
| domain: 'tinyurl.com' | |
| }), | |
| onload: (response) => { | |
| try { | |
| const json = JSON.parse(response.responseText); | |
| if (response.status >= 200 && response.status < 300 && json?.data?.tiny_url) { | |
| resolve(json.data.tiny_url); | |
| return; | |
| } | |
| const msg = | |
| (Array.isArray(json?.errors) ? json.errors.join(', ') : null) || | |
| json?.error || | |
| `HTTP ${response.status}`; | |
| reject(new Error(msg)); | |
| } catch (err) { | |
| reject(new Error(`Invalid API response: ${response.responseText}`)); | |
| } | |
| }, | |
| onerror: () => reject(new Error('Network error while contacting TinyURL API')) | |
| }); | |
| }); | |
| } | |
| document.addEventListener('keydown', async (e) => { | |
| if (e.altKey && e.shiftKey && e.key.toLowerCase() === 'r') { | |
| try { | |
| const shortUrl = await createTinyUrl(window.location.href); | |
| const text = `${document.title} - ${shortUrl}`; | |
| GM_setClipboard(text); | |
| notify('Reference copied!', text); | |
| } catch (err) { | |
| notify('TinyURL error', err.message); | |
| } | |
| } | |
| }, true); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment