thread win

sort comments by favorites

  1. // ==UserScript==
  2. // @name thread win
  3. // @namespace bperkins
  4. // @description sort comments by favorites
  5. // @include https://www.metafilter.com/*
  6. // @include https://ask.metafilter.com/*
  7. // @include https://metatalk.metafilter.com/*
  8. // @include http://www.metafilter.com/*
  9. // @include http://ask.metafilter.com/*
  10. // @include http://metatalk.metafilter.com/*
  11. // @version 1.6
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15.  
  16. var savedstuff;
  17.  
  18. var winNode;
  19. var unwinNode;
  20. var copy;
  21.  
  22.  
  23.  
  24. function sortFav(a,b) {
  25.  
  26.  
  27.  
  28. return (-(getFav(a)-getFav(b)));
  29.  
  30. }
  31. function getFav(a)
  32. {
  33. var toplevel = a.getElementsByTagName("SPAN");
  34. var notloggedin = 0;
  35.  
  36. if (toplevel.length ==0 ) {
  37. return 1;
  38. }
  39.  
  40. var bylinespan = toplevel[0].getElementsByTagName("SPAN");
  41.  
  42. if (bylinespan.length ==0) {
  43. notloggedin =1
  44. bylinespan = a.getElementsByTagName("SPAN");
  45. }
  46. if (bylinespan.length ==0 ) {
  47. return 1;
  48. }
  49. var favspan;
  50.  
  51. if (bylinespan[0].getAttribute("class") == "staffp" ) {
  52. favspan = bylinespan[2].getElementsByTagName("SPAN");
  53. } else {
  54. favspan = bylinespan[0].getElementsByTagName("SPAN");
  55. }
  56.  
  57. if (favspan.length ==0) {
  58. //No extra span layer in MeTalk
  59. favspan=bylinespan
  60. }
  61.  
  62. var fava = favspan[0].getElementsByTagName("A");
  63.  
  64.  
  65.  
  66.  
  67. var fav =0;
  68. re = new RegExp("\\d+")
  69.  
  70.  
  71. if (fava.length > 0 &&fava[0] != undefined ) {
  72. var m;
  73. if (notloggedin) {
  74. if (fava.length >2) {
  75. m=re.exec(fava[2].innerHTML);
  76. } else {
  77. return 1
  78. }
  79. } else {
  80. m=re.exec(fava[0].innerHTML);
  81. }
  82. fav=m[0];
  83.  
  84. }
  85.  
  86.  
  87. return (1*fav)+1;
  88.  
  89. }
  90.  
  91.  
  92.  
  93.  
  94. function unwin () {
  95.  
  96. copy.replaceChild(winNode,unwinNode);
  97.  
  98. var parent=getCommentParent ();
  99. var newstuff = parent.childNodes;
  100. while ( newstuff.length >0 ) {
  101. parent.removeChild(newstuff[0]);
  102. }
  103.  
  104. for (var j = 0; j < savedstuff.length; j++) {
  105. parent.appendChild(savedstuff[j]);
  106. }
  107. }
  108.  
  109. function getCommentParent () {
  110. var snaps = ["//div[@id='posts']","//div[@id='page']","//body[@id='body']"];
  111.  
  112. for ( var snap in snaps ) {
  113.  
  114. page = document.evaluate(
  115. snaps[snap],
  116. document,
  117. null,
  118. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  119. null);
  120.  
  121.  
  122. for (var i = 0; i < page.snapshotLength; i++) {
  123. stuff = page.snapshotItem(i).childNodes;
  124. for (var j = 0; j < stuff.length; j++) {
  125.  
  126. if (stuff[j].nodeName.toUpperCase()=="DIV" &&
  127. (stuff[j].getAttribute("class") == "comments" ||
  128. stuff[j].getAttribute("class") == "comments bestleft"||
  129. stuff[j].getAttribute("class") == "comments best")) {
  130. return page.snapshotItem(i);
  131. }
  132. }
  133. }
  134. }
  135.  
  136. alert ("threadwin: failed to get comment parent");
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143. function threadwin () {
  144.  
  145. copy.replaceChild(unwinNode,winNode);
  146.  
  147.  
  148. var parent= getCommentParent();
  149. var stuff = parent.childNodes;
  150. var commentidxs = new Array;
  151. var comments = new Array;
  152. var objects = new Array ;
  153. savedstuff=new Array;
  154.  
  155. var k=0;
  156. var favs =""
  157.  
  158. for (var j = 0; j < stuff.length; j++) {
  159. if (stuff[j].nodeName.toUpperCase()=="DIV" &&
  160. (stuff[j].getAttribute("class") == "comments" ||
  161. stuff[j].getAttribute("class") == "comments bestleft"||
  162. stuff[j].getAttribute("class") == "comments best")) {
  163. commentidxs.push(j);
  164. comments.push(stuff[j]);
  165. }
  166. objects.push(stuff[j]);
  167. savedstuff.push(stuff[j]);
  168.  
  169. }
  170.  
  171. comments.sort(sortFav);
  172.  
  173. for (var j = 0; j < comments.length; j++) {
  174.  
  175. objects[commentidxs[j]]=comments[j];
  176.  
  177. }
  178.  
  179.  
  180. while ( stuff.length >0 ) {
  181.  
  182. parent.removeChild(stuff[0]);
  183. }
  184.  
  185.  
  186. for (var j = 0; j < objects.length; j++) {
  187. parent.appendChild(objects[j]);
  188. }
  189.  
  190. }
  191.  
  192.  
  193. res = document.evaluate(
  194. "//div[@class='copy']",
  195. document,
  196. null,
  197. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  198. null);
  199.  
  200. copy=res.snapshotItem(0)
  201.  
  202. if (copy.parentNode.id == "page"
  203. //newcode
  204. ||copy.parentNode.id == "posts" ) {
  205.  
  206.  
  207. winNode = document.createElement('a');
  208. winNode.innerHTML = "<small>[threadwin]</small> ";
  209. winNode.href ="#";
  210. winNode.addEventListener("click", threadwin, true);
  211. copy.appendChild( winNode );
  212. unwinNode = document.createElement('a');
  213. unwinNode.innerHTML = "<small>[unwin]</small>";
  214. unwinNode.href ="#";
  215. unwinNode.addEventListener("click", unwin, true);
  216.  
  217. }
  218.