gracefulally

Userscript - LJ/IJ/Scribbld Unfold All

Sep 11th, 2012
427
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LJ Thread Unfolder
  2. // version 0.5
  3. // 2008-03-03
  4. // Copyright (c) 2005-2008, Tim Babych
  5. // Homepage: http://clear.com.ua/projects/firefox/unfolder
  6. // Released under the GPL license
  7. // http://www.gnu.org/copyleft/gpl.html
  8. //
  9. // --------------------------------------------------------------------
  10. //
  11. // This is a Greasemonkey user script.
  12. //
  13. // To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
  14. // Then restart Firefox and revisit this script.
  15. // Under Tools, there will be a new menu item to "Install User Script".
  16. // Accept the default configuration and install.
  17. //
  18. // To uninstall, go to Tools/Manage User Scripts,
  19. // select "LJ Thread Unfolder", and click Uninstall.
  20. //
  21. // --------------------------------------------------------------------
  22. //
  23. // ==UserScript==
  24. // @name            LJ Thread Unfolder
  25. // @description Expands nested discussion threads in place
  26. // @include         http://*.livejournal.com/*
  27. // ==/UserScript==
  28.  
  29. //================================
  30. //  INIT
  31. //================================
  32.  
  33. GM_compat()
  34. add_unfolders(document)
  35. add_unfoldalllink()
  36.  
  37. GM_addStyle(
  38. "a.unfold_thread {color: black; text-decoration:none} "+
  39. "a.unfold_thread:hover {color: black; text-decoration:none} "+
  40. "a.unfold_thread img { border:none; position: relative; top: 3px;}"
  41. )
  42.  
  43.  
  44. //================================
  45. //  WORKHORSES
  46. //================================
  47.  
  48. function add_unfolders(elem) {
  49.     get_itemid_regexp = /thread=(\d+)/;
  50.     var allReplies;
  51.     allReplies = document.evaluate(
  52.         "//a[contains(@href, '?thread')]",
  53.         elem,
  54.         null,
  55.         XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  56.         null);
  57.        
  58.     for (var i = 0; i < allReplies.snapshotLength; i++) {
  59.         var thisOne = allReplies.snapshotItem(i);
  60.        
  61.         if (thisOne.innerHTML == 'Parent')
  62.             continue;
  63.        
  64.         if (thisOne.nextSibling && thisOne.nextSibling.nextSibling && thisOne.nextSibling.nextSibling.innerHTML == 'Unfold')
  65.             continue;
  66.        
  67.         // the first child in td
  68.         if (thisOne.parentNode.firstChild == thisOne || // just Unfold
  69.             ( thisOne.previousSibling && thisOne.previousSibling.previousSibling
  70.             && thisOne.previousSibling.previousSibling.href ) ) { // Unfold all
  71.                        
  72.             var t = document.createTextNode(' - ');
  73.             thisOne.parentNode.insertBefore(t, thisOne.nextSibling);
  74.  
  75.             var params =  get_itemid_regexp.exec(thisOne.href);
  76.  
  77.             var a = document.createElement('a');
  78.             var linktxt = document.createTextNode('Unfold');
  79.             a.href = 'javascript:void(0)';
  80.             if (thisOne.previousSibling) // 'All' mode
  81.                 a.setAttribute('all', true);
  82.             a.setAttribute('thread_id', params[1]);
  83.             a.setAttribute('thread_href', thisOne.href);
  84.             a.addEventListener("click", unfold_me, false)
  85.             a.appendChild(linktxt);
  86.             t.parentNode.insertBefore(a, t.nextSibling);
  87.         }
  88.     }
  89. }  
  90.  
  91. function unfold_me() {
  92.  unfold_thread(this);  
  93. }
  94.  
  95. function unfold_thread(e) {
  96.     e.className = 'unfold_thread';
  97.     e.style.cursor = 'default';
  98.     e.innerHTML = loader;
  99.     e.blur();
  100.  
  101.     GM_xmlhttpRequest({
  102.         method: 'GET',
  103.         url: e.getAttribute('thread_href'),
  104.         onload: function(responseDetails) { parse_result(responseDetails.responseText, e); }
  105.         });
  106.        
  107.     function parse_result(result, e){
  108.         var dummy = document.createElement('DIV');
  109.         dummy.style.display = 'none';
  110.         document.body.appendChild(dummy);
  111.         var old = document.getElementById('ljcmt'+e.getAttribute('thread_id'));
  112.         if (old)
  113.             old.id += '_old';
  114.         dummy.innerHTML = result;
  115.         var reply = document.getElementById('ljcmt'+e.getAttribute('thread_id'));
  116.            
  117.         add_unfolders(dummy);
  118.         var td = e;
  119.  
  120.         while (td.nodeName != 'TD') {
  121.             td = td.parentNode;
  122.         }
  123.         var tbody = td.parentNode.parentNode;
  124.         var span = tbody.parentNode.parentNode;
  125.  
  126.         var my_padding = tbody.firstChild.firstChild.firstChild.width;
  127.        
  128.         while(reply && reply.nodeName != 'HR'){
  129.             var next_one = reply.nextSibling;
  130.             if (span.parentNode) {
  131.               span.parentNode.insertBefore(reply, span);
  132.               if (reply.nodeName == 'SPAN')
  133.                 reply.firstChild.firstChild.firstChild.firstChild.firstChild.width += my_padding;
  134.             }
  135.           reply = next_one;
  136.         }
  137.  
  138.         var next_comment = '';
  139.         if (span.nextSibling && span.nextSibling.nextSibling && span.nextSibling.nextSibling.nextSibling)
  140.             next_comment = span.nextSibling.nextSibling.nextSibling;
  141.         while(next_comment && next_comment.firstChild && next_comment.firstChild.firstChild && next_comment.firstChild.firstChild.firstChild && next_comment.firstChild.firstChild.firstChild.firstChild && next_comment.firstChild.firstChild.firstChild.firstChild.firstChild && (next_comment.firstChild.firstChild.firstChild.firstChild.firstChild.width > my_padding)) {
  142.             var to_del = next_comment;
  143.             if (next_comment.nextSibling && next_comment.nextSibling.nextSibling && next_comment.nextSibling.nextSibling.nextSibling)
  144.                 next_comment = next_comment.nextSibling.nextSibling.nextSibling;
  145.             else
  146.                 next_comment = '';
  147.             if (to_del && to_del.parentNode)
  148.                 to_del.parentNode.removeChild(to_del);
  149.         }
  150.        
  151.         document.body.removeChild(dummy);
  152.         if (span && span.parentNode)
  153.             span.parentNode.removeChild(span);
  154.     }
  155. }
  156.  
  157.  
  158. //========================================
  159. //  ROUTINES
  160. //========================================
  161. function GM_compat() {
  162.     if ( typeof GM_xmlhttpRequest == 'undefined' )
  163.         GM_xmlhttpRequest = (function(params) {
  164.             var req = new XMLHttpRequest();
  165.             req.onreadystatechange =  function() {
  166.                     if (req.readyState != 4) return; // while not Complete
  167.                     params.onload(req);
  168.                 }
  169.             req.open(params.method, params.url, true);
  170.             req.send("");
  171.         });
  172.        
  173.     if ( typeof GM_addStyle == 'undefined' )
  174.         GM_addStyle = (function(css) {
  175.             style = document.createElement('STYLE');
  176.             style.type = 'text/css';
  177.             style.innerHTML = css;
  178.             document.body.appendChild(style);  
  179.         });
  180. }
  181.  
  182. function add_unfoldalllink(){
  183. if (!$x("//a[contains(@thread_href, '?thread')]").length)
  184.     return;
  185.    
  186. $x("//a[contains(@href, '?mode=reply')]/ancestor::b").forEach(function(b) {
  187.    
  188.     var comma = document.createTextNode(', ');
  189.     b.appendChild(comma);
  190.    
  191.     var newA = document.createElement("A");
  192.     with (newA) {
  193.         innerHTML = "Unfold all";
  194.         href = "javascript:void(0)";
  195.         addEventListener("click", unfold_all, false);
  196.     }
  197.    
  198.     b.appendChild(newA);
  199.     b.insertBefore(newA, newA.previousSibling.previousSibling);
  200.     b.insertBefore(comma, comma.previousSibling.previousSibling);
  201.    
  202. });
  203. }
  204.  
  205. var loader = '<img src="data:image/gif;base64,R0lGODlhEAAQAMQAAP///+7u7t3d3bu7u6qqqpmZmYi'+
  206. 'IiHd3d2ZmZlVVVURERDMzMyIiIhEREQAR'+
  207. 'AAAAAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05F'+
  208. 'VFNDQVBFMi4wAwEAAAAh+QQFBwAQACwAAAAAEAAQAAAFdyAkQgGJJOWoQgIjBM8jkKsoPEzgyMGs'+
  209. 'CjPDw7ADpkQBxRDmSCRetpRA6Rj4kFBkgLC4IlUGhbNQIwXOYYWCXDufzYPDMaoKGBoKb886OjAK'+
  210. 'dgZAAgQkfCwzAgsDBAUCgl8jAQkHEAVkAoA1AgczlyIDczUDA2UhACH5BAUHABAALAAAAAAPABAA'+
  211. 'AAVjICSO0IGIATkqIiMKDaGKC8Q49jPMYsE0hQdrlABCGgvT45FKiRKQhWA0mPKGPAgBcTjsspBC'+
  212. 'AoH4gl+FmXNEUEBVAYHToJAVZK/XWoQQDAgBZioHaX8igigFKYYQVlkCjiMhACH5BAUHABAALAAA'+
  213. 'AAAQAA8AAAVgICSOUGGQqIiIChMESyo6CdQGdRqUENESI8FAdFgAFwqDISYwPB4CVSMnEhSej+Fo'+
  214. 'gNhtHyfRQFmIol5owmEta/fcKITB6y4choMBmk7yGgSAEAJ8JAVDgQFmKUCCZnwhACH5BAUHABAA'+
  215. 'LAAAAAAQABAAAAViICSOYkGe4hFAiSImAwotB+si6Co2QxvjAYHIgBAqDoWCK2Bq6A40iA4yYMgg'+
  216. 'NZKwGFgVCAQZotFwwJIF4QnxaC9IsZNgLtAJDKbraJCGzPVSIgEDXVNXA0JdgH6ChoCKKCEAIfkE'+
  217. 'BQcAEAAsAAAAABAADgAABUkgJI7QcZComIjPw6bs2kINLB5uW9Bo0gyQx8LkKgVHiccKVdyRlqjF'+
  218. 'SAApOKOtR810StVeU9RAmLqOxi0qRG3LptikAVQEh4UAACH5BAUHABAALAAAAAAQABAAAAVxICSO'+
  219. '0DCQKBQQonGIh5AGB2sYkMHIqYAIN0EDRxoQZIaC6bAoMRSiwMAwCIwCggRkwRMJWKSAomBVCc5l'+
  220. 'UiGRUBjO6FSBwWggwijBooDCdiFfIlBRAlYBZQ0PWRANaSkED1oQYHgjDA8nM3kPfCmejiEAIfkE'+
  221. 'BQcAEAAsAAAAABAAEAAABWAgJI6QIJCoOIhFwabsSbiFAotGMEMKgZoB3cBUQIgURpFgmEI0EqjA'+
  222. 'CYXwiYJBGAGBgGIDWsVicbiNEgSsGbKCIMCwA4IBCRgXt8bDACkvYQF6U1OADg8mDlaACQtwJCEA'+
  223. 'IfkEBQcAEAAsAAABABAADwAABV4gJEKCOAwiMa4Q2qIDwq4wiriBmItCCREHUsIwCgh2q8MiyEKO'+
  224. 'DK7ZbHCoqqSjWGKI1d2kRp+RAWGyHg+DQUEmKliGx4HBKECIMwG61AgssAQPKA19EAxRKz4QCVIh'+
  225. 'ACH5BAUHABAALAAAAAAQABAAAAVjICSOUBCQqHhCgiAOKyqcLVvEZOC2geGiK5NpQBAZCilgAYFM'+
  226. 'ogo/J0lgqEpHgoO2+GIMUL6p4vFojhQNg8rxWLgYBQJCASkwEKLC17hYFJtRIwwBfRAJDk4Obwsi'+
  227. 'dEkrWkkhACH5BAUHABAALAAAAQAQAA8AAAVcICSOUGAGAqmKpjis6vmuqSrUxQyPhDEEtpUOgmgY'+
  228. 'ETCCcrB4OBWwQsGHEhQatVFhB/mNAojFVsQgBhgKpSHRTRxEhGwhoRg0CCXYAkKHHPZCZRAKUERZ'+
  229. 'MAYGMCEAIfkEBQcAEAAsAAABABAADwAABV0gJI4kFJToGAilwKLCST6PUcrB8A70844CXenwILRk'+
  230. 'IoYyBRk4BQlHo3FIOQmvAEGBMpYSop/IgPBCFpCqIuEsIESHgkgoJxwQAjSzwb1DClwwgQhgAVVM'+
  231. 'IgVyKCEAIfkECQcAEAAsAAAAABAAEAAABWQgJI5kSQ6NYK7Dw6xr8hCw+ELC85hCIAq3Am0U6JUK'+
  232. 'jkHJNzIsFAqDqShQHRhY6bKqgvgGCZOSFDhAUiWCYQwJSxGHKqGAE/5EqIHBjOgyRQELCBB7EAQH'+
  233. 'fySDhGYQdDWGQyUhADs="> Loading&hellip;';
  234.  
  235. function $x(path, root) {
  236.     if (!root) root = document;
  237.     var i, arr = [], xpr = document.evaluate(path, root, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  238.     for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  239.     return arr;
  240. }
  241.  
  242. function unfold_all() {
  243.     $x("//a[contains(@thread_href, '?thread')]").forEach(function(value){
  244.         console.log("ID:" + value.getAttribute('thread_id'));
  245.         unfold_thread(value);
  246.             });
  247.   }
RAW Paste Data