Skip to content

Instantly share code, notes, and snippets.

@htakeuchi
Created July 30, 2021 01:21
Show Gist options
  • Save htakeuchi/43f0fefe6ab9617ccd595c1b38626b25 to your computer and use it in GitHub Desktop.
Save htakeuchi/43f0fefe6ab9617ccd595c1b38626b25 to your computer and use it in GitHub Desktop.
表示中のページのURL、タイトル、選択範囲をクロップボードへコピーするBookmarklet
(() => {
const main = () => {
const textAreaElement = document.createElement("textarea");
textAreaElement.textContent = getUrlInfo();
const bodyElement = document.getElementsByTagName("body")[0];
bodyElement.appendChild(textAreaElement);
textAreaElement.select();
const isSuccessCopy = document.execCommand('copy');
bodyElement.removeChild(textAreaElement);
};
const getUrlInfo = () => {
let title = document.title;
const replacedStrings = {
':': ':',
'\\[': '[',
'\\]': ']',
'\\|': '|'
};
for (let key in replacedStrings) {
title = title.replace(new RegExp(key, 'g'), replacedStrings[key]);
}
var q = '';
if (String(document.getSelection()).length > 0) {
q = '“' + document.getSelection() + '”\n\n';
}
return q + title + '\n' + document.URL + '\n';
};
main();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment