Twitter 图片查看增强

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

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

  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.1
  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 closeImgView = () => $('div[aria-labelledby="modal-header"] div[aria-haspopup="false"][role="button"][style]:not([data-testid])').click();
  24. const btnGroup = () => $('div[aria-labelledby="modal-header"] div[aria-haspopup="false"][role="button"]:not([data-testid]):not([style])');
  25. const prevImg = () => {
  26. const $btn = $(btnGroup()[0]);
  27. if (!$btn.attr('disabled')) $btn.click();
  28. };
  29. const nextImg = () => {
  30. const $btn = $(btnGroup()[1]);
  31. if (!$btn.attr('disabled')) $btn.click();
  32. };
  33.  
  34. $(window).mousewheel(({ deltaY, target: { tagName, baseURI } }) => {
  35. if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
  36. switch (deltaY) {
  37. case 1:
  38. prevImg();
  39. break;
  40. case -1:
  41. nextImg();
  42. break;
  43. }
  44. }
  45. });
  46.  
  47. let x = 0;
  48. let y = 0;
  49. $(window).mousedown(({ clientX, clientY }) => {
  50. x = clientX;
  51. y = clientY;
  52. });
  53. $(window).mouseup(({ button, clientX, clientY, target: { tagName, baseURI } }) => {
  54. if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  55. const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
  56. const mx = clientX - x;
  57. if (sx <= 10 && sy <= 10) closeImgView();
  58. if (sy <= sx) {
  59. if (mx > 0) prevImg();
  60. else if (mx < 0) nextImg();
  61. }
  62. });
  63. })();