Twitter 图片查看增强

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

当前为 2022-09-30 提交的版本,查看 最新版本

  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.3
  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. const i18nModule = webpackChunk_twitter_responsive_web.find(([[name]]) =>
  50. name.startsWith('i18n')
  51. );
  52. Object.values(i18nModule[1]).forEach(fn => {
  53. if (fn.length < 3) return;
  54. try {
  55. fn(undefined, undefined, () => ({
  56. _register: () => (k, v) => {
  57. if (k in kv) labels[kv[k]] = v;
  58. },
  59. }));
  60. } catch (e) {}
  61. });
  62. } catch (error) {
  63. console.error(error);
  64. }
  65.  
  66. const getBtnByLabel = label =>
  67. document.querySelector(`div[aria-labelledby="modal-header"] div[aria-label="${label}"]`);
  68. const clickBtn = name => {
  69. const $btn = getBtnByLabel(labels[name]);
  70. if ($btn) {
  71. $btn.click();
  72. return true;
  73. }
  74. return false;
  75. };
  76.  
  77. const closeImgView = () => clickBtn('close');
  78. const prevImg = () => clickBtn('prev');
  79. const nextImg = () => clickBtn('next');
  80.  
  81. window.addEventListener('wheel', ({ deltaY, target: { tagName, baseURI } }) => {
  82. if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
  83. if (deltaY < 0) prevImg();
  84. else if (deltaY > 0) nextImg();
  85. }
  86. });
  87.  
  88. if (enableDragToSwitch) {
  89. let x = 0;
  90. let y = 0;
  91. window.addEventListener('mousedown', ({ clientX, clientY }) => {
  92. x = clientX;
  93. y = clientY;
  94. });
  95. window.addEventListener(
  96. 'mouseup',
  97. ({ button, clientX, clientY, target: { tagName, baseURI } }) => {
  98. if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  99. const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
  100. const mx = clientX - x;
  101. if (sx <= 10 && sy <= 10) closeImgView();
  102. if (sy <= sx) {
  103. if (mx > 0) prevImg();
  104. else if (mx < 0) nextImg();
  105. }
  106. }
  107. );
  108. } else {
  109. document.addEventListener(
  110. 'click',
  111. e => {
  112. const {
  113. target: { tagName, baseURI },
  114. } = e;
  115. if (!(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  116. closeImgView();
  117. e.stopPropagation();
  118. },
  119. { capture: true }
  120. );
  121. }
  122. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址