Last active
September 1, 2024 00:58
-
-
Save tkdn/cf8084bb18737abc218d9c7662c1a9a4 to your computer and use it in GitHub Desktop.
キーバインドでタイトルとURLをコピーする
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 出、出〜文書鍵押下複製奴〜 | |
// @namespace https://gist.github.com/tkdn/cf8084bb18737abc218d9c7662c1a9a4#file-url-title-copy-user-js | |
// @updateURL https://gist.github.com/tkdn/cf8084bb18737abc218d9c7662c1a9a4/raw/36e6c441b38c8c84ee9d64677020414b98bd293f/url-title-copy.user.js | |
// @downloadURL https://gist.github.com/tkdn/cf8084bb18737abc218d9c7662c1a9a4/raw/36e6c441b38c8c84ee9d64677020414b98bd293f/url-title-copy.user.js | |
// @version 0.1 | |
// @description 特定のキーバインドでクリップボードにタイトルとURLをコピーする | |
// @author tkdn <[email protected]> | |
// @grant none | |
// @match https://*/* | |
// ==/UserScript== | |
const MARKDOWN_TEXT = `[${document.title}](${location.href})`; | |
const COSENSE_TEXT = `[${document.title} ${location.href}]`; | |
/** | |
* DOM Manipulation Setup | |
*/ | |
const $fragment = document.createDocumentFragment(); | |
const $div = document.createElement('div'); | |
$div.id = 'tkdn-copy-area'; | |
$fragment.appendChild($div); | |
const $style = document.createElement('style'); | |
$style.textContent = ` | |
#tkdn-copy-area { | |
display: none; | |
position: fixed; | |
inset: 0; | |
width: 80vw; | |
margin-inline: auto; | |
align-content: center; | |
text-align: center; | |
background-color: rgba(0, 0, 0, .7); | |
max-height: 7vh; | |
padding: 10px; | |
color: white; | |
font-weight: 600; | |
animation: vanish .75s; | |
&.appear { | |
display: block; | |
animation: appear .75s; | |
} | |
} | |
@keyframes appear { | |
from { | |
opacity: 0; | |
} | |
to { | |
opacity: 1; | |
} | |
} | |
` | |
$fragment.appendChild($style) | |
document.body.appendChild($fragment); | |
/** | |
* KeyEvent Listen | |
*/ | |
const markDownKeyStack = new Set(); | |
const markDownShortcutKeys = new Set(['Meta', 'Control', 'c']); | |
const cosenseKeyStack = new Set(); | |
const cosenseShortcutKeys = new Set(['Meta', 'Control', 'x']); | |
document.addEventListener('keydown', async (e) => { | |
if (markDownShortcutKeys.has(e.key)) { | |
markDownKeyStack.add(e.key); | |
} | |
if (cosenseShortcutKeys.has(e.key)) { | |
cosenseKeyStack.add(e.key); | |
} | |
if (markDownShortcutKeys.difference(markDownKeyStack).size === 0) { | |
await window.navigator.clipboard.writeText(MARKDOWN_TEXT); | |
$div.innerText = `Copied: ${MARKDOWN_TEXT}`; | |
$div.classList = 'appear'; | |
setTimeout(() => $div.classList = '', 2000); | |
} | |
if (cosenseShortcutKeys.difference(cosenseKeyStack).size === 0) { | |
await window.navigator.clipboard.writeText(COSENSE_TEXT); | |
$div.innerText = `Copied: ${COSENSE_TEXT}`; | |
$div.classList = 'appear'; | |
setTimeout(() => $div.classList = '', 2000); | |
} | |
}); | |
document.addEventListener('keyup', (e) => { | |
markDownKeyStack.delete(e.key); | |
cosenseKeyStack.delete(e.key); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment