Twitter media-only filter toggle.

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

目前為 2018-03-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.3
// @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 https://greasyfork.org/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 mutationCallback = function(mutations)
    {
        for (var mutation of mutations)
        {
            for (var node of mutation.addedNodes)
            {
                hideIfTarget(node);
            }
        }
    };

    var hideAllNonMedia = function()
    {
        document.querySelectorAll("li.stream-item").forEach(hideIfTarget);
    };

    var showAllTweets = function()
    {
        document.querySelectorAll("li.stream-item").forEach(function (node) { node.hidden = false; });
    };

    var observer = new MutationObserver(mutationCallback);
    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));

        if (GM_getValue(storageKey))
        {
            button.innerText = only;
            hideAllNonMedia();
            observer.observe(stream, { childList: true, subtree: true });
        }
        else
        {
            button.innerText = all;
            showAllTweets();
            observer.disconnect();
        }
    };

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