Twitter media-only filter toggle.

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

当前为 2019-09-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter media-only filter toggle.
  3. // @version 0.6
  4. // @description Toggle non-media tweets on and off, for the power-viewer!
  5. // @author Cro
  6. // @match https://twitter.com/*
  7. // @exclude https://twitter.com/notifications
  8. // @run-at document-idle
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @namespace https://greasyfork.org/users/10865
  12. // ==/UserScript==
  13. /* jshint esversion: 6 */
  14.  
  15. (function() {
  16. 'use strict';
  17. let program = function(target)
  18. {
  19. let storageKey = "cro-media-toggle";
  20. // The complexity of determining what is a meaningful image post.
  21. let containsImage = function(node)
  22. {
  23. let res = false;
  24. node.querySelectorAll("img").forEach(function (sub)
  25. {
  26. if (!(sub.src.startsWith("https://abs-0.twimg.com/emoji") ||
  27. sub.src.startsWith("https://pbs.twimg.com/profile_images") ||
  28. sub.src.startsWith("https://abs.twimg.com/hashflags")))
  29. {
  30. res = true;
  31. }
  32. });
  33. return res;
  34. };
  35. let isTarget = node => node.tagName &&
  36. node.tagName.toLowerCase() == "article" &&
  37. !(node.querySelector("video") || containsImage(node));
  38. let hideIfTarget = function(node) { if (isTarget(node)) node.style.display = "none"; };
  39. let show = function(node) { node.style.display = "block"; };
  40. let hideAll = function() { document.body.querySelectorAll("article").forEach(hideIfTarget); };
  41. let showAll = function() { document.body.querySelectorAll("article").forEach(show); };
  42. let running = GM_getValue(storageKey);
  43.  
  44. setInterval(function() { if (running) hideAll(); }, 250);
  45.  
  46. // UI
  47. let button = document.createElement("button");
  48. let setButtonState = function()
  49. {
  50. if (running)
  51. {
  52. button.innerText = "Only Media Tweets";
  53. }
  54. else
  55. {
  56. button.innerText = "All Tweets";
  57. showAll();
  58. }
  59. };
  60.  
  61. button.onclick = function(event)
  62. {
  63. running = !running;
  64. GM_setValue(storageKey, running);
  65. setButtonState();
  66. };
  67.  
  68. target.prepend(button);
  69. setButtonState();
  70. };
  71.  
  72. // Wait for twitter's react crap finish loading things.
  73. let scanInterval = setInterval(function()
  74. {
  75. let target = document.body.querySelector("h1[role='heading']");
  76. let mainContents = document.body.querySelector("main article");
  77. if (target && mainContents)
  78. {
  79. clearInterval(scanInterval);
  80. program(target);
  81. }
  82. }, 10);
  83. })();