Greasy Fork 还支持 简体中文。

Twitter media-only filter toggle.

Toggle non-media tweets on and off on the home timeline, for the power-viewer!

目前為 2024-05-18 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Twitter media-only filter toggle.
  3. // @version 0.13
  4. // @description Toggle non-media tweets on and off on the home timeline, for the power-viewer!
  5. // @author Cro
  6. // @match https://*.twitter.com/*
  7. // @match https://*.x.com/*
  8. // @run-at document-idle
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @namespace https://greasyfork.org/users/10865
  12. // @icon https://www.google.com/s2/favicons?domain=twitter.com
  13. // @license MIT
  14. // ==/UserScript==
  15. /* jshint esversion: 6 */
  16.  
  17. (function() {
  18. 'use strict';
  19. let storage_key = "cro-media-toggle";
  20. let show_all = GM_getValue(storage_key);
  21. let has_photo = node => node.querySelector('[data-testid="tweetPhoto"]');
  22. let has_video = node => node.querySelector('[data-testid="videoPlayer"]');
  23. let has_card_media = node => node.querySelector('[data-testid*="media"]');
  24. let has_media = node => [has_photo, has_video, has_card_media].some(f => f(node));
  25. let get_target_parent = node => node.parentNode.parentNode.parentNode;
  26. let for_each_article = func => void document.body.querySelectorAll("article").forEach(func);
  27. let set_article_state = node => void(get_target_parent(node).style.display = show_all || has_media(node) ? "block" : "none");
  28. let set_all_article_states = () => void for_each_article(set_article_state);
  29.  
  30. let create_ui = function(target)
  31. {
  32. let button = document.createElement("button");
  33. let set_button_state = () => { button.innerText = show_all ? "Showing all home tweets" : "Showing only media home tweets"; };
  34.  
  35. button.onclick = function(event)
  36. {
  37. show_all = !show_all;
  38. GM_setValue(storage_key, show_all);
  39. set_button_state();
  40. };
  41.  
  42. target.prepend(button);
  43. set_button_state();
  44. };
  45.  
  46. let start_process = function()
  47. {
  48. setInterval(function()
  49. {
  50. if (location.pathname == "/home")
  51. {
  52. set_all_article_states();
  53. }
  54. });
  55. };
  56.  
  57. // Wait for twitter's react crap finish loading things.
  58. let scan_interval = setInterval(function()
  59. {
  60. let target = document.body.querySelector("h1[role='heading']");
  61. if (target)
  62. {
  63. clearInterval(scan_interval);
  64. start_process(target);
  65. create_ui(target);
  66. }
  67. }, 10);
  68. })();