Skip to content

Instantly share code, notes, and snippets.

@hyuki

hyuki/LinkMD.js Secret

Last active January 13, 2023 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki/ca56f989939a3b6a70c49a7d231db3d6 to your computer and use it in GitHub Desktop.
Save hyuki/ca56f989939a3b6a70c49a7d231db3d6 to your computer and use it in GitHub Desktop.
LinkMD.js - ブラウザで見ているWebページのタイトルとURLをMarkdown形式でクリップボードに保存するBookmarklet(LinkMD.js)
function initToast() {
const bodies = document.getElementsByTagName("body");
const body = bodies[0];
const div = document.createElement("div");
div.id = "TOAST-IT";
div.innerText = "TOAST-IT";
div.style.position = "fixed";
div.style.zIndex = "5000";
div.style.top = "0px";
div.style.left = "0px";
div.style.padding = "10px";
div.style.margin = "10px";
div.style.borderRadius = "5px";
div.style.display = "none";
div.style.color = "white";
div.style.backgroundColor = "#1976D2";
div.style.whiteSpace = "pre-wrap";
div.style.wordWrap = "break-word";
body.append(div);
}
function showToast(s) {
const div = document.getElementById("TOAST-IT");
div.innerText = s;
div.style.display = "block";
setTimeout(function() {
div.style.display = "none";
}, 5000);
}
initToast();
function saveToClipboard(s) {
navigator.clipboard.writeText(s).then(function() {
showToast(`Saved to clipboard:\n----\n${s}\n----\n`);
}, function() {
showToast(`ERROR: ${s} is NOT saved`);
});
}
function start() {
const url = location.href;
const title = document.title;
const text = `- [${title}](${url})`;
saveToClipboard(text);
}
start();