Twitter 图片查看增强

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

目前为 2020-04-09 提交的版本,查看 最新版本

  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.5.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 GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_registerMenuCommand
  17. // @grant GM_openInTab
  18. // @run-at document-end
  19. // @require https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js
  20. // @require https://cdn.jsdelivr.net/npm/jquery-mousewheel@3.1.13/jquery.mousewheel.min.js
  21. // ==/UserScript==
  22.  
  23. // 注意 NOTICE
  24. // v0.5.1 开始包含一些重大更新,如果你的推特界面语言不是简体中文/繁体中文/英语/日语,请访问脚本主页以了解这一变更,否则你可能无法正常使用。
  25. // There are some significant changes in v0.5.1, please visit the homepage of this script for more information if your twitter display language is not one of English, Japanese, Simplified Chinese, Traditional Chinese.
  26.  
  27. (function() {
  28. 'use strict';
  29.  
  30. const safetyParseJSON = str => {
  31. try {
  32. return JSON.parse(str);
  33. } catch (error) {}
  34. };
  35.  
  36. const defaultLabelsByLang = {
  37. zh: { close: '关闭', prev: '上一个', next: '下一步' },
  38. 'zh-Hant': { close: '關閉', prev: '上一個', next: '下一步' },
  39. en: { close: 'Close', prev: 'Previous', next: 'Next' },
  40. ja: { close: '閉じる', prev: '前', next: '次' },
  41. };
  42.  
  43. const defaultLabels = defaultLabelsByLang[$('html').attr('lang')] || defaultLabelsByLang.zh;
  44. const labels = (() => {
  45. const setting = GM_getValue('labels', '');
  46. return setting ? safetyParseJSON(setting) || defaultLabels : defaultLabels;
  47. })();
  48. console.log('aria-labels', labels);
  49. GM_registerMenuCommand('Set aria-labels', () => {
  50. let input, list;
  51. let error = false;
  52. do {
  53. const current = GM_getValue('labels', '');
  54. input = prompt(`Please input the aria-label of Close, Previous, Next button and join them by comma (,). Submit an empty string will reset it to default.${error ? '\n\nINPUT ERROR' : ''}`, input || current ? Object.values(safetyParseJSON(current) || {}).join(',') : '');
  55. if (input === null) return;
  56. input = input.trim();
  57. if (input.length === 0) list = Object.values(defaultLabels);
  58. else list = input.split(',').map(label => label.trim());
  59. error = list.length !== Object.keys(labels).length;
  60. } while (error);
  61. Object.keys(labels).forEach((key, index) => {
  62. labels[key] = list[index];
  63. });
  64. GM_setValue('labels', input.length ? JSON.stringify(labels) : '');
  65. console.log('aria-labels-setting', GM_getValue('labels'));
  66. console.log('aria-labels', labels);
  67. });
  68.  
  69. const getBtnByLabel = label => $(`div[aria-labelledby="modal-header"] div[aria-label="${label}"]`);
  70.  
  71. const closeImgView = () => {
  72. const $btn = getBtnByLabel(labels.close);
  73. if ($btn.length) $btn.click();
  74. else if (confirm("It seems that you haven't set the right aria-labels yet. Please visit the homepage of this script for more information.")) GM_openInTab('https://greasyfork.org/zh-CN/scripts/387918', false);
  75. };
  76. const prevImg = () => getBtnByLabel(labels.prev).click();
  77. const nextImg = () => getBtnByLabel(labels.next).click();
  78.  
  79. $(document).mousewheel(({ deltaY, target: { tagName, baseURI } }) => {
  80. if (tagName == 'IMG' && /\/photo\//.test(baseURI)) {
  81. switch (deltaY) {
  82. case 1:
  83. prevImg();
  84. break;
  85. case -1:
  86. nextImg();
  87. break;
  88. }
  89. }
  90. });
  91.  
  92. let x = 0;
  93. let y = 0;
  94. $(document).mousedown(({ clientX, clientY }) => {
  95. x = clientX;
  96. y = clientY;
  97. });
  98. $(document).mouseup(({ button, clientX, clientY, target: { tagName, baseURI } }) => {
  99. if (button !== 0 || !(tagName == 'IMG' && /\/photo\//.test(baseURI))) return;
  100. const [sx, sy] = [clientX - x, clientY - y].map(Math.abs);
  101. const mx = clientX - x;
  102. if (sx <= 10 && sy <= 10) closeImgView();
  103. if (sy <= sx) {
  104. if (mx > 0) prevImg();
  105. else if (mx < 0) nextImg();
  106. }
  107. });
  108. })();