Elimination Filter

Removes targets that are not Okay from the team list and revenge pages for elimination.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Elimination Filter
// @namespace    sullengenieEliminationFilter
// @version      0.4
// @description  Removes targets that are not Okay from the team list and revenge pages for elimination.
// @author       sullengenie [1946152], epicaricacy [2040809]
// @match        *://*.torn.com/competition.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const TEAM_LIST_STATUS_INDEX = 4;
    const REVENGE_LIST_STATUS_INDEX = 3;

    const TEAM_LIST_LEVEL_INDEX = 3;
    const REVENGE_LIST_LEVEL_INDEX = 2;

    let STATUS_INDEX;
    let LEVEL_INDEX;

    function hide(player) {
        player.style.display = 'none';
    }

    function show(player) {
        player.style.display = 'inline';
    }

    function statusOf(player) {
        return player.firstElementChild.children[STATUS_INDEX].innerText;
    }

    function levelOf(player) {
        return player.firstElementChild.children[LEVEL_INDEX].innerText;
    }

    function isTeamListPage() {
        return window.location.href.includes('team');
    }

    function isRevengePage() {
        return window.location.href.includes('revenge');
    }

    function isListOfPlayers(node) {
        return node.classList !== undefined &&
            (node.classList.contains('team-list-wrap') ||
             node.classList.contains('revenge-wrap'));
    }

    function updateIndices() {
        if (isTeamListPage()) {
            STATUS_INDEX = TEAM_LIST_STATUS_INDEX;
            LEVEL_INDEX = TEAM_LIST_LEVEL_INDEX;
        } else if (isRevengePage()) {
            STATUS_INDEX = REVENGE_LIST_STATUS_INDEX;
            LEVEL_INDEX = REVENGE_LIST_LEVEL_INDEX;
        } else {
            throw 'Invalid page.';
        }
    }

    function shouldHide(filterOptions, player) {
        return (filterOptions.okayOnly && statusOf(player) !== 'Okay') ||
            (filterOptions.maxLevel && levelOf(player) > filterOptions.maxLevel);
    }

    function applyFilter() {
        let playerList = document.querySelector('.competition-list');
        let filterOptions = getFilterOptionsFromPanel();
        localStorage.eliminationFilter = JSON.stringify(filterOptions);
        for (let player of playerList.children) {
            if (shouldHide(filterOptions, player)) {
                hide(player);
            } else {
                show(player);
            }
        }
    }

    function getFilterOptionsFromPanel() {
        let maxLevelInput = document.getElementById('ef-max-level');
        let okayOnlyCheckbox = document.getElementById('ef-status-okay-only');
        return {
            'maxLevel': maxLevelInput && !isNaN(maxLevelInput.value) && parseInt(maxLevelInput.value, 10),
            'okayOnly': okayOnlyCheckbox && okayOnlyCheckbox.checked
        };
    }

    function createFilterOptionsPanel() {
        if (document.getElementById('elimination-filter')) {
            return;
        }
        let cachedOptions = JSON.parse(localStorage.eliminationFilter || '{}');
        let competitionWrap = document.getElementById('competition-wrap');
        let competitionBr = competitionWrap.querySelector('.page-head-delimiter');

        let lineBreak = document.createElement('br');

        let filterOptionsPanel = document.createElement('div');
        filterOptionsPanel.id = 'elimination-filter';
        filterOptionsPanel.className += ' m-top10';

        let panelTitle = document.createElement('div');
        panelTitle.className += ' title-gray top-round';
        panelTitle.innerHTML = '[UNOFFICIAL] Elimination Filter Options';

        let panelContent = document.createElement('div');
        panelContent.className += ' bottom-round cont-gray p10';

        let statusCheckbox = document.createElement('input');
        statusCheckbox.type = 'checkbox';
        statusCheckbox.name = 'ef-status';
        statusCheckbox.value = 'Okay Only';
        statusCheckbox.id = 'ef-status-okay-only';
        statusCheckbox.checked = cachedOptions.okayOnly || false;

        let okayOnlyLabel = document.createElement('label');
        okayOnlyLabel.innerHTML = ' Okay Only';
        okayOnlyLabel.setAttribute('for', 'ef-status-okay-only');

        let statusSpan = document.createElement('p');
        statusSpan.appendChild(statusCheckbox);
        statusSpan.appendChild(okayOnlyLabel);

        let levelLabel = document.createElement('label');
        levelLabel.setAttribute('for', 'ef-max-level');
        levelLabel.innerHTML = 'Max Level: ';

        let levelInput = document.createElement('input');
        levelInput.id = 'ef-max-level';
        levelInput.setAttribute('type', 'number');
        levelInput.value = cachedOptions.maxLevel || '';

        let levelSpan = document.createElement('p');
        levelSpan.appendChild(levelLabel);
        levelSpan.appendChild(levelInput);

        panelContent.appendChild(statusSpan);
        panelContent.appendChild(lineBreak);
        panelContent.appendChild(levelSpan);
        filterOptionsPanel.appendChild(panelTitle);
        filterOptionsPanel.appendChild(panelContent);
        competitionBr.parentNode.insertBefore(filterOptionsPanel, competitionBr.nextSibling);

        statusCheckbox.onchange=applyFilter;
        levelInput.onchange=applyFilter;
        levelInput.onkeyup=applyFilter;
    }

    function watchForPlayerListUpdates() {
        let target = document.getElementById('competition-wrap');
        let observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                let doApplyFilter = false;
                for (let i = 0; i < mutation.addedNodes.length; i++) {
                    if (isListOfPlayers(mutation.addedNodes.item(i))) {
                        doApplyFilter = true;
                        break;
                    }
                }
                if (doApplyFilter) {
                    createFilterOptionsPanel();
                    updateIndices();
                    applyFilter();
                }
            });
        });
        // configuration of the observer:
        let config = { attributes: true, childList: true, characterData: true };
        // pass in the target node, as well as the observer options
        observer.observe(target, config);
    }
    watchForPlayerListUpdates();
})();