Advertisement
Guest User

Untitled

a guest
Mar 12th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Show Reply Times
  3. // @version 0.0.4.1
  4. // @description show reply times for reddit comments in a thread
  5. // @author anothershittyalt
  6. // @include /^https://(.*).reddit.com/r/counting/comments/(.*)
  7. // @namespace https://greasyfork.org/users/32246
  8. // ==/UserScript==
  9. /* jshint -W097 */
  10. 'use strict';
  11.  
  12. //add CSS from /r/reddit_timestamps/stylesheet
  13. var link = document.createElement('link');
  14. link.setAttribute('rel', 'stylesheet');
  15. link.setAttribute('type', 'text/css');
  16. link.setAttribute('href', 'https://a.thumbs.redditmedia.com/Hbef45nswkkeZVr-ODTUBUi9EyFiPAN4NgCV_6F7_j0.css');
  17. document.getElementsByTagName('head')[0].appendChild(link);
  18.  
  19. //convert seconds to a h:m:s string
  20. function time(t){
  21. var h = Math.floor(t/3600);
  22. var m = Math.floor((t%3600) / 60);
  23. var s = t%60;
  24. if(h > 0){h = h.toString() + "h ";} else{h = ""};
  25. if(m > 0){m = m.toString() + "m ";} else{m = ""};
  26. s = s.toString() + "s";
  27. return h+m+s;
  28. }
  29.  
  30. //fetch reply times on load
  31. window.addEventListener('load', function() {
  32. //grab all the comments in the page
  33. var comments = document.getElementsByClassName("sitetable")[1];
  34. comments = comments.getElementsByClassName("comment");
  35.  
  36. //keep track of whether a comment is "marked" or not so we dont parse it twice
  37. var commentsMarked = []
  38. for(var i = 0; i < comments.length; i++){
  39. commentsMarked.push(false);
  40. }
  41.  
  42. //HTMLCollections don't work with .indexOf() so we use this to find a certain element in them instead
  43. function search(list, item){
  44. for(var i = 0; i < comments.length; i++){
  45. if(list[i] == item){return i;}
  46. }
  47. return -1;
  48. }
  49.  
  50. function crawl(comment, last){
  51. //mark the comment
  52. var searchIndex = search(comments, comment);
  53. commentsMarked[searchIndex] = true;
  54.  
  55. //get all the children of the comment
  56. var c = comment.getElementsByClassName("comment");
  57.  
  58. //for each child comment...
  59. for(var i = 0; i < c.length; i++){
  60. //check if the child comment hasn't been marked already
  61. searchIndex = search(comments, c[i]);
  62. if(commentsMarked[searchIndex] == false){
  63. //if it hasn't then grab the timestamp and get the difference between this and it's parent
  64. var time_element = c[i].getElementsByTagName("time")[0]
  65. var timestamp_b = time_element.getAttribute("datetime");
  66. timestamp_b = new Date(timestamp_b).getTime()/1000;
  67. var diff = timestamp_b - last;
  68.  
  69. //then add that difference to the end of the timestamp (if it isn't there already)
  70. var title = time_element.getAttribute("title");
  71. if(title.indexOf(";") == -1){time_element.setAttribute("title", title + "; " + time(diff) + " reply");}
  72. if(diff <= 1){time_element.setAttribute("style", "font-weight: bold;");}
  73. commentsMarked[searchIndex] = true;
  74. }
  75. //do this recursively
  76. crawl(c[i], timestamp_b);
  77. }
  78. }
  79.  
  80. //crawl() through every single unmarked comment
  81. for(var i = 0; i < comments.length; i++){
  82. if(commentsMarked[i] == false){
  83. var timestamp = comments[i].getElementsByTagName("time")[0].getAttribute("datetime");
  84. timestamp = new Date(timestamp).getTime()/1000;
  85. crawl(comments[i], timestamp);
  86. commentsMarked[i] = true
  87. }
  88. }
  89.  
  90. }, false);
  91.  
  92. //fetch reply times on newly added comments (TODO: something about this method feels like it would be gross/hacky/slow, but idk)
  93. window.addEventListener("DOMNodeInserted", function(e) {
  94. //grab the node added
  95. var node = e.relatedNode;
  96.  
  97. //check if the node is a live-timestamp that doesn't have a reply time added to it
  98. if(node.getAttribute("class") == "live-timestamp"){
  99. if(node.getAttribute("title").indexOf(";") == -1){
  100. //grab the comment attached to that and its timestamp
  101. //that parentNode chain though
  102. var comment = node.parentNode.parentNode.parentNode;
  103. var parent = comment.parentNode.parentNode.parentNode;
  104.  
  105. //check if the elements we grabbed are actually both comments or not
  106. if(comment.getAttribute("data-type") == "comment" && parent.getAttribute("data-type") == "comment"){
  107. //grab the timestamp for the parent
  108. var timestamp = parent.getElementsByTagName("time")[0].getAttribute("datetime");
  109. timestamp = new Date(timestamp).getTime()/1000;
  110.  
  111. //grab the timestamp for the new comment and get the difference between that and it's parent
  112. var timestamp_b = node.getAttribute("datetime");
  113. timestamp_b = new Date(timestamp_b).getTime()/1000;
  114. var diff = timestamp_b - timestamp;
  115.  
  116. //then add that difference to the end of the child's timestamp (if it isn't there already somehow)
  117. var title = node.getAttribute("title");
  118. if(title.indexOf(";") == -1){node.setAttribute("title", title + "; " + time(diff) + " reply");}
  119. if(diff <= 1){node.setAttribute("style", "font-weight: bold;");}
  120. }
  121. }
  122. }
  123. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement