kbin enhancement script

Few small changes to the kbin UI while they still develop some features

当前为 2023-06-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name kbin enhancement script
  3. // @description Few small changes to the kbin UI while they still develop some features
  4. // @namespace com.sirpsychomantis
  5. // @license MIT
  6. // @version 1.3
  7. // @grant none
  8. // @run-at document-end
  9. // @match https://fedia.io/*
  10. // @match https://kbin.social/*
  11. // ==/UserScript==
  12.  
  13.  
  14. (function(){
  15. function getSetting(setting) {
  16. let value = localStorage.getItem("setting-" + setting);
  17. if (value === null)
  18. value = "true";
  19. return value === "true";
  20. }
  21. function setSetting(setting, value) {
  22. localStorage.setItem("setting-" + setting, value);
  23. location.reload();
  24. }
  25. function addDomain(link) {
  26. const parts = link.title.split("@");
  27. if (parts[2] !== location.hostname) {
  28. const linkText = link.childNodes[link.childNodes.length-1];
  29. linkText.nodeValue += "@" + parts[2];
  30. }
  31. }
  32. function addDomains() {
  33. if (location.pathname.startsWith('/m')) {
  34. document.querySelectorAll(".magazine-inline, .user-inline").forEach(link => {
  35. addDomain(link);
  36. })
  37. } else {
  38. document.querySelectorAll(".magazine-inline").forEach(link => {
  39. addDomain(link);
  40. })
  41. }
  42. }
  43. function getComments(comment, allComments) {
  44. const id = comment.id.split('-')[2];
  45. allComments.push(comment);
  46. const subComments = comment.parentElement.querySelectorAll('blockquote[data-subject-parent-value="'+id+'"]');
  47. subComments.forEach(blockquote => { getComments(blockquote, allComments); });
  48. }
  49. function getCollapsos(comment, allCollapsos) {
  50. const id = comment.id.split('-')[2];
  51. if (comment.classList.contains('kes-expand'))
  52. allCollapsos.push(comment);
  53. const subComments = comment.parentElement.querySelectorAll('blockquote[data-subject-parent-value="'+id+'"]');
  54. subComments.forEach(blockquote => { getCollapsos(blockquote, allCollapsos); });
  55. }
  56. function expandComment(blockquote) {
  57. const allComments = [];
  58. getComments(blockquote, allComments);
  59. allComments.forEach(comment => { comment.style.display="" });
  60. // Just remove all these for now, don't want to figure out how to do this cleanly right now.
  61. const allCollapsos = [];
  62. getCollapsos(blockquote, allCollapsos);
  63. allCollapsos.forEach(comment => { comment.remove() });
  64. }
  65. function collapseComment(blockquote) {
  66. const id = blockquote.id.split('-')[2];
  67. let commentLevel = "1";
  68. blockquote.classList.forEach(classItem => {
  69. if (classItem.includes("comment-level"))
  70. commentLevel = classItem.split("--")[1];
  71. });
  72. const allComments = [];
  73. getComments(blockquote, allComments);
  74. allComments.forEach(comment => { comment.style.display="none" });
  75.  
  76. const username = blockquote.querySelector("header a").innerText;
  77.  
  78. const newBlockquote = document.createElement('blockquote');
  79. newBlockquote.className = 'kes-expand section comment entry-comment comment-level--' + commentLevel;
  80. newBlockquote.dataset.subjectParentValue = id;
  81. newBlockquote.innerHTML = '<header>' + username + ' <a href="javascript:;">expand</a></header>';
  82. newBlockquote.querySelector('a').addEventListener("click", () => {expandComment(blockquote)});
  83. blockquote.parentNode.insertBefore(newBlockquote, blockquote);
  84. }
  85. function addCollapseLinks() {
  86. if (location.pathname.startsWith('/m')) {
  87. const comments = document.querySelectorAll("blockquote.comment");
  88. comments.forEach(blockquote => {
  89. const menu = blockquote.querySelector("footer menu");
  90. const newLi = document.createElement('li');
  91. newLi.innerHTML = '<a href="javascript:;" class="kes-collapse">collapse</a>';
  92. menu.appendChild(newLi);
  93. });
  94. document.querySelectorAll(".kes-collapse").forEach(link => {link.addEventListener("click", () => {
  95. const blockquote = link.closest("blockquote.comment");
  96. collapseComment(blockquote);
  97. })});
  98. }
  99. }
  100. function generateSettingDiv(settingDisplay, setting) {
  101. const settingValue = getSetting(setting);
  102. const newDiv = document.createElement('div');
  103. newDiv.className = "row";
  104. newDiv.innerHTML = `<span>${settingDisplay}:</span>
  105. <div>
  106. <a class="kes-setting-yes link-muted ${settingValue ? 'active' : ''}" href="javascript:;" data-setting="${setting}">
  107. Yes
  108. </a>
  109. |
  110. <a class="kes-setting-no link-muted ${settingValue ? '' : 'active'}" href="javascript:;" data-setting="${setting}">
  111. No
  112. </a>
  113. </div>`;
  114. return newDiv;
  115. }
  116. function addHTMLSettings() {
  117. const settingsList = document.querySelector(".settings-list");
  118. const header = document.createElement('strong');
  119. header.textContent = "kbin enhancement script";
  120. settingsList.appendChild(header);
  121. settingsList.appendChild(generateSettingDiv("Show domains", "show-domains"));
  122. settingsList.appendChild(generateSettingDiv("Show collapse comment", "show-collapse"));
  123. document.querySelectorAll(".kes-setting-yes").forEach(link => {link.addEventListener("click", () => {setSetting(link.dataset.setting, true)})});
  124. document.querySelectorAll(".kes-setting-no").forEach(link => {link.addEventListener("click", () => {setSetting(link.dataset.setting, false)})});
  125. }
  126. addHTMLSettings();
  127. if (getSetting("show-domains"))
  128. addDomains();
  129. if (getSetting("show-collapse"))
  130. addCollapseLinks();
  131. })();