-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHqPCB-MMImpedance.user.js
308 lines (282 loc) · 15.8 KB
/
HqPCB-MMImpedance.user.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ==UserScript==
// @name HqPCB-MMImpedance
// @name:zh-CN HqPCB-毫米计算阻抗
// @namespace https://github.com/Mehver
// @version 1.1
// @description 华秋PCB阻抗计算页面本来只有mil,添加单位选择,可以切换到mm。
// @description:zh-CN 华秋PCB阻抗计算页面本来只有mil,添加单位选择,可以切换到mm。
// @author https://github.com/Mehver
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAAXNSR0IB2cksfwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAuJQTFRFAAAA5lxd5lxd5lxd5lxd5lxd5lxd5lxd5lxd5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6COLPt//DY058lLj2y//r/3EP/xEbg/+ZVPk/PwRzZ/00j2f//0FZZFGno/0dZ+v//x8blM2TrODUTiF///6/U9jcJvf//1+r3N1GaYpy+8P//6+3Qj139/9DyNlDU3K395lUWtP//wDE5FVl3Uxmk6tS+rSKW/7UbFkDwoA1FX29jU0T8zX/hScnx8+FSvdBgiZdKxAAAAlBJREFUeJyNk21Ik1EUx89/tU2RqaNJwZorwgbFUkTUyl6UqIVChR/2aZm9fUwqioJCCIJeoA/2uQwqkhBrURAOzIWEJRm0CCkbMZ9GkZVl6p5mPd23Z3P0Ie+Hew/n/7vnnnvOvaD/DMwTgBx6jpYnfNxyKMOwAOMZvQRpIhv3WxchpZz5gCYtD/CDLUUMWK5PZzNxIm6IM1eIWCVTIB8+MWvJrBVJrrgR40vp7AzX876CyhPM8uJlOctjlJlleEpUixEOFyY00PrXRKsHi1lAe/VzO3P7Ed08ZGOGa2m/wa7Z8IKosk8c7KqIf2NLVdo2wMjqpPshr8P2QeaKqNT9T1i+6zDNNtVhWCMT2HjPkMRO9LO5Hn1EqcZuWcll34m2vBtW17QGBtR9q26rUgd72bINb1dCizKrwCaBRlxXQOmk2mxYmh6NUUtYVK3BcS3TrNa7mTruuqnTvh5uNU90ZwBP4MGMSQQcqn+ikR1muzdVsNqEeQlaMeeBABfmPpiWKLvOwd8LRQgR4KejPedFncElOjrh5NK5k3xOug/nAJ60Tu2ah0unzvL5je8AB3zHgL2C6DzCgaJXfrR18BMkcCvyLH519y+m22tiEiguEymMS+AO390Z1Cn0fjQlARFurQmE9/CXUbPjzwKESAL1mCzElAlUjtizeWZy6PqcbwLUG8wFVgEf2rqsIo0NHLA8RpOp157uaebC1oj8SGtEHWJAnZDvO0M3ruzPVvLyIa8qVII7Ae/isY+4qIIdx3k64fr3835R3ZQxCubxu/8C8y6rm68DyM4AAAAASUVORK5CYII=
// @match https://www.hqpcb.com/quote/impedance
// @license MPL-2.0
// @license Mozilla Public License 2.0
// @charset UTF-8
// @homepageURL https://github.com/SynRGB/HqPCB-MMImpedance
// @contributionURL https://github.com/SynRGB/HqPCB-MMImpedance
// ==/UserScript==
(function () {
'use strict';
/**
* 添加单位选择下拉菜单
*/
function addUnitSelector() {
let impedanceParams = document.querySelector("body > div.impedance > div > div.impedance__content > div.impedance__params > ul");
let li = document.createElement("li");
li.innerHTML = "<span>计算单位</span><span> <select id='unit-select' style='width: 100px;border: 7px solid #FFF;color: #777;'><option value='mil' style='color: #777;'>mil</option><option value='mm' style='color: #777;'>mm</option></select></span>";
impedanceParams.appendChild(li);
}
/**
* 更新表头文本
*/
function updateTableHeaders() {
const headers = [
"body > div.impedance > div > div.impedance__content > div.impedance__params > div > div.el-table.el-table--fit.el-table--border.el-table--enable-row-hover.el-table--enable-row-transition.impedance__params__list.impedance-table > div.el-table__header-wrapper > table > thead > tr > th.el-table_1_column_7.is-leaf > div",
"body > div.impedance > div > div.impedance__content > div.impedance__params > div > div.el-table.el-table--fit.el-table--border.el-table--enable-row-hover.el-table--enable-row-transition.impedance__params__list.impedance-table > div.el-table__header-wrapper > table > thead > tr > th.el-table_1_column_8.is-leaf > div",
"body > div.impedance > div > div.impedance__content > div.impedance__params > div > div.el-table.el-table--fit.el-table--border.el-table--enable-row-hover.el-table--enable-row-transition.impedance__params__list.impedance-table > div.el-table__header-wrapper > table > thead > tr > th.el-table_1_column_9.is-leaf > div"
];
const unit = document.getElementById('unit-select').value;
headers.forEach(selector => {
const header = document.querySelector(selector);
if (header) {
if (header.innerText.includes('(mil)')) {
header.innerText = header.innerText.replace('(mil)', ` (mil)`);
} else if (header.innerText.includes('(mm)')) {
header.innerText = header.innerText.replace('(mm)', ` (mm)`);
}
if (header.innerText.includes('(mil)')) {
header.innerText = header.innerText.replace('(mil)', ` (${unit})`);
} else if (header.innerText.includes('(mm)')) {
header.innerText = header.innerText.replace('(mm)', ` (${unit})`);
}
}
});
}
/**
* 根据单位选择进行转换
*/
function resultConverter() {
const unit = document.getElementById('unit-select').value;
const listItems = document.querySelectorAll('div.impedance__list__item');
listItems.forEach(item => {
const rows = item.querySelectorAll('.el-table__row');
rows.forEach(row => {
const tds = row.querySelectorAll('td');
if (unit === 'mm') {
[4, 5, 6].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value * 0.0254).toFixed(3);
}
}
});
[9, 10, 11].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div > div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value * 0.0254).toFixed(3);
}
}
});
} else {
[4, 5, 6].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value).toFixed(3);
}
}
});
[9, 10, 11].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div > div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value).toFixed(3);
}
}
});
}
});
// 处理表头部分,添加单位
const firstThs = item.querySelectorAll('th');
[4, 5, 6, 11].forEach(thIndex => {
if (firstThs[thIndex]) {
const div = firstThs[thIndex].querySelector('div');
if (div && !div.innerText.includes('(')) { // 防止重复添加
div.innerText = div.innerText + `\n(${unit})`;
} else if (div) {
div.innerText = div.innerText.replace(/\((mil|mm)\)/, `(${unit})`);
}
}
});
[9, 10].forEach(thIndex => {
if (firstThs[thIndex]) {
const div = firstThs[thIndex].querySelector('div');
if (div && !div.innerText.includes('(')) { // 防止重复添加
div.innerText = div.innerText + ` (${unit})`;
} else if (div) {
div.innerText = div.innerText.replace(/\((mil|mm)\)/, `(${unit})`);
}
}
});
if (firstThs[12]) {
const div = firstThs[12].querySelector('div');
if (div && !div.innerText.includes('(')) { // 防止重复添加
div.innerText = div.innerText + `\n(ohm)`;
}
}
});
// 更新表头单位
updateTableHeaders();
}
/**
* 某些情况下原始值是mm,需要转换为mil
*/
function resultReverseConverter() {
const listItems = document.querySelectorAll('div.impedance__list__item');
listItems.forEach(item => {
const rows = item.querySelectorAll('.el-table__row');
rows.forEach(row => {
const tds = row.querySelectorAll('td');
[4, 5, 6].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value / 0.0254).toFixed(3);
}
}
});
[9, 10, 11].forEach(tdIndex => {
if (tds[tdIndex]) {
const div = tds[tdIndex].querySelector('div > div');
if (div && div.innerText && div.innerText !== '/' && !isNaN(parseFloat(div.innerText))) {
const value = parseFloat(div.innerText);
div.innerText = (value / 0.0254).toFixed(3);
}
}
});
});
});
}
/**
* 模拟用户输入事件
* @param {HTMLElement} element 目标输入框
* @param {string} value 输入的值
*/
function simulateInput(element, value) {
element.focus();
element.value = value;
element.dispatchEvent(new Event('input', { bubbles: true }));
element.dispatchEvent(new Event('change', { bubbles: true }));
element.blur();
}
/**
* 创建并返回自定义输入框
* @param {HTMLElement} originalInput 原输入框
* @returns {HTMLElement} 自定义输入框
*/
function createCustomInput(originalInput) {
const customInputWrapper = document.createElement('div');
customInputWrapper.style.display = 'inline-block';
customInputWrapper.style.paddingLeft = '5px'; // 左边添加空格
const customInput = document.createElement('input');
customInput.type = 'text';
customInput.className = 'custom-input';
customInput.style = originalInput.style.cssText + 'border: none; background: none;';
customInput.placeholder = '请输入';
customInput.value = originalInput.value;
originalInput.style.display = 'none';
customInput.addEventListener('blur', () => {
const unit = document.getElementById('unit-select').value;
let value = parseFloat(customInput.value);
if (!isNaN(value)) {
const milValue = unit === 'mm' ? (value / 0.0254).toFixed(3) : value.toFixed(3);
simulateInput(originalInput, milValue);
}
});
customInput.addEventListener('input', () => {
const unit = document.getElementById('unit-select').value;
let value = parseFloat(customInput.value);
if (!isNaN(value)) {
const milValue = unit === 'mm' ? (value / 0.0254).toFixed(3) : value.toFixed(3);
originalInput.value = milValue;
}
});
// 监听单位选择变化,更新输入框的值
document.getElementById('unit-select').addEventListener('change', () => {
const unit = document.getElementById('unit-select').value;
let value = parseFloat(originalInput.value);
if (!isNaN(value)) {
const displayValue = unit === 'mm' ? (value * 0.0254).toFixed(3) : value.toFixed(3);
customInput.value = displayValue;
}
});
customInputWrapper.appendChild(customInput);
originalInput.parentElement.appendChild(customInputWrapper);
return customInput;
}
/**
* 替换原输入框为自定义输入框并处理单位转换
*/
function replaceInputs() {
const rows = document.querySelectorAll('.el-table__row');
rows.forEach(row => {
const tds = row.querySelectorAll('td');
for (let i = 5; i < tds.length; i++) {
const input = tds[i].querySelector('.el-input-number.el-input-number--mini.is-without-controls input');
if (input && !input.parentElement.querySelector('.custom-input')) {
createCustomInput(input);
}
}
});
}
/**
* 监控特定元素的变化并动态替换输入框
*/
function monitorChanges() {
const target = document.querySelector("body > div.impedance > div > div.impedance__content > div.impedance__params");
if (target) {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length || mutation.removedNodes.length) {
replaceInputs();
}
});
});
observer.observe(target, { childList: true, subtree: true });
}
}
/**
* 初始化脚本
*/
function init() {
addUnitSelector();
replaceInputs();
monitorChanges();
// 监听单位选择变化,重新匹配
document.getElementById('unit-select').addEventListener('change', () => {
resultConverter(); // 重新转换所有值
if (document.getElementById('unit-select').value === 'mil') {
resultReverseConverter();
}
});
// 监听按钮点击事件,重新匹配
const button = document.querySelector("body > div.impedance > div > div.impedance__content > div.impedance__params > div > div.impedance__params__btns > div > button.el-button.el-button--primary.el-button--mini");
if (button) {
button.addEventListener('click', function () {
setTimeout(() => {
resultConverter();
replaceInputs();
}, 1500);
});
}
}
// 初始化脚本
init();
})();
console.log("Userscript HqPCB-MMImpedance (HqPCB-毫米计算阻抗) loaded. See more details at https://github.com/SynRGB/HqPCB-MMImpedance");