Steam Inventory Preview Background Button

Add preview background button to steam inventory.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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();
        };
    }
})();