Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
148 lines (128 loc) · 5.58 KB
/
Copy pathpopup.js
File metadata and controls
148 lines (128 loc) · 5.58 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('scan-btn').addEventListener('click', async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab.url.includes("tradingview.com")) {
alert("Please open TradingView!");
return;
}
chrome.tabs.sendMessage(tab.id, { action: "scan" }, (response) => {
if (response && response.inputs) {
currentStrategyName = response.strategyName || "Unknown_Strategy";
renderInputs(response.inputs);
} else if (response && response.error) {
alert(response.error);
} else {
alert("Failed to scan. Is the Settings dialog open? Injecting might require a page refresh if the extension was just installed.");
}
});
});
document.getElementById('start-btn').addEventListener('click', () => {
const permutations = generatePermutations();
const target = document.getElementById('target-select').value;
if (permutations.length === 0) {
alert("No changing inputs to perform optimization.");
return;
}
if (permutations.length > 500) {
if (!confirm(`Warning: You are about to run ${permutations.length} permutations. This might take a while. Continue?`)) {
return;
}
}
chrome.runtime.sendMessage({
action: "start_optimization",
permutations: permutations,
target: target,
strategyName: currentStrategyName
});
alert(`Optimization started! Running ${permutations.length} iterations. Do not close the TradingView tab.`);
window.close();
});
});
let capturedInputs = [];
let currentStrategyName = "Unknown_Strategy";
function renderInputs(inputs) {
capturedInputs = inputs;
const container = document.getElementById('inputs-container');
container.innerHTML = '';
if (inputs.length === 0) {
container.innerHTML = "<i>No inputs found.</i>";
return;
}
inputs.forEach((input, index) => {
const div = document.createElement('div');
div.className = 'input-row';
const isCheckbox = input.type === 'checkbox';
div.innerHTML = `
<div class="row-header">
<label title="${input.label}">${input.label}</label>
<div class="opt-toggle-container">
Opt.
<input type="checkbox" id="opt-enable-${index}" class="opt-checkbox">
</div>
</div>
<div id="controls-${index}" class="range-inputs">
${isCheckbox ? `
<span class="boolean-label">Will test: True & False</span>
` : `
<input type="number" id="start-${index}" placeholder="Start" value="${input.value}" disabled>
<input type="number" id="end-${index}" placeholder="End" value="${input.value}" disabled>
<input type="number" id="step-${index}" placeholder="Step" value="0" disabled title="Use 0 for static value">
`}
</div>
`;
container.appendChild(div);
// Toggle disabled state based on 'Opt.' checkbox
const toggle = div.querySelector(`#opt-enable-${index}`);
toggle.addEventListener('change', (e) => {
const controls = div.querySelectorAll('input:not(.opt-checkbox)');
controls.forEach(c => c.disabled = !e.target.checked);
});
});
document.getElementById('optimization-options').style.display = 'block';
}
function generatePermutations() {
const ranges = capturedInputs.map((input, index) => {
const isOptimizing = document.getElementById(`opt-enable-${index}`).checked;
const vals = [];
if (input.type === 'checkbox') {
if (isOptimizing) {
vals.push("true", "false");
} else {
vals.push(input.checked.toString()); // Current static state
}
} else {
if (isOptimizing) {
const start = parseFloat(document.getElementById(`start-${index}`).value);
const end = parseFloat(document.getElementById(`end-${index}`).value);
const step = parseFloat(document.getElementById(`step-${index}`).value);
if (isNaN(start) || isNaN(end) || isNaN(step) || step === 0) {
vals.push(input.value);
} else {
if (start <= end && step > 0) {
for (let v = start; v <= end; v += step) vals.push(Number(v.toPrecision(10)).toString());
} else if (start >= end && step < 0) {
for (let v = start; v >= end; v += step) vals.push(Number(v.toPrecision(10)).toString());
} else {
vals.push(input.value);
}
}
} else {
vals.push(input.value); // Static original value
}
}
return { name: input.name, label: input.label, values: vals, type: input.type };
});
// Cartesian product
const combine = (arr) => {
if (arr.length === 0) return [{}];
const result = [];
const rest = combine(arr.slice(1));
for (let val of arr[0].values) {
for (let r of rest) {
result.push({ ...r, [arr[0].name]: val });
}
}
return result;
};
return combine(ranges);
}