Copy HackerNews Comments As Markdown List

Need to install https://chrome.google.com/webstore/detail/modern-for-hacker-news/dabkegjlekdcmefifaolmdhnhdcplklo first to use this script.

От 05.02.2023. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         Copy HackerNews Comments As Markdown List
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Need to install https://chrome.google.com/webstore/detail/modern-for-hacker-news/dabkegjlekdcmefifaolmdhnhdcplklo first to use this script.
// @author       XXX
// @match        *://news.ycombinator.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ycombinator.com
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @license      GPL v3

// ==/UserScript==
(function ($, undefined) {
    'use strict';
    $(function () {
      $(document).ready(function() {
          $('head').append('<style type="text/css">.copy-text {text-align: center; padding-left: 5px; padding-right: 5px;} .copy-icon {display:flex; align-items: center; margin-right: 1em; width: 50px; height: 16px; cursor: pointer;} .copy-icon:hover {background-color: rgb(243 243 243);} .hn-comment-icons {display: flex; align-items: center;}</style>');
          const el = document.createElement("div");
          const iconEl = document.createElement("div");
          el.innerText = "COPY";
          el.className = "copy-text";
          iconEl.appendChild(el);
          iconEl.className = "copy-icon";

          $( ".hn-comment-icons" ).prepend(iconEl);
          $( ".copy-icon" ).click(function() {
              const currentCommentEl = this.parentElement.parentElement.parentElement;
              const infoLinkEl = currentCommentEl.querySelector(".hn-story-info-age");
              const linkHref = infoLinkEl.href;
              let allCommentText = "";
              // Convert comment text into Markdown List based on indent
              const todayDate = new Date().toISOString().slice(0, 10);
              const parentCommentText =  todayDate + ` [link](${linkHref})` + currentCommentEl.querySelector(".hn-comment-text").innerText.trim().replace(/^\s*[\r\n]/gm, "").split("\n").map(line => line.trim()).join("\n").replace(/\n/g, "<br>");

              const currentIndent = currentCommentEl.className.split(" ")[1].split("-")[3];
              $(currentCommentEl).nextUntil(".hn-comment-indent-" + currentIndent).each(function()
              {
                    const currentCommentIndent = this.className.split(" ")[1].split("-")[3];
                    const indent = " ".repeat((currentCommentIndent - currentIndent) * 4);
                    const currentCommentText = this.querySelector(".hn-comment-text").innerText.replace(/^\s*[\r\n]/gm, "").split("\n").map(line => line.trim()).join("\n").replace(/\n/g, "<br>");

                    // Comment Text maybe multiline, so we need to add indent to each line except the first line
                    const commentText = currentCommentText;
                    allCommentText += indent + "- " + commentText + "\n";

              });
              allCommentText = "- " + parentCommentText + "\n" + allCommentText;
              // Copy to clipboard
              navigator.clipboard.writeText(allCommentText);
          });
      })
    });
  })(window.jQuery.noConflict(true));