-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgear_script_template.js
More file actions
89 lines (77 loc) · 3.62 KB
/
Copy pathgear_script_template.js
File metadata and controls
89 lines (77 loc) · 3.62 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
// gear_script_template.js
const gearData = {{GEAR_DATA_PLACEHOLDER}};
// Function to update the quantities
function updateQuantities(data) {
data.forEach(item => {
let itemName = item.item_name;
let quantity = item.quantity;
console.log("Updating " + itemName + " to quantity " + quantity);
// Find the element by the item name
let itemElement = jQuery("div.item-name").filter(function() {
return jQuery(this).text().trim() === itemName;
});
if (itemElement.length) {
// Find the input element within the same gear container
let inputElement = itemElement.closest("div.gear-container").find("input[type='number']");
if (inputElement.length) {
// Set the value of the input element
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
nativeInputValueSetter.call(inputElement[0], quantity);
// Create and dispatch the input event
let inputEvent = new Event('input', { bubbles: true });
inputElement[0].dispatchEvent(inputEvent);
// Create and dispatch the change event
let changeEvent = new Event('change', { bubbles: true });
inputElement[0].dispatchEvent(changeEvent);
// Create and dispatch the blur event
let blurEvent = new Event('blur', { bubbles: true });
inputElement[0].dispatchEvent(blurEvent);
}
}
});
}
// Function to reset all quantities to 0
function resetQuantities() {
jQuery("div.gear-container").each(function() {
let inputElement = jQuery(this).find("input[type='number']");
if (inputElement.length) {
// Set the value of the input element to 0
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
nativeInputValueSetter.call(inputElement[0], 0);
// Create and dispatch the input event
let inputEvent = new Event('input', { bubbles: true });
inputElement[0].dispatchEvent(inputEvent);
// Create and dispatch the change event
let changeEvent = new Event('change', { bubbles: true });
inputElement[0].dispatchEvent(changeEvent);
// Create and dispatch the blur event
let blurEvent = new Event('blur', { bubbles: true });
inputElement[0].dispatchEvent(blurEvent);
console.log("Set value to 0 for " + inputElement.prevObject[0].innerText.split('\n')[0])
}
});
}
// Check if jQuery is loaded
if (typeof jQuery === 'undefined') {
// If not, load jQuery
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
script.onload = function() {
// Run the resetQuantities function after jQuery is loaded
resetQuantities();
// Run the updateQuantities function after reset
setTimeout(function() {
updateQuantities(gearData);
}, 1000); // Delay before updating quantities (adjust delay as needed)
};
document.head.appendChild(script);
} else {
// If jQuery is already loaded, run the functions
resetQuantities();
console.log("Quantities reset")
// Delay before updating quantities (adjust delay as needed)
setTimeout(function() {
updateQuantities(gearData);
console.log("Quantities updated!")
}, 1000); // Delay before updating quantities (adjust delay as needed)
}