Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- javascript:(function() {
- function htmlToMarkdown(node, indent) {
- if (typeof indent === 'undefined') {
- indent = "";
- }
- var output = "";
- for (var i = 0; i < node.childNodes.length; i++) {
- var childNode = node.childNodes[i];
- if (childNode.nodeType === Node.TEXT_NODE) {
- output += childNode.textContent;
- } else if (childNode.nodeType === Node.ELEMENT_NODE) {
- if (childNode.tagName === 'BR') {
- output += "\n";
- } else if (childNode.tagName === 'LI') {
- output += "\n" + indent + "* " + htmlToMarkdown(childNode, indent);
- } else if (childNode.tagName === 'OL' || childNode.tagName === 'UL') {
- output += htmlToMarkdown(childNode, indent + " ");
- } else if (childNode.tagName === 'A') {
- output += "[" + htmlToMarkdown(childNode, indent) + "](" + childNode.href + ")";
- } else {
- output += htmlToMarkdown(childNode, indent);
- }
- }
- }
- return output;
- }
- function copyToClipboard(text) {
- if (window.clipboardData && window.clipboardData.setData) {
- return clipboardData.setData("Text", text);
- } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
- var textarea = document.createElement("textarea");
- textarea.textContent = text;
- textarea.style.position = "fixed";
- document.body.appendChild(textarea);
- textarea.select();
- try {
- return document.execCommand("copy");
- } catch (ex) {
- console.warn("Copy to clipboard failed.", ex);
- return false;
- } finally {
- document.body.removeChild(textarea);
- }
- }
- }
- var markdown = '[' + document.title + '](' + window.location.href + ')';
- var selection = window.getSelection();
- if (selection.rangeCount > 0) {
- var range = selection.getRangeAt(0);
- var selectedContent = range.cloneContents();
- var div = document.createElement('div');
- div.appendChild(selectedContent);
- selection = '\n' + htmlToMarkdown(div);
- }
- copyToClipboard(markdown + selection);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement