Twitter media-only filter toggle.

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

目前為 2024-09-06 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitter media-only filter toggle.
// @version      0.20
// @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 https://greasyfork.org/users/10865
// @icon         https://www.google.com/s2/favicons?domain=twitter.com
// @license      MIT
// ==/UserScript==
/* jshint esversion: 6 */

(function() {
    'use strict';
    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 walk_objects = function*(obj)
    {
        let stack = Object.entries(obj);
        while (stack.length > 0)
        {
            let entry = stack.pop();
            yield entry;
            if (entry[1] != null && typeof(entry[1]) == 'object')
            {
                stack = stack.concat(Object.entries(entry[1]));
            }
        }
    }

    let find_objects_at_keys = (obj, keys) => Array.from(walk_objects(obj)).filter(e => keys.includes(e[0])).map(e => e[1]);
    let any_key_in_obj = (obj, keys) => Array.from(walk_objects(obj)).some(e => keys.includes(e[0]));
    let has_media = (obj) => obj.entryId.includes("cursor-") || any_key_in_obj(obj, ['media', 'card']);

    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 = JSON.parse;
    let unsafe_window_parse = unsafeWindow.JSON.parse;
    let new_unsafe_window_parse = function(string)
    {
        let data = old_parse(string);
        try
        {
            if (data != null)
            {
                update_data(data);
            }
        }
        catch(error)
        {
            console.log(error);
        }
        return unsafe_window_parse(JSON.stringify(data));;
    };
    exportFunction(new_unsafe_window_parse, unsafeWindow.JSON, { defineAs: "parse" });

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