NGA Pins

NGA Pins 可将顶部菜单、导航栏、页码栏固定在页面顶部,亦可通过设置取消其中某项;主帖内容较长时将作者信息固定在主帖左侧(不包含回复)

当前为 2017-12-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NGA Pins
  3. // @namespace none
  4. // @version 0.0.4.20171210
  5. // @icon http://bbs.nga.cn/favicon.ico
  6. // @description NGA Pins 可将顶部菜单、导航栏、页码栏固定在页面顶部,亦可通过设置取消其中某项;主帖内容较长时将作者信息固定在主帖左侧(不包含回复)
  7. // @author AgLandy
  8. // @include /^https?://(bbs\.ngacn\.cc|nga\.178\.com|bbs\.nga\.cn)/
  9. // @grant none
  10. // @require http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
  11. // ==/UserScript==
  12.  
  13.  
  14. (function(){
  15.  
  16. commonui.pins = {};
  17. var pins = commonui.pins;
  18.  
  19. function testStorage(s){
  20. if(!!s){
  21. try{
  22. s.testkey = 'testvalue';
  23. s.removeItem('testkey');
  24. return true;
  25. }
  26. catch(e){
  27. return false;
  28. }
  29. }
  30. else
  31. return false;
  32. }
  33.  
  34. function init(){
  35. let args = {};
  36. if(localStorage.pins)
  37. args = JSON.parse(localStorage.pins);
  38. else{
  39. args = {a: 56, b: 42, c: 43};
  40. localStorage.pins = JSON.stringify(args);
  41. }
  42. __SETTING.pins = function(){
  43. this.o = commonui.createCommmonWindow();
  44. this.o._.addContent(null);
  45. var $ = window._$;
  46. this.o._.addTitle('Pins 设置');
  47. let update = function(t){
  48. if(t.checked)
  49. pins.args[t.name] = parseInt(t.value);
  50. else
  51. pins.args[t.name] = 0;
  52. localStorage.pins = JSON.stringify(pins.args);
  53. pins.update();
  54. },
  55. z = [['a', '56', '顶部菜单'], ['b','42', '导航栏'], ['c','43', '页码栏']];
  56. for(let i = 0; i < z.length; i++){
  57. let k = $('/input').$0('type','checkbox','checked',0,'name',z[i][0],'value',z[i][1])._.on('click', function(e){update(e.target);});
  58. this.o._.addContent(
  59. k,
  60. z[i][2],
  61. $('/br')
  62. );
  63. if(pins.args[z[i][0]])
  64. k._.attr('checked', 1);
  65. }
  66. this.o._.show();
  67. };
  68. commonui.mainMenu.data[401] = {innerHTML: 'Pins 设置', on: {event: 'click', func: function(){__SETTING.pins();}}, parent: 18};
  69. commonui.mainMenu.data[18].subKeys.push(401);
  70. return args;
  71. }
  72.  
  73. pins.update = function(){
  74.  
  75. var $Q = jQuery.noConflict(),
  76. args = pins.args,
  77. hA = args.a + 8,
  78. hB = args.b && args.b + 8,
  79. hC = args.c && args.c + 8,
  80. a = $Q('#mainmenu'),
  81. b = $Q('#m_nav'),
  82. c = $Q('#m_pbtntop'),
  83. t = $Q('#posterinfo0').closest('td'),
  84. h = c.length && c.offset().top - hA - hB || b.offset().top - hA,
  85. z = [[a, 'pinsTopBar'], [b, 'pinsNavBar'], [c, 'pinsPageBar'], [t, 'pinsAuthorInfo']],
  86. newDiv = function(i){
  87. return $Q('<div id="' + z[i][1] + '" />').append(z[i][0].children()).appendTo(z[i][0]);
  88. };
  89.  
  90. //还原默认
  91. for(let i = 0; i < z.length; i++){
  92. if($Q('#' + z[i][1]).length){
  93. $Q('#' + z[i][1]).contents().unwrap();
  94. z[i][0].find('img.avatar').css({'margin-left':''});
  95. }
  96. }
  97. $Q(window).unbind('.pins');
  98.  
  99. //顶部菜单
  100. if(args.a){
  101. let d = newDiv(0);
  102. d.css({'height':a.css('height'), 'width':'100%', 'overflow':'hidden', 'position':'fixed', 'top':'0px', 'z-index':'3', 'opacity':'0.95'});
  103. $Q(window).bind('scroll.pins', function(){
  104. if($Q(window).scrollTop() > $Q('#custombg').height())
  105. d.css({'border-bottom':'1px solid #' + __COLOR.shadow1.replace(/;.+;/,'').replace(/^.+#/,'')});
  106. else
  107. d.css({'border-bottom':''});
  108. });
  109. }
  110.  
  111. //导航栏
  112. if(args.b){
  113. let d = newDiv(1);
  114. b.css({'height':b.css('height')});
  115. b.find('.bbsinfo').prependTo(b);
  116. $Q(window).bind('scroll.pins', function(){
  117. if($Q(window).scrollTop() > h)
  118. d.css({'position':'fixed', 'top':hA + 'px', 'z-index':'3', 'opacity':'0.95'});
  119. else
  120. d.css({'position':'', 'top':'', 'z-index':'', 'opacity':''});
  121. });
  122. }
  123.  
  124. //页码栏
  125. if(args.c){
  126. let d = newDiv(2);
  127. c.css({'height':c.css('height')});
  128. c.find('tbody').css({'pointer-events':'auto'});
  129. $Q(window).bind('scroll.pins', function(){
  130. if($Q(window).scrollTop() > h)
  131. d.css({'width':'calc(100% - 20px)', 'position':'fixed', 'top':(hA + hB) + 'px', 'z-index':'2', 'opacity':'0.95', 'pointer-events':'none'});
  132. else
  133. d.css({'width':'', 'position':'', 'top':'', 'z-index':'', 'opacity':''});
  134. });
  135. }
  136.  
  137. //作者信息
  138. if($Q('#posterinfo0').length && $Q('#posterinfo0').closest('td').outerHeight() > 600){
  139. t.css({'min-width':'197px'});
  140. let d = newDiv(3),
  141. h1 = hA + hB + hC + 6,
  142. h2 = d.offset().top - h1,
  143. w = function(){
  144. d.css('width', t.css('width'));
  145. d.find('img.avatar').css({'margin-left':d.width() % 2 + 'px'});
  146. };
  147. $Q(window).bind('click.pins', function(){setTimeout(function(){w();}, 5);});
  148. $Q(window).bind('transitionend.pins', function(){setTimeout(function(){w();}, 5);});
  149. $Q(window).bind('resize.pins', function(){w();});
  150. $Q(window).bind('scroll.pins', function(){
  151. if($Q(window).scrollTop() > h2 && $Q(window).scrollTop() < t.height() - d.height() + h2){
  152. d.css({'width':t.css('width'), 'position':'fixed', 'top':h1 + 'px'});
  153. d.find('img.avatar').css({'margin-left':d.width() % 2 + 'px'});
  154. }
  155. else if($Q(window).scrollTop() >= t.height() - d.height() + h2){
  156. d.css({'width':t.css('width'), 'position':'fixed', 'top':(h1 - $Q(window).scrollTop() + t.height() - d.height() + h2) + 'px'});
  157. d.find('img.avatar').css({'margin-left':d.width() % 2 + 'px'});
  158. }
  159. else{
  160. d.css({'width':'', 'position':'', 'top':''});
  161. d.find('img.avatar').css({'margin-left':''});
  162. }
  163. });
  164. }
  165.  
  166. $Q(window).triggerHandler('scroll.pins');
  167.  
  168. };
  169.  
  170. pins.args = !testStorage(window.localStorage) ? {a: 56, b: 42, c: 43} : init();
  171.  
  172. pins.update();
  173.  
  174. })();
  175.  
  176.  
  177.  
  178.  
  179.  
  180.