复制 HackerNews 评论(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.

  1. // ==UserScript==
  2. // @name 复制 HackerNews 评论(Copy HackerNews Comments As Markdown List)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Need to install https://chrome.google.com/webstore/detail/modern-for-hacker-news/dabkegjlekdcmefifaolmdhnhdcplklo first to use this script.
  6. // @author Boninall
  7. // @match *://news.ycombinator.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=ycombinator.com
  9. // @grant none
  10. // @require https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js
  11. // @license GPL v3
  12.  
  13. // ==/UserScript==
  14. (function ($, undefined) {
  15. 'use strict';
  16. $(function () {
  17. $(document).ready(function() {
  18. $('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>');
  19. const el = document.createElement("div");
  20. const iconEl = document.createElement("div");
  21. el.innerText = "COPY";
  22. el.className = "copy-text";
  23. iconEl.appendChild(el);
  24. iconEl.className = "copy-icon";
  25.  
  26. $( ".hn-comment-icons" ).prepend(iconEl);
  27. $( ".copy-icon" ).click(function() {
  28. const currentCommentEl = this.parentElement.parentElement.parentElement;
  29. const infoLinkEl = currentCommentEl.querySelector(".hn-story-info-age");
  30. const linkHref = infoLinkEl.href;
  31. let allCommentText = "";
  32. // Convert comment text into Markdown List based on indent
  33. const todayDate = new Date().toISOString().slice(0, 10);
  34. 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>");
  35.  
  36. const currentIndent = currentCommentEl.className.split(" ")[1].split("-")[3];
  37. $(currentCommentEl).nextUntil(".hn-comment-indent-" + currentIndent).each(function()
  38. {
  39. const currentCommentIndent = this.className.split(" ")[1].split("-")[3];
  40. const indent = " ".repeat((currentCommentIndent - currentIndent) * 4);
  41. 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>");
  42.  
  43. // Comment Text maybe multiline, so we need to add indent to each line except the first line
  44. const commentText = currentCommentText;
  45. allCommentText += indent + "- " + commentText + "\n";
  46.  
  47. });
  48. allCommentText = "- " + parentCommentText + "\n" + allCommentText;
  49. // Copy to clipboard
  50. navigator.clipboard.writeText(allCommentText);
  51. });
  52. })
  53. });
  54. })(window.jQuery.noConflict(true));