-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
442 lines (390 loc) · 14.6 KB
/
Copy pathpopup.js
File metadata and controls
442 lines (390 loc) · 14.6 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Popup スクリプト
* スコア計算方式の選択UI を提供する
*/
/**
* ブラウザの言語設定に基づいてメッセージを取得する
* @returns {Object} 言語に対応したメッセージオブジェクト
*/
function getMessages() {
const lang = navigator.language.split('-')[0]; // "en-US" → "en"
const messages = {
ja: {
calculationMethodLabel: 'スコア計算方式',
scoreMethodStrictName: '厳密型',
scoreMethodPopularName: '普及型',
scoreMethodDescription: 'スコア = ステータス値 × スコア係数',
statName: 'ステータス',
multiplier: 'スコア係数',
critDMG: '会心ダメージ',
critRate: '会心率',
atkPercent: '攻撃力%',
hpPercent: 'HP%',
defPercent: '防御力%',
elementalMastery: '元素熟知',
energyRecharge: '元素チャージ効率',
targetEnergyRechargeDisplay: '目標チャージ効率 表示',
targetEnergyRechargeDescriptionHeader: '目標チャージ効率 表示をオンにした場合、以下のようにスコアが算出されます。',
subtotalScoreLabel: '小計スコア',
subtotalScoreDescription: '元々の合計スコア',
excessScoreLabel: '超過スコア',
excessScoreDescription: '目標チャージ効率を超過した元素チャージ効率のスコア',
totalScoreLabel: '合計スコア',
totalScoreDescription: '小計スコア - 超過スコア',
statusSaved: '設定を保存しました',
statusSaveError: '設定の保存に失敗しました',
statusInitError: '初期化に失敗しました'
},
en: {
calculationMethodLabel: 'Calculation Method',
scoreMethodStrictName: 'Strict',
scoreMethodPopularName: 'Popular',
scoreMethodDescription: 'Score = Stat Value × Score Coefficient',
statName: 'Stat',
multiplier: 'Score Coefficient',
critDMG: 'CRIT DMG',
critRate: 'CRIT Rate',
atkPercent: 'ATK%',
hpPercent: 'HP%',
defPercent: 'DEF%',
elementalMastery: 'Elemental Mastery',
energyRecharge: 'Energy Recharge',
targetEnergyRechargeDisplay: 'Target Energy Recharge Display',
targetEnergyRechargeDescriptionHeader: 'When Target Energy Recharge Display is enabled, scores are calculated as follows:',
subtotalScoreLabel: 'Subtotal Score',
subtotalScoreDescription: 'Original total score',
excessScoreLabel: 'Excess Score',
excessScoreDescription: 'Energy Recharge score exceeding target',
totalScoreLabel: 'Total Score',
totalScoreDescription: 'Subtotal Score - Excess Score',
statusSaved: 'Settings saved successfully',
statusSaveError: 'Failed to save settings',
statusInitError: 'Failed to initialize'
}
};
return messages[lang] || messages['ja']; // フォールバック
}
// config.jsのデータを使用して多言語対応のスコア計算方式オブジェクトを作成
function createLocalizedScoreCalculationMethods() {
const messages = getMessages();
return Object.freeze({
STRICT: {
name: messages.scoreMethodStrictName,
description: messages.scoreMethodDescription,
multipliers: SCORE_CALCULATION_METHODS.STRICT.multipliers
},
POPULAR: {
name: messages.scoreMethodPopularName,
description: messages.scoreMethodDescription,
multipliers: SCORE_CALCULATION_METHODS.POPULAR.multipliers
}
});
}
const LOCALIZED_SCORE_METHODS = createLocalizedScoreCalculationMethods();
// デフォルト設定
const DEFAULT_METHOD = 'STRICT';
const STORAGE_KEY = 'selectedCalculationMethod';
const TARGET_ER_DISPLAY_KEY = 'targetEnergyRechargeDisplay';
// DOM要素
let calculationMethodSelect;
let methodDescriptionDiv;
let targetEnergyRechargeToggle;
let statusDiv;
// 初期化
document.addEventListener('DOMContentLoaded', async () => {
initializeElements();
await initializeUI();
setupEventListeners();
});
/**
* DOM要素の初期化
*/
function initializeElements() {
calculationMethodSelect = document.getElementById('calculationMethod');
methodDescriptionDiv = document.getElementById('methodDescription');
targetEnergyRechargeToggle = document.getElementById('targetEnergyRechargeToggle');
statusDiv = document.getElementById('status');
}
/**
* UIの初期化
*/
async function initializeUI() {
try {
// 国際化対応
updateI18nText();
// プルダウンのオプションを生成
populateMethodOptions();
// 保存された設定を読み込み
const savedMethod = await loadSavedMethod();
const savedToggleState = await loadTargetERDisplaySetting();
// 選択状態を復元
calculationMethodSelect.value = savedMethod;
updateDescription(savedMethod);
updateToggleState(savedToggleState);
} catch (error) {
console.error('Failed to initialize popup UI:', error);
showStatus('statusInitError', 'error');
}
}
/**
* 国際化対応のテキスト更新
*/
function updateI18nText() {
const messages = getMessages();
const i18nElements = document.querySelectorAll('[data-i18n]');
i18nElements.forEach(element => {
const key = element.getAttribute('data-i18n');
const message = messages[key];
if (message) {
element.textContent = message;
}
});
}
/**
* プルダウンのオプションを生成
*/
function populateMethodOptions() {
// 既存のオプションをクリア
calculationMethodSelect.innerHTML = '';
// 各計算方式をオプションとして追加
Object.entries(LOCALIZED_SCORE_METHODS).forEach(([key, method]) => {
const option = document.createElement('option');
option.value = key;
option.textContent = method.name;
calculationMethodSelect.appendChild(option);
});
}
/**
* スコア係数表のHTMLを生成する
* @param {string} methodKey - 計算方式のキー(STRICT/POPULAR)
* @returns {string} 表形式のHTML
*/
function generateScoreTable(methodKey) {
const messages = getMessages();
const multipliers = LOCALIZED_SCORE_METHODS[methodKey].multipliers;
const statNames = {
CRIT_DMG: messages.critDMG,
CRIT_RATE: messages.critRate,
ATK_PERCENT: messages.atkPercent,
HP_PERCENT: messages.hpPercent,
DEF_PERCENT: messages.defPercent,
ELEMENTAL_MASTERY: messages.elementalMastery,
ENERGY_RECHARGE: messages.energyRecharge
};
// 数値を省略表記に変換
const formatMultiplier = (value) => {
if (value === 1.0) return '1.0';
if (value === 2.0) return '2.0';
// 割り切れない数値は3桁で切って省略記号を付ける
const rounded = Math.round(value * 1000) / 1000;
const str = rounded.toString();
if (str !== value.toString() && value % 1 !== 0) {
return str + '...';
}
return rounded.toString();
};
let tableHTML = `
<table style="width: 100%; border-collapse: collapse; margin-bottom: 8px;">
<thead>
<tr style="background: #f0f0f0;">
<th style="border: 1px solid #ddd; padding: 6px; text-align: left; font-weight: 500;">${messages.statName}</th>
<th style="border: 1px solid #ddd; padding: 6px; text-align: center; font-weight: 500;">${messages.multiplier}</th>
</tr>
</thead>
<tbody>
`;
Object.entries(multipliers).forEach(([statKey, multiplier]) => {
if (multiplier > 0) { // 実数ステータス(0)は表示しない
tableHTML += `
<tr>
<td style="border: 1px solid #ddd; padding: 6px;">${statNames[statKey]}</td>
<td style="border: 1px solid #ddd; padding: 6px; text-align: center;">${formatMultiplier(multiplier)}</td>
</tr>
`;
}
});
tableHTML += `
</tbody>
</table>
`;
return tableHTML;
}
/**
* イベントリスナーの設定
*/
function setupEventListeners() {
calculationMethodSelect.addEventListener('change', handleMethodChange);
targetEnergyRechargeToggle.addEventListener('click', handleToggleChange);
}
/**
* 計算方式変更のハンドラ
*/
async function handleMethodChange(event) {
const selectedMethod = event.target.value;
try {
// 選択された方式を保存
await saveSelectedMethod(selectedMethod);
// 説明文を更新
updateDescription(selectedMethod);
// 成功メッセージを表示
showStatus('statusSaved', 'success');
// content script に変更を通知
await notifyContentScript(selectedMethod);
} catch (error) {
console.error('Failed to save calculation method:', error);
showStatus('statusSaveError', 'error');
}
}
/**
* 説明文の更新
*/
function updateDescription(methodKey) {
const method = LOCALIZED_SCORE_METHODS[methodKey];
if (method) {
// 補足情報と詳細な係数表を表示
const descriptionText = `<div style="font-size: 11px; color: #888;">${method.description}</div>`;
const tableHTML = generateScoreTable(methodKey);
methodDescriptionDiv.innerHTML = tableHTML + descriptionText;
}
}
/**
* ステータスメッセージの表示
*/
function showStatus(messageKey, type) {
const messages = getMessages();
const message = messages[messageKey] || messageKey; // フォールバック
// 既存のメッセージがある場合は一旦非表示にする
statusDiv.style.display = 'none';
// 少し遅延してから新しいメッセージを表示
setTimeout(() => {
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
statusDiv.style.display = 'block';
// 3秒後に非表示
setTimeout(() => {
statusDiv.style.display = 'none';
}, 3000);
}, 50);
}
/**
* 保存された計算方式を読み込み
*/
async function loadSavedMethod() {
try {
const result = await chrome.storage.sync.get(STORAGE_KEY);
return result[STORAGE_KEY] || DEFAULT_METHOD;
} catch (error) {
console.error('Failed to load saved method:', error);
return DEFAULT_METHOD;
}
}
/**
* 選択された計算方式を保存
*/
async function saveSelectedMethod(method) {
try {
await chrome.storage.sync.set({ [STORAGE_KEY]: method });
} catch (error) {
console.error('Failed to save method:', error);
throw error;
}
}
/**
* content script に設定変更を通知
*/
async function notifyContentScript(selectedMethod) {
try {
// 対象URLにマッチするすべてのタブを取得
const tabs = await chrome.tabs.query({
url: 'https://act.hoyolab.com/app/community-game-records-sea/index.html*'
});
let successCount = 0;
let errorCount = 0;
// 各タブに設定変更を通知
for (const tab of tabs) {
try {
await chrome.tabs.sendMessage(tab.id, {
type: 'CALCULATION_METHOD_CHANGED',
method: selectedMethod
});
successCount++;
} catch (error) {
errorCount++;
// content scriptが読み込まれていない場合は警告を出さない
if (error.message !== 'Could not establish connection. Receiving end does not exist.') {
console.warn(`Failed to notify tab ${tab.id}:`, error.message);
}
}
}
// 通知完了(詳細ログは不要)
} catch (error) {
console.error('Failed to query tabs or send notifications:', error);
}
}
/**
* トグル変更のハンドラ
*/
async function handleToggleChange() {
const isActive = targetEnergyRechargeToggle.classList.contains('active');
const newState = !isActive;
try {
// 設定を保存
await chrome.storage.local.set({ [TARGET_ER_DISPLAY_KEY]: newState });
// UI状態を更新
updateToggleState(newState);
// content scriptに通知
await notifyTargetERDisplayChange(newState);
// 成功メッセージを表示
showStatus('statusSaved', 'success');
} catch (error) {
console.error('Failed to save toggle setting:', error);
showStatus('statusSaveError', 'error');
}
}
/**
* トグル状態を更新
*/
function updateToggleState(isActive) {
if (isActive) {
targetEnergyRechargeToggle.classList.add('active');
} else {
targetEnergyRechargeToggle.classList.remove('active');
}
}
/**
* 目標チャージ効率表示設定を読み込み
*/
async function loadTargetERDisplaySetting() {
try {
const result = await chrome.storage.local.get(TARGET_ER_DISPLAY_KEY);
return result[TARGET_ER_DISPLAY_KEY] !== undefined ? result[TARGET_ER_DISPLAY_KEY] : false; // デフォルトはオフ
} catch (error) {
console.error('Failed to load target ER display setting:', error);
return false; // デフォルト値
}
}
/**
* content scriptに目標チャージ効率表示変更を通知
*/
async function notifyTargetERDisplayChange(isEnabled) {
try {
const tabs = await chrome.tabs.query({
url: 'https://act.hoyolab.com/app/community-game-records-sea/index.html*'
});
for (const tab of tabs) {
try {
await chrome.tabs.sendMessage(tab.id, {
type: 'TARGET_ER_DISPLAY_CHANGED',
enabled: isEnabled
});
} catch (error) {
// content scriptが読み込まれていない場合は警告を出さない
if (error.message !== 'Could not establish connection. Receiving end does not exist.') {
console.warn(`Failed to notify tab ${tab.id}:`, error.message);
}
}
}
} catch (error) {
console.error('Failed to query tabs or send notifications:', error);
}
}