Twitter 图片查看增强

让推特图片浏览更加人性化

当前为 2020-01-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter image viewing enhancement
  3. // @name:zh-CN Twitter 图片查看增强
  4. // @name:zh-TW Twitter 圖像查看增強
  5. // @icon https://twitter.com/favicon.ico
  6. // @namespace https://moe.best/
  7. // @version 0.4.2
  8. // @description Make Twitter photo viewing more humane
  9. // @description:zh-CN 让推特图片浏览更加人性化
  10. // @description:zh-TW 讓 Twitter 照片瀏覽更人性化
  11. // @author Jindai Kirin
  12. // @include https://twitter.com/*
  13. // @license MIT
  14. // @grant none
  15. // @run-at document-end
  16. // @require https://cdn.bootcss.com/jquery/3.4.1/jquery.slim.min.js
  17. // @require https://cdn.bootcss.com/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22.  
  23. const btns = () => {
  24. const $btns = $('div[aria-labelledby="modal-header"] div[aria-haspopup="false"][role="button"]:not([data-testid])');
  25. return {
  26. prev: $($btns[0]),
  27. next: $($btns[1]),
  28. close: $($btns[2]),
  29. };
  30. };
  31. const closeImgView = () => btns().close.click();
  32. const prevImg = () => {
  33. const $btn = btns().prev;
  34. if (!$btn.attr('disabled')) $btn.click();
  35. };
  36. const nextImg = () => {
  37. const $btn = btns().next;
  38. if (!$btn.attr('disabled')) $btn.click();
  39. };
  40.  
  41. $(window).mousewheel(({ deltaY, target: { tagName, baseURI } }) => {
  42. if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
  43. switch (deltaY) {
  44. case 1:
  45. prevImg();
  46. break;
  47. case -1:
  48. nextImg();
  49. break;
  50. }
  51. }
  52. });
  53.  
  54. let x = 0;
  55. let y = 0;
  56. $(window).mousedown(({ clientX, clientY }) => {
  57. x = clientX;
  58. y = clientY;
  59. });
  60. $(window).mouseup(({ button, clientX, clientY, target: { tagName, baseURI } }) => {
  61. if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  62. const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
  63. const mx = clientX - x;
  64. if (sx <= 10 && sy <= 10) closeImgView();
  65. if (sy <= sx) {
  66. if (mx > 0) prevImg();
  67. else if (mx < 0) nextImg();
  68. }
  69. });
  70. })();