userscripts 只显示中文

在 userscripts、greasyfork 脚本页面只显示中文脚本,支持 AutoPager 和其它翻页脚本。

目前为 2014-06-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name userscripts 只显示中文
  3. // @namespace https://github.com/ywzhaiqi
  4. // @version 1.5
  5. // @author ywzhaiqi@gmail.com
  6. // @description 在 userscripts、greasyfork 脚本页面只显示中文脚本,支持 AutoPager 和其它翻页脚本。
  7.  
  8. // @include http*://greasyfork.org/scripts*
  9. // @include http*://userscripts.org*/scripts*
  10. // @include http*://userscripts-mirror.org*/scripts*
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @run-at document-end
  14. // ==/UserScript==
  15.  
  16. (function(){
  17.  
  18. var Config = {
  19. debug: false,
  20. buttonID: 'gm-userscript-show-chinese',
  21. // 需要?稍微减少 MutationObserver 的触发?
  22. igoreAddedNodeName: ['script', 'hr', 'p', 'button'],
  23.  
  24. chineseRegExp: /[\u4E00-\u9Fa5]/,
  25. // 日文包含:1、片假名(Katakana) 2、平假名(Hiragana) 3、汉字
  26. // 下面正则检测的是 片假名 和 平假名
  27. japaneseRegexp: /[\u30A0-\u30FF]|[\u3040-\u309F]/,
  28. };
  29.  
  30.  
  31. var Sites = {
  32. // line、test 的后一个使用了 "Greasy Fork (Firefox)" 脚本
  33. 'greasyfork.org': {
  34. button: '#script-list-filter', // 按钮插入的位置
  35. line: '.script-list > li, #script-table > tr[id]', // 要隐藏的行(css 选择器)
  36. test: 'article, td', // 要检测的对象(css 选择器)
  37. },
  38.  
  39. 'userscripts.org': {
  40. button: '#content th.la',
  41. line: 'tr[id^="scripts-"]',
  42. test: '.title, .desc',
  43. },
  44. };
  45.  
  46. Sites['userscripts-mirror.org'] = Sites['userscripts'];
  47.  
  48.  
  49. var debug = Config.debug ? console.log.bind(console) : function() {};
  50.  
  51. var onlyChinese,
  52. info,
  53. style;
  54.  
  55. function init() {
  56. var hostname = location.hostname;
  57.  
  58. info = Sites[hostname];
  59. if (!info) {
  60. return;
  61. }
  62.  
  63. var buttonParent = document.querySelector(info.button);
  64. if (!buttonParent) {
  65. return;
  66. }
  67.  
  68. // load setting
  69. onlyChinese = GM_getValue('onlyChinese', false);
  70.  
  71. if (onlyChinese) {
  72. addHiddenStyle();
  73. }
  74.  
  75. addButton(buttonParent);
  76.  
  77. checkScriptNode();
  78.  
  79. // 增加对 翻页脚本的支持
  80. addMutationObserver('body', checkScriptNode);
  81. }
  82.  
  83. function addHiddenStyle() {
  84. var cssArr = info.line.split(',').map(function(selector) {
  85. return selector + ':not([gm-script-lan="zh"]) {display:none !important}';
  86. });
  87.  
  88. style = document.createElement('style');
  89. style.textContent = cssArr.join('\n');
  90. document.head.appendChild(style);
  91. }
  92.  
  93. function removeHiddenStyle() {
  94. if (style) {
  95. document.head.removeChild(style);
  96. }
  97. }
  98.  
  99.  
  100. function checkScriptNode() {
  101. var scripts = document.querySelectorAll(info.line);
  102. var script,
  103. nodes;
  104. for (var i = scripts.length - 1; i >= 0; i--) {
  105. script = scripts[i];
  106.  
  107. if (script.hasAttribute('gm-script-lan')) {
  108. continue;
  109. }
  110.  
  111. nodes = script.querySelectorAll(info.test);
  112. if (nodes) {
  113. script.setAttribute('gm-script-lan', getScriptLan(nodes));
  114. }
  115. }
  116. }
  117.  
  118. function getScriptLan(nodes) {
  119. for (var i = nodes.length - 1, text; i >= 0; i--) {
  120. text = nodes[i].textContent;
  121.  
  122. if (text.match(Config.japaneseRegexp)) {
  123. return 'jp';
  124. }
  125.  
  126. if (text.match(Config.chineseRegExp)) {
  127. return 'zh';
  128. }
  129. }
  130.  
  131. return 'other';
  132. }
  133.  
  134. function addButton(parent) {
  135. var button = document.createElement('button');
  136. button.type = "button";
  137. button.id = Config.buttonID;
  138. button.innerHTML = onlyChinese ? "显示全部" : "只显示中文";
  139. button.onclick = function(){
  140. if (onlyChinese) {
  141. removeHiddenStyle();
  142. button.innerHTML = "只显示中文";
  143. } else {
  144. addHiddenStyle();
  145. button.innerHTML = "显示全部";
  146. }
  147.  
  148. onlyChinese = !onlyChinese;
  149. GM_setValue('onlyChinese', onlyChinese);
  150. };
  151.  
  152. parent.appendChild(button);
  153. }
  154.  
  155. function addMutationObserver(selector, callback) {
  156. var watch = document.querySelector(selector);
  157. if (!watch) return;
  158.  
  159. var observer = new MutationObserver(function(mutations){
  160. // 排除设定里的插入对象
  161. var nodeAdded = mutations.some(function(x){
  162. return [].some.call(x.addedNodes, function(node) {
  163. return node.localName && Config.igoreAddedNodeName.indexOf(node.localName) == -1;
  164. });
  165. });
  166. if (nodeAdded) {
  167. // console.log(mutations)
  168. // observer.disconnect();
  169. callback(mutations);
  170. }
  171. });
  172. observer.observe(watch, {childList: true, subtree: true});
  173. }
  174.  
  175.  
  176. // function showAll () {
  177. // var trs = document.querySelectorAll(".gm-mhide");
  178. // for (var i = trs.length - 1; i >= 0; i--) {
  179. // trs[i].classList.remove("gm-mhide");
  180. // }
  181.  
  182. // onlyChinese = false;
  183. // }
  184.  
  185. // // 隐藏其它,只显示中文
  186. // function hideOthers(){
  187. // var scripts = document.querySelectorAll(info.line + ':not(.gm-mhide)'),
  188. // script, checkElems;
  189.  
  190. // debug('要检查的行是:', scripts, '选择器是:' + info.line + ':not(.gm-mhide)');
  191.  
  192. // var hasChinese = function (elems) {
  193. // for (var i = elems.length - 1, text; i >= 0; i--) {
  194. // text = elems[i].textContent;
  195.  
  196. // if (text.match(Config.japaneseRegexp)) {
  197. // continue;
  198. // }
  199.  
  200. // if (text.match(Config.chineseRegExp)) {
  201. // return true;
  202. // }
  203. // }
  204.  
  205. // return false;
  206. // };
  207.  
  208. // for (var i = scripts.length - 1; i >= 0; i--) {
  209. // script = scripts[i];
  210.  
  211. // checkElems = script.querySelectorAll(info.test);
  212. // if (!hasChinese(checkElems)) {
  213. // script.classList.add('gm-mhide');
  214. // }
  215. // }
  216.  
  217. // onlyChinese = true;
  218. // }
  219.  
  220.  
  221. init();
  222.  
  223.  
  224. })();
  225.