Skip to content

Instantly share code, notes, and snippets.

@jackbaty
Created July 29, 2021 12:15
Show Gist options
  • Save jackbaty/f4a7ecff1af638af45abd584dcbeb761 to your computer and use it in GitHub Desktop.
Save jackbaty/f4a7ecff1af638af45abd584dcbeb761 to your computer and use it in GitHub Desktop.
Org-mode bookmarklet
javascript: (function() {
let sel = document.getSelection();
let selText = "";
let reference = `[[${location.href }][${document.title }]]`;
const selectedRanges = [];
if (sel.rangeCount > 1) {
selText = reference + "\n";
for (let i = 0; i < sel.rangeCount; i += 1) {
selText += `\t${sel.getRangeAt(i).toString().trim()}<<< \n`;
selectedRanges.push(sel.getRangeAt(i))
}
alert("More than 1");
} else {
selText = sel.toString().trim();
if (selText.length) {
selectedRanges.push(sel.getRangeAt(0));
selText = `#+begin_quote\n${ selText }\n#+end_quote `
}
selText = reference + `\n${selText}`;
}
const ta = document.createElement("textarea");
ta.textContent = `${ selText }`;
document.body.appendChild(ta);
const docSel = document.getSelection();
docSel.removeAllRanges();
ta.select();
document.execCommand("copy");
docSel.removeAllRanges();
document.body.removeChild(ta);
let newSel = document.getSelection();
for (let i = 0; i < selectedRanges.length; i += 1) {
newSel.addRange(selectedRanges[i])
}
let toaster = document.createElement("div");
toaster.innerHTML = `Copied!`;
toaster.style.position = "fixed";
toaster.style.display = "block";
toaster.style.right = "10px";
toaster.style.top = "10px";
toaster.style.backgroundColor = "red";
toaster.style.color = "#FFFFFF";
toaster.style.padding = "5px";
toaster.style.borderRadius = "5px";
toaster.style.zIndex = "99999";
document.body.appendChild(toaster);
setTimeout(function() {
toaster.style.opacity = 0;
toaster.style.transition = "opacity 2s"
}, 2000);
setTimeout(function() {
document.body.removeChild(toaster)
}, 4000)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment