Created
November 24, 2019 19:46
-
-
Save BJIAST/4d42716f507dc8337b2eb0b614989c4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Cases Calc | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://steamcommunity.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
// Your code here... | |
console.log('hello!'); | |
function write(btn) { | |
const priceList = JSON.parse(localStorage.getItem('priceList')) || []; | |
let block = document.querySelector('#tabContentsMyMarketHistoryTable'); | |
if (btn !== 'all') { | |
const chkbx = document.querySelectorAll('.chkb_for_add'); | |
chkbx.forEach(item => { | |
if (item.checked == true) { | |
const row = item.parentNode.parentNode; | |
const name = row.querySelector('.market_listing_item_name').innerText; | |
const price = row.querySelector('.market_listing_price').innerText.replace(',', ".").replace('₴', ''); | |
const priceItem = { | |
name, | |
count: 1, | |
price | |
}; | |
priceList.push(priceItem); | |
item.checked = false; | |
} | |
}) | |
} else { | |
let items = block.querySelectorAll('.status__'); | |
items.forEach(item => { | |
const row = item.parentNode; | |
const name = row.querySelector('.market_listing_item_name').innerText; | |
const price = row.querySelector('.market_listing_price').innerText.replace(',', ".").replace('₴', ''); | |
const priceItem = { | |
name, | |
count: 1, | |
price | |
}; | |
priceList.push(priceItem); | |
}) | |
} | |
localStorage.setItem('priceList', JSON.stringify(priceList)); | |
getReadyArray(); | |
} | |
/////////////////////////////////////////////////////////// | |
function getReadyArray() { | |
let priceListHistory = '[{"name":"TRIUMPH CRATE","count":1,"price":"1.27"},{"name":"TRIUMPH CRATE","count":1,"price":"1.28"},{"name":"TRIUMPH CRATE","count":1,"price":"1.28"},{"name":"TRIUMPH CRATE","count":1,"price":"1.28"},{"name":"TRIUMPH CRATE","count":1,"price":"1.28"},{"name":"TRIUMPH CRATE","count":1,"price":"1.28"},{"name":"DESPERADO CRATE","count":1,"price":"0.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.87"},{"name":"EQUINOX CRATE","count":1,"price":"1.87"},{"name":"5 EQUINOX CRATE","count":1,"price":"9.45"},{"name":"EQUINOX CRATE","count":1,"price":"1.78"},{"name":"EQUINOX CRATE","count":1,"price":"1.78"},{"name":"EQUINOX CRATE","count":1,"price":"1.78"},{"name":"EQUINOX CRATE","count":1,"price":"1.78"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.19"},{"name":"TRIUMPH CRATE","count":1,"price":"2.22"},{"name":"TRIUMPH CRATE","count":1,"price":"2.14"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.24"},{"name":"TRIUMPH CRATE","count":1,"price":"2.23"},{"name":"TRIUMPH CRATE","count":1,"price":"2.23"},{"name":"TRIUMPH CRATE","count":1,"price":"2.23"},{"name":"20 TRIUMPH CRATE","count":1,"price":"45.20"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.14"},{"name":"TRIUMPH CRATE","count":1,"price":"2.25"},{"name":"TRIUMPH CRATE","count":1,"price":"2.16"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"TRIUMPH CRATE","count":1,"price":"2.16"},{"name":"TRIUMPH CRATE","count":1,"price":"2.16"},{"name":"TRIUMPH CRATE","count":1,"price":"2.15"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"DESPERADO CRATE","count":1,"price":"0.71"},{"name":"40 FEVER CRATE","count":1,"price":"75.20"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.77"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.50"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.80"},{"name":"TRIUMPH CRATE","count":1,"price":"2.50"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.43"},{"name":"EQUINOX CRATE","count":1,"price":"1.53"},{"name":"EQUINOX CRATE","count":1,"price":"1.51"},{"name":"24 EQUINOX CRATE","count":1,"price":"41.76"},{"name":"EQUINOX CRATE","count":1,"price":"1.73"},{"name":"EQUINOX CRATE","count":1,"price":"1.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.72"},{"name":"EQUINOX CRATE","count":1,"price":"1.65"},{"name":"EQUINOX CRATE","count":1,"price":"1.65"},{"name":"10 FEVER CRATE","count":1,"price":"12.90"},{"name":"10 EQUINOX CRATE","count":1,"price":"13"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"},{"name":"FEVER CRATE","count":1,"price":"1.21"}]' | |
let priceListJSON = localStorage.getItem('priceList') || priceListHistory; | |
const priceList = JSON.parse(priceListJSON); | |
let newList = priceList.map(item => { | |
let ifNumber = item.name.split(" "); | |
if (!isNaN(+ifNumber[0])) { | |
item.count = +ifNumber[0]; | |
ifNumber.shift(); | |
item.name = ifNumber.join(" "); | |
item.price = item.price / item.count; | |
} | |
item.price = +item.price; | |
return item; | |
}) | |
let lastList = []; | |
newList.forEach(item => { | |
let obj = lastList.find(obj => obj.name == item.name && obj.price == item.price); | |
// console.log(obj); | |
if (typeof obj == 'undefined') { | |
const toAdd = {}; | |
toAdd.name = item.name; | |
toAdd.count = item.count; | |
toAdd.price = item.price; | |
lastList.push(toAdd); | |
} else { | |
obj.count += item.count; | |
} | |
}) | |
const finishList = lastList.sort((a, b) => a.count - b.count).reverse(); | |
localStorage.setItem('priceListFinished', JSON.stringify(finishList)); | |
console.table(JSON.parse(localStorage.getItem('priceListFinished'))); | |
} | |
function renderHtml() { | |
const viewListBtn = document.createElement('a'); | |
const viewList = document.createElement('div'); | |
const hiddenBlock = document.createElement('div'); | |
const body = document.querySelector('body'); | |
hiddenBlock.style.display = 'none'; | |
viewListBtn.className = "btn_profile_action btn_medium"; | |
viewListBtn.innerText = "Чекнуть кейсы"; | |
viewListBtn.style.position = 'absolute'; | |
viewListBtn.style.padding = '6px 12px'; | |
viewListBtn.style.top = '15px'; | |
viewListBtn.style.right = '15px'; | |
viewListBtn.style.zIndex = '2'; | |
viewList.style.position = 'absolute'; | |
viewList.style.padding = '6px 12px'; | |
viewList.style.top = '100px'; | |
viewList.style.right = '10%'; | |
viewList.style.width = '80vw'; | |
viewList.style.background = '#3c3c3c'; | |
viewList.style.display = 'none'; | |
viewList.style.zIndex = '9999999'; | |
body.prepend(viewListBtn); | |
body.prepend(viewList); | |
viewListBtn.addEventListener('click', event => { | |
const myItems = JSON.parse(localStorage.getItem('priceListFinished')); | |
let response = fetch('https://steamcommunity.com/market/search?appid=578080&q=CRATE'); | |
response.then(result => { | |
return result.text(); | |
}).then(html => { | |
hiddenBlock.innerHTML = html; | |
let items = hiddenBlock.querySelectorAll('.market_listing_searchresult'); | |
const freshPrices = []; | |
items.forEach(item => { | |
const name = item.getAttribute('data-hash-name'); | |
const price = item.querySelector('.normal_price > .normal_price').getAttribute('data-price') / 100; | |
// console.log(item.querySelector('.normal_price')); | |
freshPrices.push({ | |
name, | |
price | |
}) | |
}) | |
viewList.innerHTML = `<h1> Лист кейсов </h1> | |
<table border="1px" style="margin: 20px auto; width: 80%;"> | |
<thead> | |
<tr> | |
<th>Название</th> | |
<th>Количество</th> | |
<th>Купил</th> | |
<th>Сейчас</th> | |
</tr> | |
</thead> | |
<tbody></tbody> | |
<tfoot></tfoot> | |
</table> | |
`; | |
const tbody = viewList.querySelector('tbody'); | |
const tfoot = viewList.querySelector('tfoot'); | |
let buy = 0, | |
now = 0; | |
myItems.forEach(item => { | |
let obj = freshPrices.find(obj => obj.name == item.name); | |
if (typeof obj !== 'undefined') { | |
item.fresh = obj.price; | |
} | |
item.price = Math.round(item.price * 100) / 100; | |
buy += item.price * item.count; | |
now += item.fresh * item.count; | |
tbody.insertAdjacentHTML('beforeEnd', `<tr> | |
<td>${item.name}</td> | |
<td>${item.count}</td> | |
<td>${item.price}</td> | |
<td>${item.fresh}</td> | |
</tr>`) | |
}) | |
let mathResult = now * 0.87; | |
mathResult = Math.round(mathResult * 100) / 100 | |
tfoot.insertAdjacentHTML('beforeEnd', `<tr> | |
<td colspan=2 style="text-align: center;"><strong>Закуплено на </strong> ${buy} грн</td> | |
<td colspan=2 style="text-align: center;"><strong>Продашь за </strong> ${mathResult} грн</td> | |
</tr>`) | |
viewList.style.display = 'block'; | |
}) | |
}); | |
} | |
function marketHtml() { | |
const tab = document.querySelector("#tabMyMarketHistory"); | |
let addBtn = document.createElement('a'), | |
addAllBtn = document.createElement('a'); | |
addBtn.classList.add('item_market_action_button', 'item_market_action_button_green', 'item_market_action_button_contents'); | |
addBtn.innerText = "Добавить в купленные"; | |
addBtn.style.background = "url('https://steamcommunity-a.akamaihd.net/public/images/economy/btn_green_small.png')" | |
addBtn.style.backgroundRepeat = 'repeat-x'; | |
addBtn.style.backgroundPosition = "left bottom"; | |
addBtn.style.marginRight = "50px"; | |
addAllBtn.classList.add('item_market_action_but`ton', 'item_market_action_button_green', 'item_market_action_button_contents'); | |
addAllBtn.innerText = "Добавить ВСЕ купленные"; | |
addAllBtn.style.background = "url('https://steamcommunity-a.akamaihd.net/public/images/economy/btn_green_small.png')" | |
addAllBtn.style.backgroundRepeat = 'repeat-x'; | |
addAllBtn.style.backgroundPosition = "left bottom"; | |
addAllBtn.style.marginRight = "50px"; | |
tab.addEventListener("click", function () { | |
setTimeout(() => { | |
let block = document.querySelector('#tabContentsMyMarketHistoryTable'), | |
items = block.querySelectorAll('.market_recent_listing_row'), | |
pagination = document.querySelector("#tabContentsMyMarketHistory_links"), | |
buttonArea = document.querySelector('.pick_and_sell_button'); | |
items.forEach(item => { | |
const status = item.querySelector('.market_listing_gainorloss').innerText; | |
if (status === "+") { | |
item.querySelector('.market_listing_gainorloss').classList.add("status__"); | |
const chkbx = document.createElement('div'); | |
chkbx.classList.add('market_listing_left_cell', 'market_listing_gainorloss'); | |
chkbx.innerHTML = ' <input type="checkbox" class="chkb_for_add">'; | |
item.prepend(chkbx); | |
} | |
}) | |
buttonArea.prepend(addBtn); | |
buttonArea.prepend(addAllBtn); | |
pagination.addEventListener("click", function () { | |
setTimeout(() => { | |
let block = document.querySelector('#tabContentsMyMarketHistoryTable'), | |
items = block.querySelectorAll('.market_recent_listing_row'), | |
pagination = document.querySelector("#tabContentsMyMarketHistory_links"), | |
buttonArea = document.querySelector('.pick_and_sell_button'); | |
items.forEach(item => { | |
const status = item.querySelector('.market_listing_gainorloss').innerText; | |
if (status === "+") { | |
item.querySelector('.market_listing_gainorloss').classList.add("status__"); | |
const chkbx = document.createElement('div'); | |
chkbx.classList.add('market_listing_left_cell', 'market_listing_gainorloss'); | |
chkbx.innerHTML = ' <input type="checkbox" class="chkb_for_add">'; | |
item.prepend(chkbx); | |
} | |
}) | |
buttonArea.prepend(addBtn); | |
buttonArea.prepend(addAllBtn); | |
}, 2000) | |
}); | |
}, 2000) | |
}); | |
addBtn.addEventListener("click", function () { | |
write(); | |
}); | |
addAllBtn.addEventListener("click", function () { | |
write('all'); | |
}); | |
} | |
// Запуск функций | |
renderHtml(); | |
marketHtml(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment