Last active
January 22, 2021 13:26
-
-
Save Lifeni/e974de88b11245231e84751e1dc8673e to your computer and use it in GitHub Desktop.
Copy link in markdown format. Like this: [title](url).
This file contains 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 link in markdown format | |
// @description Copy the links on the page into a format supported by markdown, like this: [title](url) | |
// @author Lifeni | |
// @namespace https://github.com/Lifeni | |
// @include http*://* | |
// @version 0.1.2 | |
// @license MIT | |
// ==/UserScript== | |
(() => { | |
if (window.self !== window.top) { | |
return; | |
} | |
class CopyButton extends HTMLElement { | |
constructor() { | |
super(); | |
const shadow = this.attachShadow({ mode: 'closed' }); | |
const copy = document.createElement('button'); | |
copy.setAttribute('aria-label', 'Copy link in markdown format'); | |
copy.setAttribute('tab-index', '-1'); | |
copy.addEventListener('mouseover', () => { | |
copy.style.background = 'rgba(0, 0, 0, 0.8)'; | |
}); | |
copy.addEventListener('mouseout', () => { | |
copy.style.width = '6px'; | |
copy.style.background = 'rgba(0, 0, 0, 0.05)'; | |
}); | |
copy.addEventListener('click', (e) => { | |
navigator.clipboard | |
.writeText(`[${document.title}](${location.href})`) | |
.then(() => { | |
copy.style.width = '12px'; | |
copy.style.background = 'rgba(0, 200, 83 ,1)'; | |
setTimeout(() => { | |
copy.style.background = 'rgba(0, 0, 0, 0.05)'; | |
}, 2000); | |
}) | |
.catch((err) => { | |
copy.style.width = '6px'; | |
copy.style.background = 'rgba(255, 87, 34, 1)'; | |
console.log(err); | |
}); | |
}); | |
// Click the middle mouse button to hide the window | |
copy.addEventListener('mousedown', (e) => { | |
if (e.button === 1) { | |
copy.style.display = 'none'; | |
} | |
}); | |
copy.style.position = "fixed"; | |
copy.style.zIndex = "999999"; | |
copy.style.width = "6px"; | |
copy.style.height = "24px"; | |
copy.style.padding = "0"; | |
copy.style.margin = "0"; | |
copy.style.left = "0"; | |
copy.style.top = "12px"; | |
copy.style.cursor = "pointer"; | |
copy.style.outline = "none"; | |
copy.style.border = "none"; | |
copy.style.borderRadius = "0 2px 2px 0"; | |
copy.style.background = "rgba(0, 0, 0, 0.05)"; | |
copy.style.transition = "all 0.2s"; | |
shadow.append(copy); | |
} | |
} | |
customElements.define('copy-link-in-markdown-format', CopyButton); | |
const wrapper = document.createElement('copy-link-in-markdown-format'); | |
document.querySelector('body').appendChild(wrapper); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment