unity

hkgalden utilities

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

  1. // ==UserScript==
  2. // @name unity
  3. // @version 0.1a
  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'], CHKBX_OPTS_TITLE = {nextPageHideAuthor:'只在第一頁顯示 #0',lessQuote:'限制Quote數量'}, CFG = {};
  28. for(var i = 0, l = CHKBX_OPTS.length; i < l; i++){
  29. CFG[CHKBX_OPTS[i]] = G.GM_getValue(CHKBX_OPTS[i], false);
  30. }
  31. var API = {}, _exec;
  32. API.isLoggedIn = $$('#gb .actp a[href="/my"]').length;
  33. // topics
  34. _exec = /\/[A-Za-z]{2}\/(\d+)/.exec(location.href);
  35. API.topics = {};
  36. API.topics.is = location.href.indexOf('topics/') > 0;
  37. API.topics.page = API.topics.is ? _exec ? +_exec[1] : 1 : 0;
  38. // view
  39. _exec = /\/page\/(\d+)/.exec(location.href);
  40. API.view = {};
  41. API.view.is = location.href.indexOf('view/') > 0;
  42. API.view.page = API.view.is ? _exec ? +_exec[1] : 1 : 0;
  43. API.view.author = API.view.is ? $$('.gpt.author .dtl>.unm>.name')[0].innerHTML : null;
  44. // insert config panel
  45. var panel = $('<a/>', {
  46. href:'#',
  47. text:'unity',
  48. }).on('click', function(e){
  49. e.preventDefault();
  50. $('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">\
  51. <div style="display:table;height:100%;width:100%">\
  52. <div style="display:table-cell;height:100%;vertical-align: middle;width:100%">\
  53. <div id="unity--config"></div>\
  54. </div>\
  55. </div>\
  56. </div>').css({overflow:'hidden'});
  57. var chkbxes = '<table class="nxtb" style="background:#cec;width:540px">';
  58. for(var i = 0, l = CHKBX_OPTS.length; i < l; i++){
  59. 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>';
  60. }
  61. chkbxes += '</table>';
  62. $('#unity--config').append([
  63. chkbxes,
  64. $('<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(){
  65. $(D).off('.unity');
  66. $('#unity--panel').remove();
  67. $('body').css({overflow:'auto'});
  68. })
  69. ]);
  70. $(D).on('change.unity', '#unity--config :checkbox', function(){
  71. G.GM_setValue(this.getAttribute('id'), this.checked);
  72. });
  73. });
  74. panel.insertBefore('#gb .actp :first-child');
  75. // mod
  76. var DOMwatcher = function(root, callback){
  77. var __appendChild = document.body.appendChild,
  78. patch = function(node){
  79. if(typeof node.appendChild !== 'undefined' && node.nodeName !== '#text'){
  80. node.appendChild = function(incomingNode){
  81. callback(node, incomingNode);
  82. patch(incomingNode);
  83. walk(incomingNode);
  84. __appendChild.call(node, incomingNode);
  85. };
  86. }
  87. walk(node);
  88. },
  89. walk = function(node){
  90. var i = node.childNodes.length;
  91. while(i--) patch(node.childNodes[i]);
  92. };
  93. patch(root);
  94. };
  95. if(API.view.is){
  96. if(CFG.nextPageHideAuthor && API.view.page > 1){
  97. var ctn = $('.gpt.author>.r>.ctn');
  98. ctn.hide().after($('<a href="#" style="color:#499;display:inline-block;margin:12px">按此顯示 #0</a>').on('click',function(){
  99. $(this).remove() && ctn.show();
  100. }));
  101. }
  102. var hideQuotes = function(){
  103. if(!CFG.lessQuote) return;
  104. $.each($$('.gpt.author>.r>.ctn, .gpt.replies>.r>.ctn'), function(i, e){
  105. var d = $(e).findAtDepth('blockquote:not(.united)', 3), b;
  106. if(d.length === 3){
  107. b = $(d[2]);
  108. b.hide().addClass('united');
  109. $('<blockquote class="united" style="background:#cea;color:#000;cursor:pointer;display:inline-block;margin-bottom:24px">顯示更多引用回覆</blockquote>').on('click',function(){
  110. b.show();
  111. $(this).remove();
  112. }).insertAfter(b);
  113. }
  114. });
  115. };
  116. hideQuotes();
  117. $(D).on('DOMNodeInserted', '#main>article', function(e){
  118. if(e.target.className.indexOf('replies') > 0){
  119. hideQuotes();
  120. hidden = !0;
  121. }
  122. });
  123. }
  124. })(unsafeWindow, document, this);