Profile Info

Stores and displays information on player profile pages

当前为 2017-09-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         Profile Info
// @namespace    sullenProfileInfo
// @version      0.1
// @description  Stores and displays information on player profile pages
// @author       sullengenie [1946152]
// @match        *://*.torn.com/profiles.php?XID=*
// ==/UserScript==

(function() {
    'use strict';

    const tag_colors = {
        tbd: 'inherited',
        easy: 'rgba(161, 248, 161, 1)',
        medium: 'rgba(231, 231, 104, 1)',
        impossible: 'rgba(242, 140, 140, 0.7)'
    };

    const create_html = (html) => document.createRange().createContextualFragment(html);
    const insert_before = (nodes, target) => target.parentNode.insertBefore(nodes, target);
    const insert_after  = (nodes, target) => target.parentNode.insertBefore(nodes, target.nextSibling);

    const button_style = (color) => `.profile-button-attack { background: linear-gradient(180deg, #ebebeb, ${color}) !important; }`;
    document.body.appendChild(create_html('<style id="difficulty-profile-button-style"></style>'));
    const color_button = function(color) {
        document.getElementById('difficulty-profile-button-style').innerText = button_style(color);
    };

    const initial_tags = JSON.parse(localStorage.sullenProfileInfo || '{}');
    const player_id = parseInt(window.location.href.match(/XID=(\d+)/)[1]);

    if (initial_tags.hasOwnProperty(player_id)) {
        color_button(tag_colors[initial_tags[player_id].difficulty]);
    }

    function update_tags(f) {
        let tags = JSON.parse(localStorage.sullenProfileInfo || '{}');
        f(tags);
        localStorage.sullenProfileInfo = JSON.stringify(tags);
    }

    function update_player(vals) {
        update_tags(tags => tags[player_id] = Object.assign(tags[player_id] || {}, vals));
    }

    function update_hidden(val) {
        update_tags(tags => tags.hidden = val);
    }

    function get_player_difficulty() {
        const tags = JSON.parse(localStorage.sullenProfileInfo || '{}');
        return (tags[player_id] && tags[player_id].difficulty) || 'tbd';
    }

    function get_player_notes() {
        const tags = JSON.parse(localStorage.sullenProfileInfo || '{}');
        return (tags[player_id] && tags[player_id].notes);
    }

    function update_difficulty(menu) {
        const difficulty = menu.value;
        update_player({difficulty: difficulty});
        color_button(tag_colors[difficulty]);
    }

    function update_notes(notes) {
        update_player({notes: notes.value});
    }

    const initial_player_info = initial_tags[player_id];
    const initial_difficulty = initial_player_info.difficulty;
    const initial_notes = initial_player_info.notes;

    const test_container = create_html(`
<div class="profile-wrapper m-top10">
<div>
<div class="title-black top-round${initial_tags.hidden ? ' all-round' : ''}" id="difficulty-profile-title">Profile Difficulty - Click to Hide</div>
<div class="cont bottom-round">
<div class="profile-container basic-info bottom-round" id="difficulty-profile-body" style="display:${initial_tags.hidden ? 'none' : 'block'}">
<div style="padding: 10px">
<select id="difficulty-dropdown" style="margin: 0px 10px 0px 0px" onchange="update_difficulty(this)">
<option ${initial_difficulty === 'tbd' ? 'selected="selected"' : ''} value="tbd">Difficulty</option>
<option ${initial_difficulty === 'easy' ? 'selected="selected"' : ''} value="easy">Easy</option>
<option ${initial_difficulty === 'medium' ? 'selected="selected"' : ''} value="medium">Medium</option>
<option ${initial_difficulty === 'impossible' ? 'selected="selected"' : ''} value="impossible">Impossible</option>
</select>
<textarea id="difficulty-notes" rows="10" cols="50">${initial_notes || 'Write player notes here.'}</textarea>
</div>
</div>
</div>
</div>
</div>`);




    function add_title_toggle_onclick() {
        const title_node = document.getElementById('difficulty-profile-title');
        const body_node = document.getElementById('difficulty-profile-body');
        title_node.onclick = function() {
            if (body_node.style.display !== 'none') {
                title_node.innerText = 'Profile Difficulty - Click to Display';
                body_node.style.display = 'none';
                update_hidden(true);
                title_node.classList.add('all-round');
            } else {
                title_node.innerText = 'Profile Difficulty - Click to Hide';
                body_node.style.display = 'block';
                update_hidden(false);
                title_node.classList.remove('all-round');
            }
        };
    }

    function add_difficulty_onchange() {
        const dropdown = document.getElementById('difficulty-dropdown');
        dropdown.onchange = () => update_difficulty(dropdown);
    }

    function add_notes_oninput() {
        const notes = document.getElementById('difficulty-notes');
        notes.oninput = () => update_notes(notes);
    }

    function add_info_panel(medal_wrapper) {
        insert_before(test_container, medal_wrapper);
        add_title_toggle_onclick();
        add_difficulty_onchange();
        add_notes_oninput();
    }

    const is_medals_wrapper = (node) => node.classList !== undefined && node.classList.contains('medals-wrapper');

    function wait_for_medals_wrapper() {
        let target = document.querySelector('div.user-profile');
        let observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                for (let i = 0; i < mutation.addedNodes.length; i++) {
                    const node = mutation.addedNodes.item(i);
                    if (is_medals_wrapper(node)) {
                        add_info_panel(node);
                        this.disconnect();
                        break;
                    }
                }
            }.bind(this));
        });
        // 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);
    }

    function wait_for_page_load() {
        let target = document.getElementById('profileroot').firstElementChild;
        let observer = new MutationObserver(function(mutations) {
            console.log('Page load');
            wait_for_medals_wrapper();
            this.disconnect();
        });
        // 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);
    }
    wait_for_page_load();

    const global_css = create_html(`<style>
#difficulty-profile-title { cursor: pointer; }
#difficulty-profile-body * { vertical-align:top; }
.all-round { border-radius: 5px !important; }
</style>`);
    document.body.appendChild(global_css);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址