-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgw2pc-t6-stack-price-with-discount.user.js
More file actions
102 lines (91 loc) · 3.69 KB
/
Copy pathgw2pc-t6-stack-price-with-discount.user.js
File metadata and controls
102 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// ==UserScript==
// @name GW2PC T6 Stack Price with Discount
// @namespace https://gw2pc.com/
// @version 1.0
// @description Show the price of a whole stack (250) for each of the T6 materials with a 10% discount on https://gw2pc.com/t6/
// @author Korunos
// @match https://gw2pc.com/t6/
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to convert price strings to copper value
function parsePrice(priceStr) {
const match = priceStr.match(/(\d+)s (\d+)c/) || priceStr.match(/(\d+)g (\d+)s (\d+)c/);
if (match) {
if (match.length === 3) {
const [_, s, c] = match.map(Number);
return (s * 100) + c;
} else if (match.length === 4) {
const [_, g, s, c] = match.map(Number);
return (g * 10000) + (s * 100) + c;
}
}
return 0;
}
// Function to format numbers as in-game currency
function formatCurrency(copper) {
const gold = Math.floor(copper / 10000);
const silver = Math.floor((copper % 10000) / 100);
const copperRemainder = copper % 100;
if (gold > 0) {
return `${gold}g ${silver}s ${copperRemainder}c`;
} else {
return `${silver}s ${copperRemainder}c`;
}
}
// Function to calculate and display the stack price with discount
function displayStackPrice() {
const table = document.querySelector('.price-table');
if (!table) {
console.error('Table not found');
return;
}
const headerRow = table.querySelector('thead tr');
const rows = table.querySelectorAll('tbody tr');
if (!headerRow || rows.length === 0) {
console.error('Table rows or header not found');
return;
}
// Add a new header for the discounted stack price
const newHeader = document.createElement('th');
newHeader.innerText = 'Discounted Stack Price (90%)';
newHeader.style.textAlign = 'center'; // Center the header text
newHeader.style.width = '150px'; // Set a fixed width
headerRow.appendChild(newHeader);
// Iterate through each row to calculate the discounted stack price
rows.forEach(row => {
const depth250Cell = row.querySelector('td:nth-child(4)');
if (!depth250Cell) {
console.error('Depth 250 cell not found for row');
return;
}
const priceText = depth250Cell.innerText.trim();
const price = parsePrice(priceText);
if (!isNaN(price) && price > 0) {
const stackPrice = price * 250;
const discountedPrice = stackPrice * 0.9;
const newCell = document.createElement('td');
newCell.innerText = formatCurrency(discountedPrice);
newCell.style.color = 'red';
newCell.style.textAlign = 'center'; // Center the cell text
newCell.style.width = '150px'; // Set a fixed width
row.appendChild(newCell);
} else {
console.error(`Failed to parse price: ${priceText}`);
}
});
}
// Function to periodically check for the table until it is available
function waitForTable() {
const checkInterval = setInterval(() => {
const table = document.querySelector('.price-table');
if (table) {
clearInterval(checkInterval);
displayStackPrice();
}
}, 100); // Check every 100ms
}
// Start checking for the table as soon as the script is loaded
waitForTable();
})();