Skip to content

Instantly share code, notes, and snippets.

@stringertheory
Created November 24, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stringertheory/cbe7a3a927789ecfe1fcb92fed943902 to your computer and use it in GitHub Desktop.
Save stringertheory/cbe7a3a927789ecfe1fcb92fed943902 to your computer and use it in GitHub Desktop.
code for a bookmarklet to email stuff to myself
(function() {
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function format_body() {
var body = "";
body += "URL: " + document.location + "\n\n";
body += "TITLE: " + document.title + "\n\n";
body += "LAST MODIFIED: " + document.lastModified + "\n\n";
var quote = getSelectionText();
if (quote !== "") {
body += "------------------------------------------\n";
body += "SELECTED TEXT:\n\n" + quote + "\n\n";
body += "------------------------------------------\n\n";
}
body += "NOTES:\n\n\n";
return body;
}
function format_subject() {
return "[email to self]: " + document.title;
}
function make_url() {
var to_email = "the_email_where_I_want_to_capture_this@example.com";
return `mailto:${to_email}?subject=${encodeURIComponent(format_subject())}&body=${encodeURIComponent(format_body())}`;
}
console.log(make_url());
window.open(make_url());
})()
@stringertheory
Copy link
Author

then put in bookmarklet by copying, removing newlines with pbpaste | tr -d '\n' | pbcopy, and then pasting into the bookmarket, prepended by "javascript:"

@stringertheory
Copy link
Author

Like this (remember to change the to_email):

javascript:(function() { function getSelectionText() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } else if (document.selection && document.selection.type != "Control") { text = document.selection.createRange().text; } return text; } function format_body() { var body = ""; body += "URL: " + document.location + "\n\n"; body += "TITLE: " + document.title + "\n\n"; body += "LAST MODIFIED: " + document.lastModified + "\n\n"; var quote = getSelectionText(); if (quote !== "") { body += "------------------------------------------\n"; body += "SELECTED TEXT:\n\n" + quote + "\n\n"; body += "------------------------------------------\n\n"; } body += "NOTES:\n\n\n"; return body; } function format_subject() { return "[email to self]: " + document.title; } function make_url() { var to_email = "the_email_where_I_want_to_capture_this@example.com"; return mailto:${to_email}?subject=${encodeURIComponent(format_subject())}&body=${encodeURIComponent(format_body())}; } console.log(make_url()); window.open(make_url());})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment