mobile01.com 键盘浏览

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

  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.12
  11. // @match *://*.mobile01.com/*
  12. // @run-at document-end
  13. // @grant none
  14. // @license MIT
  15. // @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.
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20. // 新增[我的文章]按鈕
  21. var elm=document.querySelector(".l-header__main .l-signedIn");
  22. if(elm){
  23. const css=`._myLink { display: inline-block; white-space: nowrap; height: 1em; font-size: calc(9pt + .25vw); text-align: center; padding: .3em; line-height: 1; margin: calc(.5em - .4vw) 1mm 1px 4mm; border-radius: 5pt; transition: .2s }
  24. ._myLink:hover { transition: 60ms }
  25. ._myLink:active { margin: calc(.5em - .4vw + 1px) 1mm 0 4mm; transition: 0s }
  26. @media (prefers-color-scheme: light) {
  27. ._myLink { color: #f3f3f3; background: #30A651; text-shadow: 0 0 1px #000; box-shadow: inset 0 0 0 1px #30A651, inset 0 0 0 2px #ebeae7; }
  28. ._myLink:hover { color: #f3f3f3; background: #23803d; box-shadow: inset 0 0 0 1px #3a5, inset 0 0 0 2px #fff, 0 1px 4px rgba(0, 0, 0, .4); }
  29. ._myLink:active { color: #fff; background: #23803d; box-shadow: inset 0 0 0 1px #3a5, inset 0 0 0 2px #ccc, inset 0 0 5px #000; }
  30. }
  31. @media (prefers-color-scheme: dark) {
  32. ._myLink { color: #ccc; background: #263; box-shadow: inset 0 0 0 1px #052, inset 0 0 0 2px #30A651; }
  33. ._myLink:hover { background: #23803D; box-shadow: inset 0 0 0 1px #052, inset 0 0 0 2px #30A651; }
  34. ._myLink:active { color: #ddd; background: #263; box-shadow: inset 0 0 0 1px #052, inset 0 0 0 2px #183; }
  35. }`;
  36. var currPos = document.querySelector(".l-header .l-header__left>a").getAttribute("href"),
  37. myArticle = document.createElement('a');
  38. myArticle.setAttribute("class", "_myLink");
  39.  
  40. if(currPos == "/marketindex.php") {
  41. myArticle.appendChild(document.createTextNode("我的市集"));
  42. myArticle.setAttribute("href", "/mypurchaselist.php");
  43. myArticle.setAttribute("title", "我的市集");
  44. }
  45. else {
  46. myArticle.appendChild(document.createTextNode("我的文章"));
  47. myArticle.setAttribute("href", "/participatetopics.php");
  48. myArticle.setAttribute("title", "我的文章");
  49. }
  50. elm.parentNode.appendChild(myArticle);
  51.  
  52. var cssStyle = document.createElement('style');
  53. if(cssStyle.styleSheet) cssStyle.styleSheet.cssText=css;
  54. else cssStyle.appendChild(document.createTextNode(css));
  55. document.querySelector('head').appendChild(cssStyle);
  56. }
  57.  
  58. // 移除指向本頁的連結
  59. document.querySelectorAll(".l-pagination__page.is-active>a, .c-filter a.c-iconLink--gn").forEach(function(elm){
  60. if(elm.tagName == "A") elm.removeAttribute("href")
  61. });
  62.  
  63. // 綁定方向鍵
  64. document.addEventListener("keydown", async function(e) {
  65. if(document.querySelector("input:focus, textarea:focus") || (e.shiftKey | e.ctrlKey | e.altKey | e.metaKey | e.isComposing)) return;
  66. e = e || window.event;
  67. try{
  68. switch(e.key.toLowerCase()) {
  69. case 'arrowdown':
  70. case 's':
  71. if(window.location.href.match(/\/topicdetail\.php\?/i)) document.querySelector(".c-breadCrumb__item:last-child a").click();
  72. else document.querySelector(".c-breadCrumb__item:nth-last-of-type(-n+2) a").click();
  73. break;
  74. case 'arrowleft':
  75. case 'a':
  76. document.querySelector(".l-pagination__page.is-active").previousSibling.querySelector("a.c-pagination").click();
  77. break;
  78. case 'arrowright':
  79. case 'd':
  80. document.querySelector(".l-pagination__page.is-active").nextSibling.querySelector("a.c-pagination").click();
  81. break;
  82. }
  83. } catch(err){ console.log(err); }
  84. });
  85.  
  86. // 拿掉外網連結警示
  87. document.addEventListener("mousedown", function(e) {
  88. var clickTarget = e.target.closest("a");
  89. if(!clickTarget) return;
  90. var theHref = clickTarget.getAttribute("href");
  91. theHref=decodeURIComponent(theHref.replace("/externallink.php?url=", ""));
  92. clickTarget.setAttribute("href", theHref);
  93. });
  94.  
  95. })();