Greasy Fork

来自缓存

Greasy Fork is available in English.

Twitter media-only filter toggle.

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

当前为 2018-03-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴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.4
// @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==

(function() {
    'use strict';
    // Die fast if we cannot hook into the button bar.
    var buttonBar = document.getElementById("global-actions");
    if (buttonBar === null) return;

    var hideIfTarget = function(node)
    {
        if (node.tagName &&
            node.tagName.toLowerCase() == "li" &&
            node.classList.contains("stream-item") &&
            null === node.querySelector("div.AdaptiveMediaOuterContainer"))
        {
            node.hidden = true;
        }
    };

    var observer = new MutationObserver(mutations => mutations.forEach(mutation => mutation.addedNodes.forEach(hideIfTarget)));
    var stream = document.getElementsByClassName("stream")[0];

    // UI
    var li = document.createElement("li");
    var button = document.createElement("button");
    var only = "Only Media Tweets";
    var all = "All Tweets";
    var storageKey = "cro-media-toggle";

    button.onclick = function(event)
    {
        GM_setValue(storageKey, !GM_getValue(storageKey));
        var items = document.querySelectorAll("li.stream-item");

        if (GM_getValue(storageKey))
        {
            button.innerText = only;
            items.forEach(hideIfTarget);
            observer.observe(stream, { childList: true, subtree: true });
        }
        else
        {
            button.innerText = all;
            items.forEach(node => node.hidden = false);
            observer.disconnect();
        }
    };

    buttonBar.appendChild(li);
    li.appendChild(button);
    button.click();
})();