FMC Expand Toggle

Fixes expand all button to allow for collapse all

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name         FMC Expand Toggle
// @namespace    http://tampermonkey.net/
// @version      1.11
// @description  Fixes expand all button to allow for collapse all
// @author       geesmavi
// @include      https://trans-logistics.amazon.com/fmc/*
// @include      https://trans-logistics-eu.amazon.com/fmc/*
// @icon         https://www.google.com/s2/favicons?domain=amazon.com
// @grant        GM_addStyle
// @run-at document-start
// ==/UserScript==

(function() {
    'use strict';
    GM_addStyle(`
        .comment-visibility-toggle {
        user-select: none;
        }
    `);

    // restarts detailsToggleObserver on url change
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            detailsToggleObserver.observe(document, {
                childList: true,
                subtree: true
            });
        }
    }).observe(document, {subtree: true, childList: true});

    // watches page for dashboard-details-toggle which loads after page load
    // if element exists starts the detailsToggler
    const detailsToggleObserver = new MutationObserver((mutations, obs) => {
        const detailsToggle = document.querySelector('th.dashboard-details-toggle');
        if (detailsToggle) {
            const fmcShown = document.querySelector('.fmc-shown');
            if (fmcShown) {
                detailsToggle.classList.remove('child-row-expand-trigger');
                detailsToggle.classList.add('child-row-collapse-trigger');
            }
            detailsToggler(detailsToggle);
            obs.disconnect();
            return;
        }
    });

    detailsToggleObserver.observe(document, {
        childList: true,
        subtree: true
    });

    // swaps the expand/collapse classes of given element
    function detailsToggler(element) {
            const removeClass = function(removedClass) {
                element.classList.remove(removedClass);
                return;
            };
            const addClass = function(addedClass) {
                element.classList.add(addedClass);
                return;
            };

            element.addEventListener("click", function() {
                switch(true){
                    case element.classList.contains('child-row-expand-trigger'):
                        removeClass('child-row-expand-trigger');
                        addClass('child-row-collapse-trigger');
                        break;
                    case element.classList.contains('child-row-collapse-trigger'):
                        removeClass('child-row-collapse-trigger');
                        addClass('child-row-expand-trigger');
                        break;
                    default:
                        break;
                }

                return;
            });
        };
})();