Twitter 圖像查看增強

讓 Twitter 照片瀏覽更人性化

目前為 2022-05-24 提交的版本,檢視 最新版本

  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 1.2.0
  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 GM_addStyle
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_registerMenuCommand
  18. // @run-at document-end
  19. // ==/UserScript==
  20.  
  21. // 注意 NOTICE
  22. // v1.0.0 是一次重大更新,你将不再需要设置 aria-label,并且支持所有语言。如果某一天脚本突然无法正常工作,请于脚本页面反馈,或退回至 v0.6.3。
  23. // v1.0.0 is an major update, you will no longer need to set up aria-labels and it support all languages. If one day the script not work, please feedback on the script homepage or use v0.6.3.
  24.  
  25. (() => {
  26. 'use strict';
  27.  
  28. // 滑动切换图片
  29. let enableDragToSwitch = GM_getValue('enableDragToSwitch', false);
  30. GM_registerMenuCommand('Drag to swtich images', () => {
  31. enableDragToSwitch = confirm(`Do you want to enable drag to swtich images?
  32. Current: ${enableDragToSwitch ? 'Enabled' : 'Disabled'}
  33.  
  34. Please refresh to take effect after modification.`);
  35. GM_setValue('enableDragToSwitch', enableDragToSwitch);
  36. });
  37.  
  38. if (enableDragToSwitch) GM_addStyle('img{-webkit-user-drag:none}');
  39.  
  40. const labels = {};
  41. try {
  42. const kv = {
  43. af8fa2ad: 'close',
  44. af8fa2ae: 'close',
  45. c4d53ba2: 'prev',
  46. d70740d9: 'next',
  47. d70740da: 'next',
  48. };
  49. Object.values(webpackJsonp[2][1]).forEach(fn => {
  50. if (fn.length < 3) return;
  51. try {
  52. fn(undefined, undefined, () => ({
  53. _register: () => (k, v) => {
  54. if (k in kv) labels[kv[k]] = v;
  55. },
  56. }));
  57. } catch (e) {}
  58. });
  59. } catch (error) {
  60. console.error(error);
  61. }
  62.  
  63. const getBtnByLabel = label => document.querySelector(`div[aria-labelledby="modal-header"] div[aria-label="${label}"]`);
  64. const clickBtn = name => {
  65. const $btn = getBtnByLabel(labels[name]);
  66. if ($btn) {
  67. $btn.click();
  68. return true;
  69. }
  70. return false;
  71. };
  72.  
  73. const closeImgView = () => clickBtn('close');
  74. const prevImg = () => clickBtn('prev');
  75. const nextImg = () => clickBtn('next');
  76.  
  77. window.addEventListener('wheel', ({ deltaY, target: { tagName, baseURI } }) => {
  78. if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
  79. if (deltaY < 0) prevImg();
  80. else if (deltaY > 0) nextImg();
  81. }
  82. });
  83.  
  84. if (enableDragToSwitch) {
  85. let x = 0;
  86. let y = 0;
  87. window.addEventListener('mousedown', ({ clientX, clientY }) => {
  88. x = clientX;
  89. y = clientY;
  90. });
  91. window.addEventListener('mouseup', ({ button, clientX, clientY, target: { tagName, baseURI } }) => {
  92. if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  93. const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
  94. const mx = clientX - x;
  95. if (sx <= 10 && sy <= 10) closeImgView();
  96. if (sy <= sx) {
  97. if (mx > 0) prevImg();
  98. else if (mx < 0) nextImg();
  99. }
  100. });
  101. } else {
  102. document.addEventListener(
  103. 'click',
  104. e => {
  105. const {
  106. target: { tagName, baseURI },
  107. } = e;
  108. if (!(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  109. closeImgView();
  110. e.stopPropagation();
  111. },
  112. { capture: true }
  113. );
  114. }
  115. })();