Advertisement
rccharles

asc hiroto pagination tweaks

Sep 1st, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 5.39 KB | None | 0 0
  1. // ==UserScript==
  2. // @name            ASC thread content view (d2)
  3. // @namespace       bubo-bubo/gmscripts
  4. // @description     ASC - disable jquery animation, initially show all replies, hide rollup solved answer in page 2 or later, clone pagination navigator in header.
  5. // @grant           unsafeWindow
  6. // @run-at          document-end
  7. // @include         https://discussions.apple.com/*
  8. // @include         https://discussionsjapan.apple.com/*
  9. // @include         https://discussionskorea.apple.com/*
  10. // @include         https://discussionschinese.apple.com/*
  11. // @include         https://communities.apple.com/*
  12. // @version         0.2.1
  13. // ==/UserScript==
  14.  
  15. var _debug = 0;
  16. var $ = unsafeWindow.jQuery;
  17. var watchdog1, watchdog2, watchdog3;
  18. var watch_interval          = 300; // [ms]
  19. var re_thread               = new RegExp('^https://[^/]+/thread/');
  20. var re_thread_or_message    = new RegExp('^https://[^/]+/(thread|message)/');
  21.  
  22. // disable jquery animation (globally)
  23. $.fx.off = true;
  24.  
  25. // supplementary styles for pagination nagivator cloned in header (.thread-container-wrapper)
  26. // pagination navigator
  27. document.styleSheets[0].insertRule(
  28. '.thread-container-wrapper .j-pagination.top {\
  29.     position: absolute !important;\
  30.     top: -10px !important;\
  31.     right: 0px !important;\
  32. }', 0);
  33. // loading gear icon
  34. document.styleSheets[0].insertRule(
  35. '.thread-container-wrapper .j-loading-big {\
  36.     position: absolute;\
  37.     top: -6px;\
  38.     left: 0;\
  39.     margin-left: -60px;\
  40.     z-index: -1;\
  41. }', 0);
  42.  
  43.  
  44. // modify thread content view behaviour (in post-load phase)
  45. window.onload = function() {
  46.     // register event listeners
  47.     window.addEventListener('unload', function(e) {
  48.         if (_debug) console.log('unload is observed');
  49.         stop_watchdog(watchdog1);
  50.         stop_watchdog(watchdog2);
  51.         stop_watchdog(watchdog3);
  52.         window.removeEventListener('_locationchange', _locationchange_handler, true);
  53.         window.removeEventListener(e.type, arguments.callee, true);
  54.     }, true);
  55.  
  56.     window.addEventListener('_locationchange', _locationchange_handler, true);
  57.     function _locationchange_handler(e) {
  58.         if (_debug) console.log('_locationchange is observed');
  59.         var href = window.location.href;
  60.         if ( href.match(re_thread) ) {
  61.             setTimeout(show_all_replies, 100);
  62.         }
  63.         if ( href.match(re_thread_or_message) ) {
  64.             setTimeout(hide_solved_p2, 100);
  65.             setTimeout(pagination_in_header, 100);
  66.         }
  67.     }
  68.  
  69.     // _locationchange watch dog
  70.     var prev_href = '';
  71.     watchdog1 = setInterval( function() {
  72.         // watch for location to change
  73.         var curr_href = window.location.href;
  74.         if (curr_href != prev_href) {
  75.             if (_debug) console.log('_locationchange is issued');
  76.             window.dispatchEvent(new Event('_locationchange'));
  77.             prev_href = curr_href;
  78.         }
  79.     }, watch_interval);
  80. };
  81.  
  82. function show_all_replies() {
  83.     window.addEventListener('_helpfuldisplay', function(e) {
  84.         if (_debug) console.log('_helpfuldisplay is observed');
  85.         $('.helpful-all-switch li.helpful').addClass('inactive').removeClass('active').hide();
  86.         $('.helpful-all-switch li.all-replies').addClass('active').removeClass('inactive').show();
  87.         $('#helpful-container').hide();
  88.         $('.all-replies-container').show();
  89.         window.removeEventListener(e.type, arguments.callee, true);
  90.     }, true);
  91.  
  92.     watchdog2 = setInterval( function() {
  93.         // watch for helpful container to appear
  94.         if (_debug) console.log('watchdog (for helpful container) is active : ' + watchdog2);
  95.         if ($('#helpful-container').css('display') != 'none') {
  96.             if (_debug) console.log('_helpfuldisplay is issued');
  97.             window.dispatchEvent(new Event('_helpfuldisplay'));
  98.             stop_watchdog(watchdog2);
  99.         }
  100.     }, watch_interval);
  101.     setTimeout(stop_watchdog, 3000, watchdog2);
  102. }
  103.  
  104. function hide_solved_p2() {
  105.     var u = window.location.href;
  106.     var re = /[?&]start=([0-9]+)/;
  107.     var m = re.exec(u);
  108.     var p = m ? m[1] : 0;
  109.     if (_debug) console.log('current start post numebr = ' + p);
  110.     var div = $('.j-answer-rollup.recommended-answers.span-full-width');
  111.     if (!div) { return; }
  112.     p > 0 ? div.hide() : div.show();
  113. }
  114.  
  115. function pagination_in_header() {
  116.     window.addEventListener('_pagereday', function(e) {
  117.         if (_debug) console.log('_pagereday is observed');
  118.         var pg = $('.all-replies-container .j-pagination.top');
  119.         if (pg) {
  120.             $('.thread-container-wrapper .j-pagination.top').remove();
  121.             pg.clone(true).appendTo($('.thread-container-wrapper'));
  122.         }
  123.         window.removeEventListener(e.type, arguments.callee, true);
  124.     }, true);
  125.  
  126.     watchdog3 = setInterval( function() {
  127.         // watch for reply container opacity to be 1 (which is .5 while loading)
  128.         if (_debug) console.log('watchdog (for page ready) is active : ' + watchdog3);
  129.         if ($('.all-replies-container').css('opacity') == 1) {
  130.             if (_debug) console.log('_pagereday is issued');
  131.             window.dispatchEvent(new Event('_pagereday'));
  132.             stop_watchdog(watchdog3);
  133.         }
  134.     }, watch_interval);
  135. //  setTimeout(stop_watchdog, 5000, watchdog3);
  136. }
  137.  
  138. function stop_watchdog(dog) {
  139.     clearInterval(dog);
  140.     if (_debug) console.log('watchdog is inactive : ' + dog);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement