unity

hkgalden utilities

当前为 2014-11-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name unity
  3. // @version 0.2.1c
  4. // @description hkgalden utilities
  5. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
  6. // @include http://*hkgalden.com/*
  7. // @namespace hkga unity
  8. // @copyright ignite
  9. // ==/UserScript==
  10. (function(W, D, G, $, $$){
  11. 'use strict';
  12. $ = W.$;
  13. $.fn.findAtDepth = function(selector, maxDepth){
  14. var depths = [], i;
  15. if(maxDepth > 0){
  16. for(i = 1; i <= maxDepth; i++) depths.push('> ' + new Array(i).join('* > ') + selector);
  17. selector = depths.join(', ');
  18. }
  19. return this.find(selector);
  20. };
  21. $$ = function(q){return D.querySelectorAll(q)};
  22. if(!G.GM_getValue || (G.GM_getValue.toString && G.GM_getValue.toString().indexOf("not supported") > -1)){
  23. G.GM_getValue=function(key,def){return localStorage[key] || def;};
  24. G.GM_setValue=function (key,value){return localStorage[key]=value;};
  25. G.GM_deleteValue=function (key) {return delete localStorage[key];};
  26. }
  27. var CHKBX_OPTS = ['nextPageHideAuthor', 'lessQuote', 'clickToShowPics'],
  28. CHKBX_OPTS_TITLE = {
  29. nextPageHideAuthor: '只在第一頁顯示 #0',
  30. lessQuote: '限制Quote數量',
  31. clickToShowPics: '不自動載入圖片'
  32. },
  33. CFG = {};
  34. for(var i = 0, l = CHKBX_OPTS.length; i < l; i++){
  35. CFG[CHKBX_OPTS[i]] = G.GM_getValue(CHKBX_OPTS[i], false);
  36. }
  37. var API = {}, _exec;
  38. API.isLoggedIn = $$('#gb .actp a[href="/my"]').length;
  39. // topics
  40. _exec = /\/[A-Za-z]{2}\/(\d+)/.exec(location.href);
  41. API.topics = {};
  42. API.topics.is = location.href.indexOf('topics/') > 0;
  43. API.topics.page = API.topics.is ? _exec ? +_exec[1] : 1 : 0;
  44. // view
  45. _exec = /\/page\/(\d+)/.exec(location.href);
  46. API.view = {};
  47. API.view.is = location.href.indexOf('view/') > 0;
  48. API.view.page = API.view.is ? _exec ? +_exec[1] : 1 : 0;
  49. API.view.author = API.view.is ? $$('.gpt.author .dtl>.unm>.name')[0].innerHTML : null;
  50. // insert config panel
  51. var panel = $('<a/>', {
  52. href:'#',
  53. text:'unity',
  54. }).on('click', function(e){
  55. e.preventDefault();
  56. $('body').append('<div id="unity--panel" style="background:rgba(0,0,0,.4);height:100%;left:0;position:absolute;top:'+D.body.scrollTop+'px;width:100%;z-index:999999">\
  57. <div style="display:table;height:100%;width:100%">\
  58. <div style="display:table-cell;height:100%;vertical-align: middle;width:100%">\
  59. <div id="unity--config"></div>\
  60. </div>\
  61. </div>\
  62. </div>').css({overflow:'hidden'});
  63. var chkbxes = '<table class="nxtb" style="background:#cec;width:540px">';
  64. for(var i = 0, l = CHKBX_OPTS.length; i < l; i++){
  65. chkbxes += '<tr><td>'+CHKBX_OPTS_TITLE[CHKBX_OPTS[i]]+'</td><td><input id="'+CHKBX_OPTS[i]+'" type="checkbox"'+(G.GM_getValue(CHKBX_OPTS[i]) ? ' checked' : '')+'></td>';
  66. }
  67. chkbxes += '</table>';
  68. $('#unity--config').append([
  69. $('<h2 style="background:#fe5;border-radius:50%;cursor:pointer;font-weight:700;line-height:64px;margin:0;position:absolute;right:24px;top:24px;width:64px;">×</h2>').on('click', function(){
  70. $(D).off('.unity');
  71. $('#unity--panel').remove();
  72. $('body').css({overflow:'auto'});
  73. }),
  74. chkbxes,
  75. '<br>',
  76. $('<button style="background:#eee;border-radius:50%;height:64px;margin:32px 0;width:64px">確認</button>').on('click', function(){
  77. location.reload();
  78. })
  79. ]);
  80. $(D).on('change.unity', '#unity--config :checkbox', function(){
  81. G.GM_setValue(this.getAttribute('id'), this.checked);
  82. });
  83. });
  84. panel.insertBefore('#gb .actp :first-child');
  85. // mod
  86. var DOMwatcher = function(root, callback){
  87. var __appendChild = document.body.appendChild,
  88. patch = function(node){
  89. if(typeof node.appendChild !== 'undefined' && node.nodeName !== '#text'){
  90. node.appendChild = function(incomingNode){
  91. callback(node, incomingNode);
  92. patch(incomingNode);
  93. walk(incomingNode);
  94. __appendChild.call(node, incomingNode);
  95. };
  96. }
  97. walk(node);
  98. },
  99. walk = function(node){
  100. var i = node.childNodes.length;
  101. while(i--) patch(node.childNodes[i]);
  102. };
  103. patch(root);
  104. };
  105. if(API.view.is){
  106. if(CFG.nextPageHideAuthor && API.view.page > 1){
  107. var ctn = $('.gpt.author>.r>.ctn');
  108. ctn.hide().after($('<div class="ctn"><a href="#" style="color:#499">按此顯示 #0</a></div>').on('click',function(e){
  109. e.preventDefault();
  110. $(this).remove() && ctn.show();
  111. }));
  112. }
  113. var hideQuotes = function(){
  114. if(!CFG.lessQuote) return;
  115. $.each($$('.gpt.author>.r>.ctn, .gpt.replies>.r>.ctn'), function(i, e){
  116. var d = $(e).findAtDepth('blockquote:not(.united)', 3), b;
  117. if(d.length === 3){
  118. b = $(d[2]);
  119. b.hide().addClass('united');
  120. $('<blockquote class="united" style="background:#cea;color:#000;cursor:pointer;display:inline-block;margin-bottom:24px">顯示更多引用回覆</blockquote>').on('click',function(){
  121. b.show();
  122. $(this).remove();
  123. }).insertAfter(b);
  124. }
  125. });
  126. },
  127. hidePics = function(){
  128. if(!CFG.clickToShowPics) return;
  129. $('body').css({overflow:'hidden'});
  130. $.each($$('.gpt.author>.r>.ctn img[data-original], .gpt.replies>.r>.ctn img[data-original]'), function(i, e){
  131. var i = $('<img alt src="'+this.getAttribute('data-original')+'" style="display:none">');
  132. i.on('load', function(){
  133. if(i[0].width > 72 && i[0].height > 72){
  134. i.parent().after($('<div class="ico" style="cursor:pointer;display:inline-block;font-size:48px;height:48px;line-height:48px;text-align:center;width:48px;"></div>').on('click',function(e){
  135. $(this).remove() && i.show();
  136. }));
  137. }else i.show();
  138. });
  139. $(this).replaceWith(i);
  140. });
  141. $('body').css({overflow:'auto'});
  142. };
  143. hideQuotes();
  144. hidePics();
  145. $(D).on('DOMNodeInserted', '#main>article', function(e){
  146. if(e.target.className.indexOf('replies') > 0){
  147. hideQuotes();
  148. hidePics();
  149. }
  150. });
  151. }
  152. })(unsafeWindow, document, this);