Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Created October 14, 2015 20:22
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 bennettscience/d888e5b896c1145cef81 to your computer and use it in GitHub Desktop.
Save bennettscience/d888e5b896c1145cef81 to your computer and use it in GitHub Desktop.
Takes a window selection and downloads a plaintext file.
var text = "";
function getPlainText() {
if(window.getSelection == undefined) {
alert("There is no text selected. Please select some text from the page and try again.")
} else {
text = window.getSelection().toString();
console.log(text);
var download = document.getElementById('download');
download.href = 'data:text/plain;charset:utf-8,' + encodeURIComponent(text);
var event = new MouseEvent('click');
download.dispatchEvent(event);
}
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment