Quick-load Dreamwidth comment pages

Loads top comments from all pages and threads in any Dreamwidth post into a single page

目前为 2022-09-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Quick-load Dreamwidth comment pages
  3. // @namespace rallamajoop@gmail.com
  4. // @description Loads top comments from all pages and threads in any Dreamwidth post into a single page
  5. // @include https://*.dreamwidth.org/*.html*view=top-only*
  6. // @include https://*.dreamwidth.org/*.html*view=top-only&page=1*
  7. // @exclude /^.*page(1\d+|[2-9]\d*).*/
  8. // @version 1.1
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. //Loads top comments of every thread in any Dreamwidth post in one place by appending contents of pages 2-n to bottom of page 1 under ?view=top-only
  13. //'Threaded' links redirected to threaded comments for that page, rather than threaded comments page 1.
  14. //NOTE: Breaks "Show [x] comments" links on every page after page 1. I have tried to find a workaround to fix this and given up.
  15. //NOTE: Does not change behaviour on top-level pages from 2-max
  16. // V1.1: Minor update to fix TamperMonkey bug
  17.  
  18.  
  19. //Retrieve page as javascript object
  20. function getSourceAsDOM(url)
  21. {
  22.     var xmlhttp=new XMLHttpRequest();
  23.     xmlhttp.open("GET",url,false);
  24.     xmlhttp.send();
  25.     var parser=new DOMParser();
  26.     return parser.parseFromString(xmlhttp.responseText,"text/html");    
  27. }
  28.  
  29. //Remove all children of className (used for stripping redundant/broken material out of later pages)
  30. function removeAll(doc, className, keepCommentText=false) {
  31.     var children = doc.getElementsByClassName(className);
  32.     var leng = children.length;
  33.     if (leng<1) {
  34.         return;
  35.     }
  36.     for (var j=0; j<leng; j++) {
  37.         var par = children[0].parentNode;
  38.         if (keepCommentText) {
  39.           var span=document.createElement('span');
  40.           span.innerHTML = children[0].innerText.replace("Show ", "");
  41.           par.insertBefore(span, children[0].nextSibling);
  42.         }
  43.         par.removeChild(children[0]);
  44.        
  45.     }
  46. }
  47.  
  48. var topLinks = document.getElementsByClassName("comment-pages toppages");
  49. var pages = topLinks[0].getElementsByTagName("a");
  50. var comments = document.getElementById("comments");
  51. removeAll(comments, "bottomcomment");
  52.  
  53. var loc = location.href;
  54. var parts = loc.split(".html");
  55. loc=parts[0] + ".html";
  56. var head = document.getElementsByTagName('head')[0];
  57.  
  58. for (var i=1; i<pages.length; i++) {
  59.  
  60.       //Print notification that next page is loading
  61.     var loading=document.createElement('div');
  62.       loading.innerHTML="<p><h3>(Loading page " + (i+1) + ")<br><marquee style='width:50px' direction='right'>...</marquee></b></h3><br></p>";
  63.     comments.appendChild(loading);
  64.  
  65.       //Load next page
  66.       var pagehref=loc + "?view=top-only&page=" + (i+1) + "#comments";
  67.     var pg = getSourceAsDOM(pagehref);
  68.  
  69.     //Trim out redundant links
  70.     var comm = pg.getElementById("comments");
  71.     //removeAll(comm, "comment-pages toppages");   //strip out surplus page links
  72.     if (i<pages.length-1) {
  73.     removeAll(comm, "bottomcomment");   //strip out surplus page links
  74.     }
  75.     //strip out 'Show comments' links, as these will be broken on pages>1
  76.     removeAll(comm, "link cmt_unhide", true);  
  77.     removeAll(comm, "link expand");
  78.  
  79.  
  80.     //Modify 'Threaded' links in header/footer of each page to point threaded page corresponding to this page number
  81.     var num=i+1;
  82.     var threaded=comm.getElementsByClassName("view-threaded");
  83.     for (var j=0; j<threaded.length; j++) {
  84.       var lnks=threaded[j].getElementsByTagName("a");
  85.       for (var k=0; k<lnks.length; k++) {
  86.         lnks[k].href=loc+"?page=" + num + "#comments";
  87.         lnks[k].text+=" (Page "+num+")";
  88.         var newlink=document.createElement('span');
  89.         newlink.innerHTML=" | <a href=\""+pagehref+"\">Top-Level (Page "+num+")</a>";
  90.         lnks[k].parentNode.parentNode.appendChild(newlink);
  91.       }
  92.     }
  93.  
  94.  
  95.     //Print summary on last page
  96.     if (i==pages.length-1) {
  97.           var pgs=pages.length;
  98.         num=comm.getElementsByClassName("comment-thread comment-depth-odd comment-depth-1").length;
  99.         var div = document.createElement('div');
  100.         div.innerHTML="<p><b>Threads on last page: "+num+"/25<br>Threads total: "+(num+(pgs-1)*25)+"</b></p>";
  101.         comm.appendChild(div);
  102.         //alert(num);
  103.     }
  104.  
  105.     //Add new page to end of current
  106.     comments.appendChild(comm);
  107.    
  108. //Remove notification that next page is loading and replace with generic title
  109. loading.innerHTML="<p><a href=\""+pagehref+"\"><h1>Page " + (i+1) + "</h1></a></p>";
  110.  
  111. }
  112.  
  113.  
  114.