-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
1089 lines (990 loc) · 40.5 KB
/
Copy pathbackground.js
File metadata and controls
1089 lines (990 loc) · 40.5 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2024-2026 SAYU
// This software is released under the MIT License, see LICENSE.
import { CONTENT_SCRIPTS_CONFIG, GAS_SETUP_CONFIG, CONTEXT_MENU_ID } from './scripts.config.js';
import { isVersionNewer } from './features/modules/version-utils.js';
const SUBJECT_FILTER_STORAGE_KEY = 'klpf-course-filter-settings';
const ATTENDANCE_CACHE_KEY = 'klpf-attendance-rate-cache';
const ATTENDANCE_CACHE_VERSION = 3;
const ATTENDANCE_FETCH_JOB_KEY = 'klpf-attendance-fetch-job';
const ATTENDANCE_BROWSER_SESSION_KEY = 'klpf-attendance-browser-session';
const ATTENDANCE_MANUAL_REFRESH_KEY = 'klpf-attendance-manual-refresh';
const ATTENDANCE_RATE_FEATURE_KEY = 'attendanceRateDisplay';
const ATTENDANCE_RATE_CONSENT_KEY = 'attendanceRateAccessConsent';
const ATTENDANCE_FETCH_JOB_TIMEOUT_MS = 2 * 60 * 1000;
const ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS = 30 * 1000;
const ATTENDANCE_BACKGROUND_JOB_TAB_ID = -1;
const HOME_UPDATE_CHECK_KEY = 'klpf-home-update-check';
const HOME_UPDATE_NOTICE_DISABLED_KEY = 'hideHomeUpdateNotification';
const LATEST_RELEASE_API_URL = 'https://api.github.com/repos/SAYUTIM/KLPF/releases/latest';
const KUPORT_ENTRY_URL = 'https://ku-port.sc.kogakuin.ac.jp/';
const KUPORT_URL_PATTERN = 'https://ku-port.sc.kogakuin.ac.jp/*';
const ATTENDANCE_PARSER_PATH = 'offscreen/attendanceParser.html';
const LMS_HOME_URL_PATTERNS = [
'https://study.ns.kogakuin.ac.jp/lms/homeHoml/*',
'https://study.ns.kogakuin.ac.jp/lms/tpicTpil/doBack*',
'https://study.ns.kogakuin.ac.jp/lms/klmsKlil/doBack*',
];
const KUPORT_TRANSITION_HOSTS = new Set([
'ku-port.sc.kogakuin.ac.jp',
'auth.kogakuin.ac.jp',
'slink.secioss.com',
]);
let creatingAttendanceParser = null;
const startingBackgroundAttendanceTabs = new Set();
let attendanceFetchAbortController = null;
let lastAttendanceRefreshAt = 0;
let homeUpdateNoticeQueue = Promise.resolve();
function getLocalDateKey(date = new Date()) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
async function claimHomeUpdateNotice() {
const noticePreference = await chrome.storage.sync.get(HOME_UPDATE_NOTICE_DISABLED_KEY);
if (noticePreference[HOME_UPDATE_NOTICE_DISABLED_KEY] === true) {
return { status: 'disabled' };
}
const today = getLocalDateKey();
const stored = await chrome.storage.local.get(HOME_UPDATE_CHECK_KEY);
let updateState = stored[HOME_UPDATE_CHECK_KEY] || {};
if (updateState.checkedDate !== today) {
const response = await fetch(LATEST_RELEASE_API_URL, { cache: 'no-store' });
if (!response.ok) throw new Error(`GitHub Release API: ${response.status}`);
const latestRelease = await response.json();
const latestVersion = String(latestRelease.tag_name || '').trim();
const currentVersion = chrome.runtime.getManifest().version;
updateState = {
checkedDate: today,
latestVersion,
updateAvailable: isVersionNewer(latestVersion, currentVersion),
notifiedDate: updateState.notifiedDate || '',
};
await chrome.storage.local.set({ [HOME_UPDATE_CHECK_KEY]: updateState });
}
if (!updateState.updateAvailable || !updateState.latestVersion) {
return { status: 'up-to-date' };
}
if (updateState.notifiedDate === today) {
return { status: 'already-notified' };
}
updateState.notifiedDate = today;
await chrome.storage.local.set({ [HOME_UPDATE_CHECK_KEY]: updateState });
return {
status: 'update-available',
latestVersion: updateState.latestVersion,
};
}
function queueHomeUpdateNoticeClaim() {
const queuedClaim = homeUpdateNoticeQueue.then(claimHomeUpdateNotice);
homeUpdateNoticeQueue = queuedClaim.catch(() => undefined);
return queuedClaim;
}
async function reportAttendanceDebug(stage, details = {}) {
const message = {
type: 'klpf-attendance-debug',
stage,
details,
timestamp: Date.now(),
};
const tabs = await chrome.tabs.query({ url: LMS_HOME_URL_PATTERNS });
await Promise.all(tabs.map(async tab => {
try {
await chrome.tabs.sendMessage(tab.id, message);
} catch {
// 対象タブでcontent scriptが準備中の場合はService Workerのログだけ残す。
}
}));
}
async function getAttendanceFetchJob() {
const stored = await chrome.storage.session.get(ATTENDANCE_FETCH_JOB_KEY);
return stored[ATTENDANCE_FETCH_JOB_KEY] || null;
}
async function clearAttendanceFetchJob() {
await chrome.storage.session.remove(ATTENDANCE_FETCH_JOB_KEY);
}
async function closeCreatedAttendanceContext(job) {
if (!job?.createdByExtension) return;
if (Number.isInteger(job.createdWindowId)) {
try {
await chrome.windows.remove(job.createdWindowId);
return;
} catch {
// ウィンドウが先に閉じられた場合はタブ側の削除も試す。
}
}
await chrome.tabs.remove(job.tabId);
}
async function finishAttendanceFetch(tabId, status) {
const job = await getAttendanceFetchJob();
if (!job || job.tabId !== tabId) return;
if (status === 'completed' && !job.manual) {
await recordAttendanceRefreshCooldown();
}
await clearAttendanceFetchJob();
await chrome.storage.session.set({
[ATTENDANCE_BROWSER_SESSION_KEY]: {
status,
finishedAt: Date.now(),
},
});
if (job.createdByExtension && job.phase !== 'background-fetch') {
try {
await closeCreatedAttendanceContext(job);
} catch (error) {
console.debug('[KLPF] 出席率取得用の一時画面を閉じられませんでした。', error);
}
}
await reportAttendanceDebug('処理終了', { status });
}
async function askKuportTabToCapture(tabId, mode = 'capture') {
try {
return await chrome.tabs.sendMessage(tabId, {
type: mode === 'bootstrap'
? 'klpf-attendance-session-bootstrap'
: mode === 'navigate'
? 'klpf-attendance-auto-fetch'
: 'klpf-attendance-capture-now',
});
} catch {
return null;
}
}
async function ensureAttendanceParser() {
const documentUrl = chrome.runtime.getURL(ATTENDANCE_PARSER_PATH);
const contexts = await chrome.runtime.getContexts({
contextTypes: ['OFFSCREEN_DOCUMENT'],
documentUrls: [documentUrl],
});
if (contexts.length > 0) return;
if (!creatingAttendanceParser) {
creatingAttendanceParser = chrome.offscreen.createDocument({
url: ATTENDANCE_PARSER_PATH,
reasons: ['DOM_PARSER'],
justification: 'Ku-portの出席表HTMLを画面へ表示せず解析するため',
}).finally(() => {
creatingAttendanceParser = null;
});
}
await creatingAttendanceParser;
}
async function parseKuportDocument(type, payload) {
await ensureAttendanceParser();
const response = await chrome.runtime.sendMessage({
target: 'attendance-parser',
type,
...payload,
});
if (!response?.success) {
throw new Error(response?.error || 'Ku-portのHTMLを解析できませんでした。');
}
return response.data;
}
function createFormBody(fields) {
const body = new URLSearchParams();
for (const [name, value] of fields || []) {
if (typeof name === 'string' && typeof value === 'string') body.append(name, value);
}
return body;
}
function assertKuportUrl(value) {
const url = new URL(value);
if (url.protocol !== 'https:' || url.hostname !== 'ku-port.sc.kogakuin.ac.jp') {
throw new Error('Ku-port以外への通信を拒否しました。');
}
return url.href;
}
function throwIfAttendanceFetchAborted(signal) {
if (!signal?.aborted) return;
const error = new Error('Ku-portが別タブで開かれたため取得を中断しました。');
error.name = 'AbortError';
throw error;
}
async function fetchKuportAttendanceInBackground(bootstrap, signal) {
await reportAttendanceDebug('バックグラウンド通信開始');
throwIfAttendanceFetchAborted(signal);
const menuAction = assertKuportUrl(bootstrap.action);
const menuBody = createFormBody(bootstrap.fields);
menuBody.set('menuForm:mainMenu', 'menuForm:mainMenu');
menuBody.set('menuForm:mainMenu_menuid', '7_0_0_0');
const attendancePageResponse = await fetch(menuAction, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
body: menuBody,
redirect: 'follow',
signal,
});
if (!attendancePageResponse.ok) {
throw new Error(`Ku-port画面遷移エラー: ${attendancePageResponse.status}`);
}
await reportAttendanceDebug('出席画面へのJSF遷移成功', {
status: attendancePageResponse.status,
});
throwIfAttendanceFetchAborted(signal);
const attendancePageHtml = await attendancePageResponse.text();
const attendanceForm = await parseKuportDocument('parse-attendance-form', {
html: attendancePageHtml,
baseUrl: attendancePageResponse.url,
});
throwIfAttendanceFetchAborted(signal);
await reportAttendanceDebug('出席フォーム解析成功', {
academicTerm: attendanceForm.academicTerm || '',
});
const attendanceAction = assertKuportUrl(attendanceForm.action);
const displayBody = createFormBody(attendanceForm.fields);
displayBody.set('javax.faces.partial.ajax', 'true');
displayBody.set('javax.faces.source', 'funcForm:btnHyoji');
displayBody.set('javax.faces.partial.execute', '@this');
displayBody.set(
'javax.faces.partial.render',
'funcForm:btnHyoji funcForm:conditionArea funcForm:jugyoKaisuInfo funcForm:jugyoKaisuTbl'
);
displayBody.set('funcForm:btnHyoji', 'funcForm:btnHyoji');
const attendanceResponse = await fetch(attendanceAction, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Faces-Request': 'partial/ajax',
'X-Requested-With': 'XMLHttpRequest',
},
body: displayBody,
redirect: 'follow',
signal,
});
if (!attendanceResponse.ok) {
throw new Error(`Ku-port出席表取得エラー: ${attendanceResponse.status}`);
}
await reportAttendanceDebug('出席表Ajax取得成功', {
status: attendanceResponse.status,
});
throwIfAttendanceFetchAborted(signal);
const attendanceResponseText = await attendanceResponse.text();
const records = await parseKuportDocument('parse-attendance-records', {
html: attendanceResponseText,
});
throwIfAttendanceFetchAborted(signal);
if (!Array.isArray(records) || records.length === 0) {
throw new Error('Ku-portの出席表に科目が見つかりませんでした。');
}
await reportAttendanceDebug('出席表解析成功', {
recordCount: records.length,
numericRateCount: records.filter(record => Number.isFinite(record.rate)).length,
});
throwIfAttendanceFetchAborted(signal);
await chrome.storage.local.set({
[ATTENDANCE_CACHE_KEY]: {
version: ATTENDANCE_CACHE_VERSION,
updatedAt: Date.now(),
academicTerm: attendanceForm.academicTerm || '',
records,
},
});
await reportAttendanceDebug('新しい出席率キャッシュを保存', {
recordCount: records.length,
});
}
async function startBackgroundAttendanceFetch(tabId, bootstrap) {
if (startingBackgroundAttendanceTabs.has(tabId)) return;
startingBackgroundAttendanceTabs.add(tabId);
const abortController = new AbortController();
attendanceFetchAbortController = abortController;
try {
const job = await getAttendanceFetchJob();
if (!job || job.tabId !== tabId || job.phase !== 'login-tab') return;
await chrome.storage.session.set({
[ATTENDANCE_FETCH_JOB_KEY]: {
...job,
phase: 'background-fetch',
},
});
if (job.createdByExtension) {
await reportAttendanceDebug('Ku-portログイン完了・一時画面を閉じます');
await closeCreatedAttendanceContext(job);
}
await fetchKuportAttendanceInBackground(bootstrap, abortController.signal);
await finishAttendanceFetch(tabId, 'completed');
} catch (error) {
if (error.name === 'AbortError') return;
await reportAttendanceDebug('バックグラウンド取得失敗', {
error: error.message,
});
console.error('[KLPF] Ku-portのバックグラウンド取得に失敗しました。', error);
await finishAttendanceFetch(tabId, 'background-fetch-error');
} finally {
if (attendanceFetchAbortController === abortController) {
attendanceFetchAbortController = null;
}
startingBackgroundAttendanceTabs.delete(tabId);
try {
await chrome.offscreen.closeDocument();
} catch {
// offscreen documentが作られる前の失敗は無視する。
}
}
}
async function createAttendanceLoginContext() {
try {
const popupWindow = await chrome.windows.create({
url: 'about:blank',
type: 'popup',
state: 'minimized',
focused: false,
});
const tabs = popupWindow?.tabs?.length
? popupWindow.tabs
: Number.isInteger(popupWindow?.id)
? await chrome.tabs.query({ windowId: popupWindow.id })
: [];
if (tabs[0]?.id !== undefined) {
return {
tab: tabs[0],
createdWindowId: popupWindow.id,
displayMode: 'minimized-window',
};
}
if (Number.isInteger(popupWindow?.id)) await chrome.windows.remove(popupWindow.id);
} catch (error) {
console.debug('[KLPF] 最小化したKu-portログイン画面を作成できませんでした。', error);
}
return {
tab: await chrome.tabs.create({ active: false }),
createdWindowId: null,
displayMode: 'inactive-tab',
};
}
async function prepareAttendanceRefreshJob() {
const currentJob = await getAttendanceFetchJob();
if (!currentJob) return null;
const isExpired = !Number.isFinite(currentJob.startedAt)
|| Date.now() - currentJob.startedAt > ATTENDANCE_FETCH_JOB_TIMEOUT_MS;
if (!isExpired && currentJob.phase === 'background-fetch') {
return { status: 'already-running' };
}
try {
if (!isExpired) {
await chrome.tabs.get(currentJob.tabId);
return { status: 'already-running' };
}
if (currentJob.createdByExtension) await closeCreatedAttendanceContext(currentJob);
} catch {
// 既に閉じられている場合もジョブ情報だけ削除する。
}
await clearAttendanceFetchJob();
return null;
}
async function setAttendanceFetchJob(
tabId,
{ createdByExtension = false, createdWindowId = null, manual = false } = {}
) {
await chrome.storage.session.set({
[ATTENDANCE_BROWSER_SESSION_KEY]: {
status: 'running',
startedAt: Date.now(),
},
[ATTENDANCE_FETCH_JOB_KEY]: {
tabId,
startedAt: Date.now(),
createdByExtension,
createdWindowId,
manual,
phase: 'login-tab',
},
});
}
async function startAttendanceLoginFlow({ abortIfKuportOpen = false, manual = false } = {}) {
const createdContext = await createAttendanceLoginContext();
const tab = createdContext.tab;
await setAttendanceFetchJob(tab.id, {
createdByExtension: true,
createdWindowId: createdContext.createdWindowId,
manual,
});
if (abortIfKuportOpen) {
const existingTabs = await chrome.tabs.query({ url: KUPORT_URL_PATTERN });
if (existingTabs.length > 0) {
await cancelAttendanceFetchForUserKuport(existingTabs[0].id);
return { status: 'kuport-open' };
}
}
await reportAttendanceDebug(
createdContext.displayMode === 'minimized-window'
? 'Ku-port最小化ウィンドウで自動ログイン開始'
: 'Ku-port非アクティブタブで自動ログイン開始'
);
await chrome.tabs.update(tab.id, { url: KUPORT_ENTRY_URL });
return { status: 'started' };
}
async function tryManualRefreshFromBackgroundSession() {
const sessionProbeController = new AbortController();
attendanceFetchAbortController = sessionProbeController;
await setAttendanceFetchJob(ATTENDANCE_BACKGROUND_JOB_TAB_ID, { manual: true });
try {
await reportAttendanceDebug('既存Ku-portセッションをバックグラウンドで確認');
let response = await fetch(KUPORT_ENTRY_URL, {
credentials: 'include',
cache: 'no-store',
redirect: 'follow',
signal: sessionProbeController.signal,
});
let bootstrap = null;
for (let step = 0; step <= 2; step += 1) {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const responseHtml = await response.text();
try {
bootstrap = await parseKuportDocument('parse-menu-bootstrap', {
html: responseHtml,
baseUrl: response.url,
});
break;
} catch (menuError) {
if (step === 2 || new URL(response.url).hostname !== 'ku-port.sc.kogakuin.ac.jp') {
throw menuError;
}
const navigation = await parseKuportDocument('parse-auto-navigation-form', {
html: responseHtml,
baseUrl: response.url,
});
const navigationAction = assertKuportUrl(navigation.action);
response = await fetch(navigationAction, {
method: 'POST',
credentials: 'include',
cache: 'no-store',
redirect: 'follow',
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
body: createFormBody(navigation.fields),
signal: sessionProbeController.signal,
});
}
}
if (!bootstrap) throw new Error('Ku-portのログイン済みセッションを確認できませんでした。');
throwIfAttendanceFetchAborted(sessionProbeController.signal);
if (attendanceFetchAbortController === sessionProbeController) {
attendanceFetchAbortController = null;
}
void startBackgroundAttendanceFetch(ATTENDANCE_BACKGROUND_JOB_TAB_ID, {
status: 'session-ready',
action: bootstrap.action,
fields: bootstrap.fields,
});
await reportAttendanceDebug('既存Ku-portセッションを再利用して手動更新');
return { status: 'started-existing-session' };
} catch (error) {
if (attendanceFetchAbortController === sessionProbeController) {
attendanceFetchAbortController = null;
}
if (error.name === 'AbortError') return { status: 'kuport-open' };
const job = await getAttendanceFetchJob();
if (job?.tabId === ATTENDANCE_BACKGROUND_JOB_TAB_ID) await clearAttendanceFetchJob();
await chrome.storage.session.remove(ATTENDANCE_BROWSER_SESSION_KEY);
await reportAttendanceDebug('既存Ku-portセッションを利用できないため再ログインへ切替', {
error: error.message,
});
try {
await chrome.offscreen.closeDocument();
} catch {
// 解析用ドキュメントが作成されていない場合は無視する。
}
return null;
}
}
async function checkManualRefreshCooldown() {
const requestedAt = Date.now();
const memoryElapsed = requestedAt - lastAttendanceRefreshAt;
if (memoryElapsed < ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS) {
return Math.ceil((ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS - memoryElapsed) / 1000);
}
lastAttendanceRefreshAt = requestedAt;
const stored = await chrome.storage.session.get(ATTENDANCE_MANUAL_REFRESH_KEY);
const lastRequestedAt = stored[ATTENDANCE_MANUAL_REFRESH_KEY]?.requestedAt;
const elapsed = Number.isFinite(lastRequestedAt) ? requestedAt - lastRequestedAt : Infinity;
if (elapsed < ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS) {
lastAttendanceRefreshAt = lastRequestedAt;
return Math.ceil((ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS - elapsed) / 1000);
}
await recordAttendanceRefreshCooldown(requestedAt);
return 0;
}
async function recordAttendanceRefreshCooldown(requestedAt = Date.now()) {
lastAttendanceRefreshAt = requestedAt;
await chrome.storage.session.set({
[ATTENDANCE_MANUAL_REFRESH_KEY]: { requestedAt },
});
}
async function getManualRefreshCooldownRemaining() {
const stored = await chrome.storage.session.get(ATTENDANCE_MANUAL_REFRESH_KEY);
const storedRequestedAt = stored[ATTENDANCE_MANUAL_REFRESH_KEY]?.requestedAt;
const lastRequestedAt = Math.max(
lastAttendanceRefreshAt,
Number.isFinite(storedRequestedAt) ? storedRequestedAt : 0
);
if (!lastRequestedAt) return 0;
return Math.max(
0,
Math.ceil(
(ATTENDANCE_MANUAL_REFRESH_COOLDOWN_MS - (Date.now() - lastRequestedAt)) / 1000
)
);
}
async function requestAttendanceRateRefresh({ manual = false } = {}) {
const attendanceSettings = await chrome.storage.sync.get([
ATTENDANCE_RATE_FEATURE_KEY,
ATTENDANCE_RATE_CONSENT_KEY,
'autoLogin',
]);
if (attendanceSettings[ATTENDANCE_RATE_CONSENT_KEY] !== true) {
return { status: 'consent-required' };
}
if (attendanceSettings[ATTENDANCE_RATE_FEATURE_KEY] !== true) {
return { status: 'feature-disabled' };
}
if (attendanceSettings.autoLogin === false) {
return { status: 'auto-login-disabled' };
}
if (!manual) {
const browserSession = await chrome.storage.session.get(ATTENDANCE_BROWSER_SESSION_KEY);
if (browserSession[ATTENDANCE_BROWSER_SESSION_KEY]) {
const previousStatus = browserSession[ATTENDANCE_BROWSER_SESSION_KEY].status;
await reportAttendanceDebug('このブラウザ起動中は取得済み', {
status: previousStatus,
});
if (previousStatus === 'running') return { status: 'already-running' };
return {
status: 'browser-session-already-checked',
previousStatus,
};
}
}
const runningStatus = await prepareAttendanceRefreshJob();
if (runningStatus) return runningStatus;
if (manual) {
const openKuportTabs = await chrome.tabs.query({ url: KUPORT_URL_PATTERN });
if (openKuportTabs.length > 0) {
await reportAttendanceDebug('Ku-portが開いているため手動更新を中止');
return { status: 'kuport-open' };
}
const remainingSeconds = await checkManualRefreshCooldown();
if (remainingSeconds > 0) return { status: 'cooldown', remainingSeconds };
await reportAttendanceDebug('出席状況の手動更新を開始');
const backgroundResult = await tryManualRefreshFromBackgroundSession();
if (backgroundResult) return backgroundResult;
const tabsOpenedDuringProbe = await chrome.tabs.query({ url: KUPORT_URL_PATTERN });
if (tabsOpenedDuringProbe.length > 0) {
await reportAttendanceDebug('Ku-portが開かれたため再ログインを中止');
return { status: 'kuport-open' };
}
return startAttendanceLoginFlow({ abortIfKuportOpen: true, manual: true });
}
const existingTabs = await chrome.tabs.query({ url: KUPORT_URL_PATTERN });
if (existingTabs.length > 0) {
await chrome.storage.session.set({
[ATTENDANCE_BROWSER_SESSION_KEY]: {
status: 'skipped-kuport-already-open',
finishedAt: Date.now(),
},
});
await reportAttendanceDebug('Ku-portが既に開いているため自動取得中止');
return { status: 'kuport-already-open' };
}
return startAttendanceLoginFlow();
}
async function cancelAttendanceFetchForUserKuport(tabId) {
const job = await getAttendanceFetchJob();
if (!job || job.tabId === tabId) return false;
attendanceFetchAbortController?.abort();
await clearAttendanceFetchJob();
if (job.createdByExtension && job.phase !== 'background-fetch') {
try {
await closeCreatedAttendanceContext(job);
} catch {
// ログイン用画面が既に閉じられている場合は無視する。
}
}
await chrome.storage.session.set({
[ATTENDANCE_BROWSER_SESSION_KEY]: {
status: 'cancelled-kuport-opened',
finishedAt: Date.now(),
},
});
await reportAttendanceDebug('Ku-portが別タブで開かれたため出席状況の取得中止');
return true;
}
async function continueAttendanceFetch(tabId, changeInfo, tab) {
const job = await getAttendanceFetchJob();
if (!job || job.tabId !== tabId) return;
if (changeInfo.status !== 'complete') return;
const url = changeInfo.url || tab.url || '';
let hostname = '';
try {
hostname = new URL(url).hostname;
} catch {
// URLが確定するまで待つ。
}
if (!hostname) return;
if (hostname && !KUPORT_TRANSITION_HOSTS.has(hostname)) {
await finishAttendanceFetch(tabId, 'unexpected-navigation');
return;
}
if (hostname && hostname !== 'ku-port.sc.kogakuin.ac.jp') return;
if (tab.title === 'Error Page') {
await finishAttendanceFetch(tabId, 'kuport-error');
return;
}
const response = await askKuportTabToCapture(
tabId,
job.createdByExtension ? 'bootstrap' : 'navigate'
);
if (response?.status === 'session-ready' && job.createdByExtension) {
await reportAttendanceDebug('Ku-portホームからJSFセッション情報を取得');
void startBackgroundAttendanceFetch(tabId, response);
return;
}
if (response?.status === 'captured') {
await finishAttendanceFetch(tabId, 'completed');
} else if (response?.status === 'menu-not-ready' && job.createdByExtension) {
// 自動ログインの次の画面へ遷移するまで待つ。
return;
} else if (!['navigating', 'waiting-for-table'].includes(response?.status)) {
if (startingBackgroundAttendanceTabs.has(tabId)) return;
const latestJob = await getAttendanceFetchJob();
if (latestJob?.tabId === tabId && latestJob.phase === 'background-fetch') return;
await finishAttendanceFetch(tabId, response?.status || 'content-script-unavailable');
}
}
async function enableAutomaticSubjectFilter() {
const result = await chrome.storage.local.get(SUBJECT_FILTER_STORAGE_KEY);
let settings = {};
try {
const parsed = result[SUBJECT_FILTER_STORAGE_KEY]
? JSON.parse(result[SUBJECT_FILTER_STORAGE_KEY])
: {};
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
settings = parsed;
}
} catch (error) {
console.debug('[KLPF] 既存の講義フィルター設定を読み込めなかったため初期化します。', error);
}
settings.isAutoActive = true;
await chrome.storage.local.set({
[SUBJECT_FILTER_STORAGE_KEY]: JSON.stringify(settings),
});
}
function isAllowedWebhookUrl(value) {
if (typeof value !== 'string') return false;
try {
const url = new URL(value);
return url.protocol === 'https:'
&& !!url.hostname
&& !url.username
&& !url.password;
} catch {
return false;
}
}
/**
* コンテンツスクリプトを登録する。
* @param {ContentScriptConfig} config - 登録するスクリプトの設定。
*/
async function registerContentScript(config) {
try {
const registration = {
id: config.id,
js: config.js,
matches: config.matches,
runAt: config.runAt,
};
if (Array.isArray(config.css) && config.css.length > 0) {
registration.css = config.css;
}
await chrome.scripting.registerContentScripts([registration]);
console.log(`[KLPF] スクリプト登録: ${config.id}`);
} catch (error) {
console.error(`[KLPF] スクリプト登録失敗: ${config.id}`, error);
}
}
/**
* コンテンツスクリプトの登録を解除する。
* @param {string} scriptId - 解除するスクリプトのID。
*/
async function unregisterContentScript(scriptId) {
try {
const scripts = await chrome.scripting.getRegisteredContentScripts({ ids: [scriptId] });
if (scripts.length > 0) {
await chrome.scripting.unregisterContentScripts({ ids: [scriptId] });
console.log(`[KLPF] スクリプト解除: ${scriptId}`);
}
} catch (error) {
console.error(`[KLPF] スクリプト解除失敗: ${scriptId}`, error);
}
}
async function replaceContentScriptRegistration(config) {
await unregisterContentScript(config.id);
await registerContentScript(config);
}
async function applyAutoAttendDependency(isEnabled) {
const meetConfig = CONTENT_SCRIPTS_CONFIG.find(config => config.storageKey === 'autoMeet');
if (!meetConfig) return;
if (isEnabled) {
await replaceContentScriptRegistration(meetConfig);
} else {
await unregisterContentScript(meetConfig.id);
}
}
/**
* 全機能の状態をストレージから読み込み、必要に応じてスクリプトを登録/解除する。
*/
async function initializeScripts() {
console.log('[KLPF] 拡張機能の初期化...');
const storageKeys = [
...CONTENT_SCRIPTS_CONFIG.map(config => config.storageKey),
ATTENDANCE_RATE_CONSENT_KEY,
];
const result = await chrome.storage.sync.get(storageKeys);
if (chrome.runtime.lastError) {
console.error('[KLPF] ストレージ読み込み失敗:', chrome.runtime.lastError);
return;
}
const hasAttendanceConsent = result[ATTENDANCE_RATE_CONSENT_KEY] === true;
if (!hasAttendanceConsent && result[ATTENDANCE_RATE_FEATURE_KEY] === true) {
result[ATTENDANCE_RATE_FEATURE_KEY] = false;
await chrome.storage.sync.set({ [ATTENDANCE_RATE_FEATURE_KEY]: false });
}
for (const config of CONTENT_SCRIPTS_CONFIG) {
const isEnabled = result[config.storageKey] !== undefined
? result[config.storageKey]
: !!config.enabledByDefault;
await unregisterContentScript(config.id); // 念のため既存のスクリプトを解除
if (isEnabled) {
await registerContentScript(config);
// 「自動出席」が有効な場合、「Meet自動参加」も有効にする依存関係を処理
if (config.storageKey === 'autoAttend') {
await applyAutoAttendDependency(true);
}
}
}
}
// --- イベントリスナーの登録 ---
/**
* 拡張機能のインストールまたは更新時に実行される。
*/
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('[KLPF] 拡張機能がインストールされました。');
const defaults = {};
const defaultOptionsOrder = [];
CONTENT_SCRIPTS_CONFIG.forEach(config => {
if (config.enabledByDefault) {
defaults[config.storageKey] = true;
if (config.optionsPanelId) {
defaultOptionsOrder.push(config.optionsPanelId);
}
}
});
defaults.optionsOrder = defaultOptionsOrder;
chrome.storage.sync.set(defaults, () => {
console.log('[KLPF] デフォルト設定を保存しました。');
// initializeScripts(); // onChangedが処理するため、インストール時は不要
});
} else {
initializeScripts();
}
// コンテキストメニューを作成
chrome.contextMenus.create({
id: CONTEXT_MENU_ID,
title: "[KLPF] 設定を開く",
contexts: ["page"],
});
});
/**
* ストレージの変更を監視する。
*/
chrome.storage.onChanged.addListener(async (changes, area) => {
if (area !== 'sync') return;
if (changes[ATTENDANCE_RATE_CONSENT_KEY]
&& changes[ATTENDANCE_RATE_CONSENT_KEY].newValue !== true) {
const attendanceSetting = await chrome.storage.sync.get(ATTENDANCE_RATE_FEATURE_KEY);
if (attendanceSetting[ATTENDANCE_RATE_FEATURE_KEY] === true) {
await chrome.storage.sync.set({ [ATTENDANCE_RATE_FEATURE_KEY]: false });
}
await unregisterContentScript('AttendanceRateDisplay');
}
for (const [key, { newValue }] of Object.entries(changes)) {
const config = CONTENT_SCRIPTS_CONFIG.find(c => c.storageKey === key);
if (!config) continue;
if (newValue) {
if (key === ATTENDANCE_RATE_FEATURE_KEY) {
const consent = await chrome.storage.sync.get(ATTENDANCE_RATE_CONSENT_KEY);
if (consent[ATTENDANCE_RATE_CONSENT_KEY] !== true) {
await chrome.storage.sync.set({ [ATTENDANCE_RATE_FEATURE_KEY]: false });
await unregisterContentScript(config.id);
continue;
}
}
if (key === 'searchSubject') {
await enableAutomaticSubjectFilter();
}
// 重複エラーを防ぐため、登録前に必ず解除する
await replaceContentScriptRegistration(config);
// 「自動出席」が有効な場合、「Meet自動参加」も有効にする依存関係を処理
if (key === 'autoAttend') {
await applyAutoAttendDependency(true);
}
} else {
// 機能が無効になった場合、スクリプトを解除する
await unregisterContentScript(config.id);
// 「自動出席」が無効な場合、「Meetミュート参加」も解除する
if (key === 'autoAttend') {
await applyAutoAttendDependency(false);
}
}
}
});
/**
* コンテキストメニューがクリックされたときに実行される。
*/
chrome.contextMenus.onClicked.addListener((info) => {
if (info.menuItemId === CONTEXT_MENU_ID) {
chrome.tabs.create({ url: "setting/options.html" });
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
void (async () => {
const url = changeInfo.url || tab.url || '';
try {
if (new URL(url).hostname === 'ku-port.sc.kogakuin.ac.jp') {
const cancelled = await cancelAttendanceFetchForUserKuport(tabId);
if (cancelled) return;
}
} catch {
// URLがまだ確定していない更新は通常の継続判定へ渡す。
}
await continueAttendanceFetch(tabId, changeInfo, tab);
})().catch(error => {
console.error('[KLPF] Ku-port出席率取得の継続に失敗しました。', error);
});
});
chrome.tabs.onRemoved.addListener((tabId) => {
void (async () => {
const job = await getAttendanceFetchJob();
if (job?.tabId !== tabId || job.phase === 'background-fetch') return;
await clearAttendanceFetchJob();
await chrome.storage.session.set({
[ATTENDANCE_BROWSER_SESSION_KEY]: {
status: 'login-tab-closed',
finishedAt: Date.now(),
},
});
})();
});
/**
* コンテンツスクリプトやポップアップからのメッセージを受信する。
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "openTab") {
chrome.tabs.create({ url: message.url });
return;
}
if (message.type === 'send-homework') {
(async () => {
try {
const result = await chrome.storage.sync.get(["gaswebhookurl", "gasWebhook"]);
if (result.gasWebhook !== true) {
throw new Error('課題通知が無効です。');
}
if (!isAllowedWebhookUrl(result.gaswebhookurl)) {
throw new Error('Webhook URLが未設定または許可対象外です。');
}
const response = await fetch(result.gaswebhookurl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message.data)
});
if (!response.ok) {
throw new Error(`HTTPエラー ステータス: ${response.status}`);
}
console.log('[KLPF] 課題データをWebhookに送信しました。');
sendResponse({ success: true });
} catch (error) {
console.error('[KLPF] Webhookへのデータ送信に失敗しました:', error);
sendResponse({ success: false, error: error.message });
}