Skip to content

Instantly share code, notes, and snippets.

@ezhevita
Last active May 23, 2017 16:36
Show Gist options
  • Save ezhevita/62dd2971dba1db482a64485eab937624 to your computer and use it in GitHub Desktop.
Save ezhevita/62dd2971dba1db482a64485eab937624 to your computer and use it in GitHub Desktop.
SteamBoosterOpener-ru
// ==UserScript==
// @name Steam Booster Opener RU
// @namespace Madjoki, Vital7
// @include http://steamcommunity.com/*/inventory*
// @include https://steamcommunity.com/*/inventory*
// @version 9
// @grant none
// ==/UserScript==
(function() {
var boosterCountDone = false;
var modal = null;
var dialog = $J("#market_sell_dialog").before('\
<div id="booster_dialog" class="newmodal" style="display: none;">\
<div class="newmodal_header_border">\
<div class="newmodal_header">\
<span>Открыть наборы карточек</span>\
<div class="newmodal_close"></div>\
</div>\
</div>\
<div class="newmodal_content_border">\
<div id="booster_content" class="newmodal_content">\
<a id="booster_open1" href="#" class="massbooster_open btn_green_white_innerfade btn_small_wide"><span>Открыть выбранные</span></a>\
<a id="booster_open2" href="#" class="massbooster_openall btn_green_white_innerfade btn_small_wide"><span>Открыть все</span></a>\
<div id="boosters_games">\
</div>\
<a id="booster_open3" href="#" class="massbooster_open btn_green_white_innerfade btn_small_wide"><span>Открыть выбранные</span></a>\
<a id="booster_open4" href="#" class="massbooster_openall btn_green_white_innerfade btn_small_wide"><span>Открыть все</span></a>\
</div>\
</div>\
</div>');
$J("div.inventory_rightnav").prepend("<a id=\"openboosters\" class=\"btn_darkblue_white_innerfade btn_medium new_trade_offer_btn\"><span>Открыть наборы карточек</span></a>");
$J(".massbooster_open").click(
function() {
//var $elCount = $J("#booster_count")
//var howMany = parseInt($elCount.val());
var selectedApps = $J("input.booster_open_checkbox:checked").map(function() {
return this.value;
}).get();
prepare_open(-1, selectedApps);
}
);
$J(".massbooster_openall").click(
function() {
prepare_open(-1, ["all"]);
}
);
function inventory_check()
{
var inv = UserYou.rgContexts[753][6];
if (!inv.inventory.m_bPerformedInitialLoad) {
alert('Инвентарь не загружен');
return false;
}
if (!inv.inventory.m_bFullyLoaded) {
alert('Инвентарь загружен не полностью. Некоторые наборы карточе могут не показываться. Нажатие "Показать доп. фильтры…" загрузит все страницы инвентаря, либо пролистайте инвентарь, пока наборы не закончатся (быстрее, если у вас огромное кол-во карт).');
return true;
}
return true;
}
$J("#openboosters").click(
function() {
var inv = UserYou.rgContexts[753][6];
if (!inventory_check())
return;
var $elDialogContent = $J("#booster_dialog");
modal = new CModal($elDialogContent);
if (!boosterCountDone) {
var boosters = count_boosters();
$J('#boosters_games').append('Всего: ' + boosters.count + '<br>');
boosters.games.forEach(function(item, index, array) {
var idName = 'booster_' + item.id;
$J('#boosters_games').append('<input id="' + idName + '" class="booster_open_checkbox" type="checkbox" value="' + item.id + '"><label for="' + idName + '">' + item.name + ' (' + item.count + ')</label><br>');
});
boosterCountDone = true;
}
modal.Show();
}
);
var submitUrl = g_strProfileURL + "/ajaxunpackbooster/";
function open_next(state, toOpen) {
if (modal !== null)
modal.Dismiss();
if (toOpen.length === 0) {
ShowAlertDialog('Открытие наборов карточек завершено!', "Наборов открыто: " + state.n + " | Обычных карт: " + state.nR + " | Металлических карт: " + state.nF);
return;
} else {
modal = ShowBlockingWaitDialog('Распаковка наборов карточек...', "Наборов открыто: " + state.n + " | " + toOpen.length + " осталось | Обычных карт: " + state.nR + " | Металлических карт: " + state.nF);
}
var current = toOpen.pop();
$J.post(submitUrl, {
appid: current.app,
communityitemid: current.id,
sessionid: g_sessionID
})
.done(function(data) {
if (data.rgItems && data.rgItems.length > 0) {
state.n++;
for (var i = 0; i < data.rgItems.length; i++) {
var item = data.rgItems[i];
if (item.foil)
state.nF++;
else
state.nR++;
}
setTimeout(open_next(state, toOpen), 500);
} else {
open_next(state, []);
}
})
.fail(function() {
open_next(state, []);
});
}
function is_booster(item)
{
for (var i in item.tags) {
var tag = item.tags[i];
if (tag.category == "item_class" && tag.internal_name == "item_class_5")
return true;
}
}
function count_boosters() {
var count = {
"count": 0,
"games": []
};
var inv = UserYou.rgContexts[753][6];
var games = {};
for (var itemid in inv.inventory.m_rgAssets) {
if (!inv.inventory.m_rgAssets.hasOwnProperty(itemid))
continue;
var asset = inv.inventory.m_rgAssets[itemid];
var item = asset.description;
if (is_booster(item))
{
if (item.market_hash_name in games) {
games[item.market_hash_name].count++;
} else {
games[item.market_hash_name] = {
"id": item.market_hash_name,
"name": item.name,
"count": 1
}
}
count.count++;
}
}
for (var key in games) {
count.games.push(games[key]);
}
count.games.sort(function(a, b) {
var game1 = a.name.toUpperCase();
var game2 = b.name.toUpperCase();
if (game1 < game2)
return -1;
if (game1 > game2)
return 1;
return 0;
});
return count;
}
function prepare_open(n, apps) {
var inv = UserYou.rgContexts[753][6];
var toOpen = [];
for (var itemid in inv.inventory.m_rgAssets) {
if (!inv.inventory.m_rgAssets.hasOwnProperty(itemid))
continue;
var asset = inv.inventory.m_rgAssets[itemid];
var item = asset.description;
if (is_booster(item)) {
var open = apps.indexOf("all") > -1 || apps.indexOf(item.market_hash_name) > -1;
if (!open)
continue;
if (n == -1 || n > 0) {
if (n > 0)
n--;
toOpen.push({
"id": asset.assetid,
"app": item.appid
});
}
}
}
open_next({
"n": 0,
"nR": 0,
"nF": 0
}, toOpen);
}
// Перевел на русский Vital7
// steamcommunity.com/id/Vital7
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment