Twitter media-only filter toggle.

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

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

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