Greasy Fork

Greasy Fork is available in English.

Twitter media-only filter toggle.

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

当前为 2024-09-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴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.15
// @description  Toggle non-media tweets on and off on the home timeline, for the power-viewer!
// @author       Cro
// @match        https://*.twitter.com/*
// @match        https://*.x.com/*
// @run-at       document-idle
// @grant        GM_setValue
// @grant        GM_getValue
// @namespace http://greasyfork.icu/users/10865
// @icon         https://www.google.com/s2/favicons?domain=twitter.com
// @license      MIT
// ==/UserScript==
/* jshint esversion: 6 */

(function() {
    let storage_key = "cro-media-toggle";
    let show_all = GM_getValue(storage_key);

    let create_ui = function(target)
    {
        let button = document.createElement("button");
        button.innerText = show_all ? "Showing all home tweets" : "Showing only media home tweets";

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

        target.prepend(button);
    };

    let find_objects_at_keys = function(obj, keys)
    {
        let found = [];
        let stack = Object.entries(obj);
        while (stack.length > 0)
        {
            let current = stack.pop();
            if (keys.includes(current[0]))
            {
                found.push(current[1]);
            }
            if (current[1] != null && typeof(current[1]) == 'object')
            {
                stack = stack.concat(Object.entries(current[1]));
            }
        }
        return found;
    };

    let has_media = function(obj)
    {
        if (obj.entryId.includes("tweet"))
        {
            return obj?.content?.itemContent?.tweet_results?.result?.legacy?.entities?.hasOwnProperty('media');
        }
        return true;
    };

    let update_data = function(data)
    {
        if (show_all || location.pathname != '/home')
        {
            return;
        }
        for (let obj of find_objects_at_keys(data, ['instructions']))
        {
            for (let subobj of obj)
            {
                if (subobj.hasOwnProperty('entries'))
                {
                    subobj.entries = subobj.entries.filter(has_media);
                }
            }
        }
    };

    let old_parse = unsafeWindow.JSON.parse;
    let new_parse = function(string)
    {
        let data = JSON.parse(string);
        try
        {
            if (data != null)
            {
                update_data(data);
            }
        }
        catch(error)
        {
            console.log(error);
        }
        return old_parse(JSON.stringify(data));;
    };
    exportFunction(new_parse, unsafeWindow.JSON, { defineAs: "parse" });

    // 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);
            create_ui(target);
        }
    }, 10);
})();