Steam Inventory Preview Background Button

Add preview background button to steam inventory.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Steam Inventory Preview Background Button
// @namespace    http://steamcommunity.com/profiles/76561197995708004/
// @version      1.02
// @description  Add preview background button to steam inventory.
// @author       HAC
// @match        http://steamcommunity.com/profiles/*
// @match        http://steamcommunity.com/id/*
// @grant        none
// @license      GPLv3
// ==/UserScript==

(function() {
    'use strict';

	let path = window.location.pathname.replace(/\/+/g, "/");
    if(/^\/(?:id|profiles)\/.+\/inventory/.test(path)) {
        let observer = new MutationObserver(function(mutations) {
            let activeItemInfo = iActiveSelectView; // Get iActiveSelectView from global
            inventoryAddButtonPreviewBackground(activeItemInfo);
        });
        observer.observe(document.getElementById('iteminfo0_item_name'), { childList: true });
        observer.observe(document.getElementById('iteminfo1_item_name'), { childList: true });
    } else if(/^\/(?:id|profiles)\/.+/.test(path)){
        let previewHash = window.location.hash.match(/#previewBackground\/(\d+\/[a-z0-9]+(?:\.[a-z0-9]+)?)/i);
        if (previewHash) {
            let imageUrl = window.location.protocol + '//steamcdn-a.akamaihd.net/steamcommunity/public/images/items/' + previewHash[1];
            profileChangeBackground(imageUrl);
        }
    }

    function inventoryAddButtonPreviewBackground (activeItemInfo){
        let profileUrlNodesWrapper = document.getElementById('global_actions');
        let profileUrlNodes = profileUrlNodesWrapper.getElementsByClassName('playerAvatar');
        let profileUrl = profileUrlNodes[0].href;
        let itemButtonsHolder = document.getElementById('iteminfo' + activeItemInfo + '_item_actions');
        let itemButtons = itemButtonsHolder.getElementsByTagName('a');
        let imageUrl = itemButtons[0].href;
        let imageUrlMatch = imageUrl.match(/images\/items\/(\d+\/[a-z0-9]+(?:\.[a-z0-9]+)?)/i);
        if(imageUrlMatch !== null) {
            imageUrlMatch = imageUrlMatch[1];
            let previewUrl = profileUrl + '#previewBackground/' + imageUrlMatch;

            let a = document.createElement('a');
            a.setAttribute('href', previewUrl);
            a.setAttribute('target', '_blank');
            a.classList.add('btn_small');
            a.classList.add('btn_darkblue_white_innerfade');
            itemButtonsHolder.appendChild(a);
            let span = document.createElement('span');
            span.textContent = 'Preview Background';
            a.appendChild(span);
            itemButtonsHolder.style.height = '';
        }
    }

    function profileChangeBackground(imageUrl) {
        let img = document.createElement('img');
        img.style.display = 'none';
        img.src = imageUrl;
        document.body.appendChild(img);
        img.onload = function() {
            let bgImgNodes = document.getElementsByClassName('no_header profile_page');
            for(let bgImgNode of bgImgNodes) { bgImgNode.style.backgroundImage = 'url(' + imageUrl + ')'; }
            let bgImgNodes2 = document.getElementsByClassName('profile_background_image_content');
            for(let bgImgNode2 of bgImgNodes2) { bgImgNode2.style.backgroundImage = 'url(' + imageUrl + ')'; }
            img.remove();
        };
        img.onerror = function() {
            console.log("Error: Can't load image. " + imageUrl);
            img.remove();
        };
    }
})();