mobile01.com 键盘浏览

用方向键 [← / a] 切换前一页,[→ / d] 切换次一页,[↓ / s] 退回外层分类。右上角加入 [我的文章] 连结。

当前为 2020-07-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name mobile01.com - navigate with keyboard
  3. // @name:zh-TW mobile01.com 鍵盤瀏覽
  4. // @name:zh-CN mobile01.com 键盘浏览
  5. // @description:en press keyboard [← / a] to the previous page, [→ / d] to the next page, [↓ / s] back to parent classification. Add "My Article" to the top right corner.
  6. // @description:zh-TW 用方向鍵 [← / a] 切換前一頁,[→ / d] 切換次一頁,[↓ / s] 退回外層分類。右上角加入 [我的文章] 連結。
  7. // @description:zh-CN 用方向键 [← / a] 切换前一页,[→ / d] 切换次一页,[↓ / s] 退回外层分类。右上角加入 [我的文章] 连结。
  8. // @namespace https://greasyfork.org/zh-TW/users/393133-evan-tseng
  9. // @author Evan Tseng
  10. // @version 1.04
  11. // @match *://www.mobile01.com/*
  12. // @grant none
  13. // @description press keyboard [← / a] to the previous page, [→ / d] to the next page, [↓ / s] back to parent classification. Add "My Article" to the top right corner.
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. var retry=0;
  19. var myArticleLink = (function(){
  20. var elm=null;
  21. if((document.readyState !== "loading") && (elm=document.querySelector(".l-signedIn"))){
  22. // 登入後,畫面右上角顯示[我的文章]連結
  23. var css=`._myArticle{display:inline-block;white-space:nowrap;height:1em;font-size:calc(9pt + .25vw);text-align:center;padding:.3em;line-height:1;margin:calc(.6em - .4vw) 1mm 1px;color:#f3f3f3;background:#30A651;border-radius:5pt;text-shadow:0 0 1px #000;box-shadow:inset 0 0 0 1px #30A651,inset 0 0 0 2px #ebeae7;transition:.2s}
  24. ._myArticle:hover{color:#fff;background:#23803d;box-shadow:inset 0 0 0 1px #3a5,inset 0 0 0 2px #fff, 0 1px 4px rgba(0,0,0,.4);transition:60ms}
  25. ._myArticle:active{color:#ddd;background:#23803d;margin:calc(.6em - .4vw + 1px) 1mm 0;box-shadow:inset 0 0 0 1px #3a5,inset 0 0 0 2px #ccc,inset 0 0 5px #000;transition:0s}`,
  26. cssStyle=document.createElement('style');
  27. if(cssStyle.styleSheet) cssStyle.styleSheet.cssText=css;
  28. else cssStyle.appendChild(document.createTextNode(css));
  29. document.querySelector('head').appendChild(cssStyle);
  30. var myArticle=document.createElement('a');
  31. myArticle.appendChild(document.createTextNode("我的文章"));
  32. myArticle.setAttribute("href","/participatetopics.php");
  33. myArticle.setAttribute("class","_myArticle");
  34. myArticle.setAttribute("title","我的文章");
  35. elm.appendChild(myArticle);
  36. // 移除指向本頁的連結
  37. elm=document.querySelectorAll(".l-pagination__page.is-active>a, .c-filter a.c-iconLink--gn");
  38. for(let i in elm) if(elm[i].tagName=="A") elm[i].removeAttribute("href");
  39. }
  40. else {
  41. if(retry++ < 10) setTimeout(myArticleLink, 1000);
  42. }
  43. })();
  44.  
  45. var fnKey = { shift: false, ctrl:false, alt:false, meta:false }
  46. document.addEventListener("keydown", function(e) {
  47. e = e || window.event;
  48. switch(e.which || e.keyCode) {
  49. case 16: // shift
  50. fnKey.shift = true;
  51. break;
  52. case 17: // ctrl
  53. fnKey.ctrl = true;
  54. break;
  55. case 18: // alt
  56. fnKey.alt = true;
  57. break;
  58. case 91: // left Meta
  59. case 93: // right Meta
  60. fnKey.meta = true;
  61. break;
  62. }
  63. });
  64.  
  65. document.addEventListener("keyup", function(e) {
  66. e = e || window.event;
  67. switch(e.which || e.keyCode) {
  68. case 16: // shift
  69. fnKey.shift = false;
  70. break;
  71. case 17: // ctrl
  72. fnKey.ctrl = false;
  73. break;
  74. case 18: // alt
  75. fnKey.alt = false;
  76. break;
  77. case 91: // left Meta
  78. case 93: // right Meta
  79. fnKey.meta = false;
  80. break;
  81. }
  82. });
  83.  
  84. document.addEventListener("keydown", async function(e) {
  85. if(document.querySelector("input:focus, textarea:focus") || (fnKey.shift | fnKey.ctrl | fnKey.alt | fnKey.meta)) return;
  86. e = e || window.event;
  87. try{
  88. switch(e.which || e.keyCode) {
  89. case 40: // down
  90. case 83: // 's'
  91. if(window.location.href.match(/\/topicdetail\.php\?/i))
  92. document.querySelector(".c-breadCrumb__item:last-child a").click();
  93. else
  94. document.querySelector(".c-breadCrumb__item:nth-last-of-type(-n+2) a").click();
  95. break;
  96. case 65: // 'a'
  97. case 37: // ArrowLeft
  98. document.querySelector(".l-pagination__page.is-active").previousSibling.querySelector("a.c-pagination").click();
  99. break;
  100. case 68: // 'd'
  101. case 39: // ArrowRight
  102. document.querySelector(".l-pagination__page.is-active").nextSibling.querySelector("a.c-pagination").click();
  103. break;
  104. }
  105. } catch(e){ console.log(e); }
  106. });
  107.  
  108. })();