Skip to content

Instantly share code, notes, and snippets.

@bzimor
Created May 20, 2024 07:23
Show Gist options
  • Save bzimor/ce92c4d87b8c6529ab4de467d96a1c60 to your computer and use it in GitHub Desktop.
Save bzimor/ce92c4d87b8c6529ab4de467d96a1c60 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Auction item parser
// @author RyuFive
// @match https://www.torn.com/amarket.php*
// @namespace https://github.com/RyuFive/TornScripts/raw/main/Auction Names.user.js
// @require https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// @version 1.4
// @description try to take over the world!
// @grant GM_xmlhttpRequest
// @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @license MIT
// ==/UserScript==
function getUrlParameterFromHash(parameter) {
// Get the hash part of the current URL
var hash = window.location.hash;
// Find the index of the parameter in the hash fragment
var startIndex = hash.indexOf(parameter + "=");
// If parameter is found
if (startIndex !== -1) {
// Extract the value from the hash fragment
var startValue = hash.substring(startIndex + parameter.length + 1); // Adding 1 for '='
// Return the value
return startValue;
}
// If parameter not found, return null
return null;
}
function sendAuctionItemDataBatch(data) {
const givenUrl = 'http://135.181.88.119:8081/api/auction_house_item';
// console.log(data)
GM_xmlhttpRequest({
method: 'POST',
url: givenUrl,
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(data),
onload: function(response) {
if (response.status === 200 || response.status === 201) {
console.log('Success:', response.responseText);
} else {
console.error('Error:', response.responseText);
}
},
onerror: function(error) {
console.error('Error:', error);
}
});
}
function sendAuctionItemData(uid, item_id, seller, auctionEndTime, elementId) {
//const givenUrl = 'http://127.0.0.1:8000/api/auction_house_item';
const givenUrl = 'http://135.181.88.119:8081/api/auction_house_item';
const postData = {
uuid: uid,
item_id, item_id,
seller: seller,
auction_endtime: auctionEndTime,
};
//console.log(postData);
GM_xmlhttpRequest({
method: 'POST',
url: givenUrl,
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(postData),
onload: function(response) {
if (response.status === 200 || response.status === 201) {
//var liElement = document.getElementById(elementId);
//liElement.innerHTML += "<div>Done</div>";
console.log('Success:', response.responseText);
} else {
console.error('Error:', response.responseText);
}
},
onerror: function(error) {
console.error('Error:', error);
}
});
}
function parseAuctionItems() {
var startValue = getUrlParameterFromHash('start');
if (startValue !== null && !uniqueStartValues.includes(startValue)) {
uniqueStartValues.push(startValue);
console.log(startValue);
const liElements = document.querySelectorAll('div.items-list-wrap > ul.items-list.t-blue-cont.h > li');
const data = [];
liElements.forEach(liElement => {
// List item id from li element id
const listItemID = liElement.id;
if (listItemID) {
// Item uid from armoury property of element with class "item-hover"
const itemHoverElement = liElement.querySelector('span.item-hover');
const itemUID = itemHoverElement ? itemHoverElement.getAttribute('armoury') : null;
const itemID = itemHoverElement ? itemHoverElement.getAttribute('item') : null;
// Item seller id from href after the string "profiles.php?XID=" in div with class "seller-wrap"
const sellerWrapDiv = liElement.querySelector('div.seller-wrap');
let sellerID = null;
if (sellerWrapDiv) {
const sellerLink = sellerWrapDiv.querySelector('a[href*="profiles.php?XID="]');
sellerID = sellerLink ? sellerLink.getAttribute('href').split('profiles.php?XID=')[1] : null;
}
// Timestamp from title of first span element in div with class "time-wrap"
const timeWrapDiv = liElement.querySelector('div.time-wrap');
const timestamp = timeWrapDiv ? timeWrapDiv.querySelector('span[title]').getAttribute('title') : null;
data.push({
uuid: itemUID,
item_id: itemID,
seller: sellerID,
auction_endtime: timestamp,
});
liElement.innerHTML += "DONE";
}
});
sendAuctionItemDataBatch(data);
}
}
var uniqueStartValues = [];
waitForKeyElements(".item-cont-wrap ", parseAuctionItems, false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment