Your Items CSV Exporter

Copies a CSV of your displayed items to the clipboard for pasting elsewhere.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Your Items CSV Exporter
// @namespace    burgerdroid.itemexporter
// @version      1.62
// @license      MIT
// @description  Copies a CSV of your displayed items to the clipboard for pasting elsewhere.
// @author       Burgerdroid
// @match        https://www.torn.com/item.php
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// ==/UserScript==


(function () {
    //Update this line if you are NOT using TornPDA
    const key = "###PDA-APIKEY###";

	const apiUrl = `https://api.torn.com/torn/?selections=items&key=${key}`;
	const btnStyles = document.createElement('style');
	btnStyles.innerHTML = `
	#get-values-btn, #export-btn {
		color: #EEE;
		text-shadow: 0 0 5px #000;
		background: #666;
		border: 1px solid #111;
		padding: 10px;
		margin-left: 5px;
		line-height: 1.5;
	}
	.btnOK {
		background: green !important;
	}
	.btnNotRun {
		background: red !important;
	}
	`
	document.head.appendChild(btnStyles);

	let marketValues = {};
	let sections = document.querySelectorAll('ul.items-cont');
	let activeSection = null;
	let getValuesBtn = document.createElement('button');
	let exportBtn = document.createElement('button');
	let btnDiv = document.createElement('div');
	let insertPoint = document.querySelector('div.items-wrap').firstChild;


	function getPlayerItems() {
		//find which items sections the user has actively selected
        exportBtn.className = 'btnNotRun';
		for (var i=0; i<sections.length; i++) {
			if (sections[i].style.display == 'none') {
				continue;
			}
			activeSection = sections[i];
			console.log(activeSection.id);
		}
		let csv = 'ID,Item,Category,Qty,Market Value,Total';
		let item = activeSection.firstChild;

		while (item != null) {
			let id = item.getAttribute('data-item');
			let name = item.getAttribute('data-sort');
			let category = item.getAttribute('data-category');
			let qty = item.getAttribute('data-qty');
			let itemValue = marketValues[id];
			let total = qty * itemValue;
			csv += `\n${id},${name},${category},${qty},${itemValue},${total}`;
			item = item.nextSibling;
		}
		navigator.clipboard.writeText(csv);
        exportBtn.className = 'btnOK';
        exportBtn.innerHTML = 'Copied!';
		console.log(csv);
        setTimeout(() => {
            exportBtn.className = 'btnNotRun';
            exportBtn.innerHTML = 'Export Items';
        }, "1500");

	}


	async function createPromise() {
		try {
			const response = await fetch(apiUrl, {});
			const parsedResponse = await response.json();
			return parsedResponse;
		} catch(error) {
			console.log(error)
		}
	}


	function runApiQuery() {
		getValuesBtn.innerHTML = 'Retrieving';
		getValuesBtn.disabled = true;
		createPromise().then(result => {
			getMarketValues(result);
		});
	}


	function getMarketValues(result) {
		marketValues = {};
		let keyList = Object.keys(result.items);
		for (var i=0; i<keyList.length; i++) {
			marketValues[keyList[i]] = result.items[keyList[i]].market_value;
		}
		getValuesBtn.className = 'btnOK';
		getValuesBtn.innerHTML = 'Done!';
		exportBtn.disabled = false;
		exportBtn.className = 'btnNotRun';
	}


	function init() {
		//configure the buttons and add them above the Items header.
		getValuesBtn.id = 'get-values-btn';
		getValuesBtn.innerHTML = "Get Prices";
		getValuesBtn.className = 'btnNotRun';
		getValuesBtn.addEventListener('click', runApiQuery);

		exportBtn.id = 'export-btn';
		exportBtn.innerHTML = "Export Items"
		exportBtn.className = 'btnDisabled';
		exportBtn.addEventListener('click', getPlayerItems);
		exportBtn.disabled = true;

		btnDiv.style.paddingBottom = '1em';
		btnDiv.appendChild(getValuesBtn);
		btnDiv.appendChild(exportBtn);

		insertPoint.before(btnDiv);
	}

	init();
})();