Advertisement
Guest User

Untitled

a guest
Mar 18th, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:(function() {
  2.  
  3. function htmlToMarkdown(node, indent) {
  4.     if (typeof indent === 'undefined') {
  5.         indent = "";
  6.     }
  7.     var output = "";
  8.     for (var i = 0; i < node.childNodes.length; i++) {
  9.         var childNode = node.childNodes[i];
  10.         if (childNode.nodeType === Node.TEXT_NODE) {
  11.             output += childNode.textContent;
  12.         } else if (childNode.nodeType === Node.ELEMENT_NODE) {
  13.             if (childNode.tagName === 'BR') {
  14.                 output += "\n";
  15.             } else if (childNode.tagName === 'LI') {
  16.                 output += "\n" + indent + "* " + htmlToMarkdown(childNode, indent);
  17.             } else if (childNode.tagName === 'OL' || childNode.tagName === 'UL') {
  18.                 output += htmlToMarkdown(childNode, indent + "  ");
  19.             } else if (childNode.tagName === 'A') {
  20.                 output += "[" + htmlToMarkdown(childNode, indent) + "](" + childNode.href + ")";
  21.             } else {
  22.                 output += htmlToMarkdown(childNode, indent);
  23.             }
  24.         }
  25.     }
  26.     return output;
  27. }
  28.  
  29. function copyToClipboard(text) {
  30.     if (window.clipboardData && window.clipboardData.setData) {
  31.         return clipboardData.setData("Text", text);
  32.     } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
  33.         var textarea = document.createElement("textarea");
  34.         textarea.textContent = text;
  35.         textarea.style.position = "fixed";
  36.         document.body.appendChild(textarea);
  37.         textarea.select();
  38.         try {
  39.             return document.execCommand("copy");
  40.         } catch (ex) {
  41.             console.warn("Copy to clipboard failed.", ex);
  42.             return false;
  43.         } finally {
  44.             document.body.removeChild(textarea);
  45.         }
  46.     }
  47. }
  48.  
  49. var markdown = '[' + document.title + '](' + window.location.href + ')';
  50. var selection = window.getSelection();
  51. if (selection.rangeCount > 0) {
  52.     var range = selection.getRangeAt(0);
  53.     var selectedContent = range.cloneContents();
  54.     var div = document.createElement('div');
  55.     div.appendChild(selectedContent);
  56.     selection = '\n' + htmlToMarkdown(div);
  57. }
  58. copyToClipboard(markdown + selection);
  59. })();
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement