Greasy Fork

Greasy Fork is available in English.

Twitter media-only filter toggle.

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

当前为 2021-05-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Twitter media-only filter toggle.
// @version      0.9
// @description  Toggle non-media tweets on and off, for the power-viewer!
// @author       Cro
// @match        https://twitter.com/*
// @run-at       document-idle
// @grant        GM_setValue
// @grant        GM_getValue
// @namespace http://greasyfork.icu/users/10865
// ==/UserScript==
/* jshint esversion: 6 */

(function() {
    'use strict';
    let storage_key = "cro-media-toggle";
    let show_all = GM_getValue(storage_key);
    let has_photo = node => node.querySelector('[data-testid="tweetPhoto"]');
    let has_video = node => node.querySelector('[data-testid="videoPlayer"]');
    let has_card_media = node => node.querySelector('[data-testid*="media"]');
    let has_media = node => [has_photo, has_video, has_card_media].some(f => f(node));
    let get_target_parent = node => node.parentNode.parentNode.parentNode;
    let for_each_article = func => void document.body.querySelectorAll("article").forEach(func);
    let set_article_state = node => void(get_target_parent(node).style.display = show_all || has_media(node) ? "block" : "none");
    let set_all_article_states = () => void for_each_article(set_article_state);

    let create_ui = function(target)
    {
        let button = document.createElement("button");
        let set_button_state = () => { button.innerText = show_all ? "All Tweets" : "Only Media Tweets"; };

        button.onclick = function(event)
        {
            show_all = !show_all;
            GM_setValue(storage_key, show_all);
            set_button_state();
        };

        target.prepend(button);
        set_button_state();
    };

    let start_process = function()
    {
        setInterval(function()
        {
            if (!location.pathname.startsWith("/notifications"))
            {
                set_all_article_states();
            }
        }, 250);
    };

    // Wait for twitter's react crap finish loading things.
    let scan_interval = setInterval(function()
    {
        let target = document.body.querySelector("h1[role='heading']");
        if (target)
        {
            clearInterval(scan_interval);
            start_process(target);
            create_ui(target);
        }
    }, 10);
})();