fix: automate graduate timetable read and refine blue glass Dock
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
@@ -10,6 +9,7 @@ import 'school_navigation_policy.dart';
|
||||
import 'school_page_adapter.dart';
|
||||
import 'school_exam_adapter.dart';
|
||||
import 'school_login_flow.dart';
|
||||
import 'school_page_probe.dart';
|
||||
|
||||
enum SchoolConnectionState {
|
||||
signedOut,
|
||||
@@ -31,10 +31,13 @@ class SchoolSession extends ChangeNotifier {
|
||||
? null
|
||||
: SchoolNavigationPolicy.secureIdentityRedirect(uri);
|
||||
if (upgrade != null) {
|
||||
final generation = _pageGeneration;
|
||||
try {
|
||||
await web.loadRequest(upgrade);
|
||||
} catch (_) {
|
||||
_failure('学校身份认证暂时无法打开,请重试。');
|
||||
if (generation == _pageGeneration) {
|
||||
_failure('学校身份认证暂时无法打开,请重试。');
|
||||
}
|
||||
}
|
||||
return NavigationDecision.prevent;
|
||||
}
|
||||
@@ -54,11 +57,13 @@ class SchoolSession extends ChangeNotifier {
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
onPageStarted: (_) {
|
||||
_poll?.cancel();
|
||||
_manualRead = false;
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
if (!hasOpened) return;
|
||||
if (!hasOpened || _routeClosed) return;
|
||||
_loading = true;
|
||||
notifyListeners();
|
||||
},
|
||||
@@ -90,6 +95,11 @@ class SchoolSession extends ChangeNotifier {
|
||||
final _reader = SchoolReader(SchoolPageAdapter());
|
||||
final _flow = SchoolLoginFlow();
|
||||
Timer? _timeout;
|
||||
Timer? _poll;
|
||||
bool _readingActive = false, _manualRead = false;
|
||||
bool _routeClosed = false;
|
||||
int _pollsLeft = 0;
|
||||
int? _probeGeneration;
|
||||
SchoolConnectionState connectionState = SchoolConnectionState.signedOut;
|
||||
bool _loading = false,
|
||||
hasOpened = false,
|
||||
@@ -137,8 +147,11 @@ class SchoolSession extends ChangeNotifier {
|
||||
if (closing != null) await closing;
|
||||
if (serial != _openSerial) return;
|
||||
_timeout?.cancel();
|
||||
_poll?.cancel();
|
||||
_manualRead = false;
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
final generation = _pageGeneration;
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
requestingExams = exams;
|
||||
@@ -155,7 +168,9 @@ class SchoolSession extends ChangeNotifier {
|
||||
try {
|
||||
await web.loadRequest(connected ? _target : authUrl);
|
||||
} catch (_) {
|
||||
if (serial == _openSerial) _failure('学校入口无法打开,请检查校园网络后重试。');
|
||||
if (serial == _openSerial && generation == _pageGeneration) {
|
||||
_failure('学校入口无法打开,请检查校园网络后重试。');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,32 +184,67 @@ class SchoolSession extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void _failure(String text) {
|
||||
if (!hasOpened) return;
|
||||
if (!hasOpened || _routeClosed) return;
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
_timeout?.cancel();
|
||||
_poll?.cancel();
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
finding = false;
|
||||
_loading = false;
|
||||
message = text;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<String> _html() async {
|
||||
final raw = await web.runJavaScriptReturningResult(
|
||||
'JSON.stringify(document.documentElement.outerHTML)',
|
||||
);
|
||||
final value = raw is String ? raw : raw.toString();
|
||||
try {
|
||||
final decoded = jsonDecode(value);
|
||||
return decoded is String ? decoded : value;
|
||||
} catch (_) {
|
||||
return value;
|
||||
/// Poll only while the authentication screen is open. Navigation callbacks
|
||||
/// still read immediately; polling handles scripts and frames ready later.
|
||||
void setReadingActive(bool active) {
|
||||
if (_readingActive == active && _routeClosed == !active) return;
|
||||
_readingActive = active;
|
||||
_routeClosed = !active;
|
||||
_poll?.cancel();
|
||||
if (active && hasOpened) {
|
||||
web.currentUrl().then((url) {
|
||||
if (_readingActive && url != null) _loaded(url);
|
||||
});
|
||||
} else if (!active) {
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
_timeout?.cancel();
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
finding = false;
|
||||
_loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool _truth(Object value) => value == true || value.toString() == 'true';
|
||||
Future<void> _loaded(String url) async {
|
||||
if (!hasOpened) return;
|
||||
void _scheduleProbe(String url, int generation) {
|
||||
if (!_readingActive ||
|
||||
!hasOpened ||
|
||||
generation != _pageGeneration ||
|
||||
pendingDraft != null ||
|
||||
pendingExams != null) {
|
||||
return;
|
||||
}
|
||||
_poll?.cancel();
|
||||
if (_pollsLeft-- <= 0) {
|
||||
if (finding) _failure('查找超时,请检查学校页面后重试。已有数据保持不变。');
|
||||
return;
|
||||
}
|
||||
_poll = Timer(const Duration(milliseconds: 750), () {
|
||||
_loaded(url, resetPolling: false);
|
||||
});
|
||||
}
|
||||
|
||||
Future<SchoolPageEvidence> _evidence(Uri uri) async =>
|
||||
SchoolPageEvidence.decode(
|
||||
await web.runJavaScriptReturningResult(schoolPageProbeScript),
|
||||
uri,
|
||||
);
|
||||
|
||||
Future<void> _loaded(String url, {bool resetPolling = true}) async {
|
||||
if (!hasOpened || _routeClosed) return;
|
||||
final generation = _pageGeneration;
|
||||
final uri = Uri.tryParse(url);
|
||||
final current = await web.currentUrl();
|
||||
@@ -206,6 +256,7 @@ class SchoolSession extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
_loading = false;
|
||||
if (pendingDraft != null || pendingExams != null) return;
|
||||
if (uri.host == 'idas.uestc.edu.cn') {
|
||||
_timeout?.cancel();
|
||||
finding = false;
|
||||
@@ -215,51 +266,61 @@ class SchoolSession extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
if (!SchoolNavigationPolicy.isGraduatePage(uri) ||
|
||||
_probeGeneration == generation) {
|
||||
return;
|
||||
}
|
||||
if (resetPolling) _pollsLeft = 60;
|
||||
_probeGeneration = generation;
|
||||
final targetPage = requestingExams
|
||||
? SchoolNavigationPolicy.isExam(uri)
|
||||
: SchoolNavigationPolicy.isTimetable(uri);
|
||||
if (targetPage) {
|
||||
if (targetPage || _manualRead) {
|
||||
finding = true;
|
||||
message = _findingMessage;
|
||||
notifyListeners();
|
||||
try {
|
||||
if (requestingExams) {
|
||||
final text = await _html();
|
||||
if (!hasOpened || generation != _pageGeneration) return;
|
||||
final draft = SchoolExamAdapter.extract(text);
|
||||
pendingExams = draft;
|
||||
} else {
|
||||
final draft = await readTimetable();
|
||||
if (!hasOpened || generation != _pageGeneration) return;
|
||||
pendingDraft = draft;
|
||||
}
|
||||
_timeout?.cancel();
|
||||
connectionState = SchoolConnectionState.connected;
|
||||
message = '读取成功,正在保存…';
|
||||
} catch (_) {
|
||||
if (!hasOpened || generation != _pageGeneration) return;
|
||||
_failure(
|
||||
'没有读取到完整${requestingExams ? '考试信息' : '课表'},请检查学校页面后重试。已有数据保持不变。',
|
||||
);
|
||||
if (_timeout?.isActive != true) _armTimeout();
|
||||
}
|
||||
try {
|
||||
final evidence = await _evidence(uri);
|
||||
if (!hasOpened ||
|
||||
generation != _pageGeneration ||
|
||||
await web.currentUrl() != url) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// A logout link is evidence of a graduate-system session, unlike simply
|
||||
// returning to a callback URL or the identity administration dashboard.
|
||||
bool authenticated = false;
|
||||
try {
|
||||
authenticated = _truth(
|
||||
await web.runJavaScriptReturningResult("""(() => {
|
||||
return Array.from(document.querySelectorAll('a[href]')).some(a => {
|
||||
try { const u = new URL(a.href, location.href); return u.hostname === 'yjsjy.uestc.edu.cn' && u.pathname === '/pyxx/home/exit'; } catch (_) { return false; }
|
||||
});
|
||||
})()"""),
|
||||
);
|
||||
} catch (_) {
|
||||
/* Keep the school page available for manual recovery. */
|
||||
for (final page in evidence.pages) {
|
||||
if (page.kind != (requestingExams ? 'exams' : 'timetable')) continue;
|
||||
try {
|
||||
if (requestingExams) {
|
||||
final draft = SchoolExamAdapter.extract(page.html);
|
||||
if (generation != _pageGeneration) return;
|
||||
pendingExams = draft;
|
||||
} else {
|
||||
// The probe validates the live document's origin. The strict parser
|
||||
// gets only its recognized fragment with the canonical table path.
|
||||
final draft = await _reader.read(
|
||||
timetableUrl,
|
||||
() async => page.html,
|
||||
);
|
||||
if (!hasOpened || generation != _pageGeneration) return;
|
||||
pendingDraft = draft;
|
||||
}
|
||||
_timeout?.cancel();
|
||||
_poll?.cancel();
|
||||
connectionState = SchoolConnectionState.connected;
|
||||
finding = true;
|
||||
message = '读取成功,正在保存…';
|
||||
notifyListeners();
|
||||
return;
|
||||
} catch (_) {
|
||||
if (generation != _pageGeneration) return;
|
||||
// A recognized table can still be incomplete during asynchronous load.
|
||||
}
|
||||
}
|
||||
if (!hasOpened || generation != _pageGeneration) return;
|
||||
final next = _flow.onLoaded(uri, authenticated: authenticated);
|
||||
final next = _manualRead
|
||||
? null
|
||||
: _flow.onLoaded(uri, authenticated: evidence.authenticated);
|
||||
if (next != null) {
|
||||
finding = true;
|
||||
_loading = true;
|
||||
@@ -269,20 +330,54 @@ class SchoolSession extends ChangeNotifier {
|
||||
try {
|
||||
await web.loadRequest(next);
|
||||
} catch (_) {
|
||||
_failure('课表查询页面暂时无法打开,请重试。');
|
||||
if (generation == _pageGeneration) {
|
||||
_failure('课表查询页面暂时无法打开,请重试。');
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
finding = false;
|
||||
message = '请在学校网页完成认证;进入研究生系统后会自动读取。';
|
||||
if (!finding) message = '请在学校网页完成认证;进入研究生系统后会自动读取。';
|
||||
} catch (_) {
|
||||
// JavaScript and frame access can fail briefly while the page is changing.
|
||||
} finally {
|
||||
if (_probeGeneration == generation) _probeGeneration = null;
|
||||
_scheduleProbe(url, generation);
|
||||
}
|
||||
notifyListeners();
|
||||
if (hasOpened && generation == _pageGeneration) notifyListeners();
|
||||
}
|
||||
|
||||
Future<ImportDraft> readTimetable() async {
|
||||
final generation = _pageGeneration;
|
||||
final uri = Uri.tryParse(await web.currentUrl() ?? '');
|
||||
if (uri == null) throw UnsupportedSchoolLayout();
|
||||
return _reader.read(uri, _html);
|
||||
if (uri == null || !SchoolNavigationPolicy.isGraduatePage(uri)) {
|
||||
throw UnsupportedSchoolLayout();
|
||||
}
|
||||
final evidence = await _evidence(uri);
|
||||
if (generation != _pageGeneration ||
|
||||
await web.currentUrl() != uri.toString()) {
|
||||
throw UnsupportedSchoolLayout();
|
||||
}
|
||||
for (final page in evidence.pages) {
|
||||
if (page.kind != 'timetable') continue;
|
||||
try {
|
||||
return await _reader.read(timetableUrl, () async => page.html);
|
||||
} on UnsupportedSchoolLayout {
|
||||
if (generation != _pageGeneration) rethrow;
|
||||
}
|
||||
}
|
||||
throw UnsupportedSchoolLayout();
|
||||
}
|
||||
|
||||
Future<void> readCurrentPage() async {
|
||||
_poll?.cancel();
|
||||
_timeout?.cancel();
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
_manualRead = true;
|
||||
final url = await web.currentUrl();
|
||||
if (url != null) await _loaded(url);
|
||||
}
|
||||
|
||||
bool saved({
|
||||
@@ -319,6 +414,7 @@ class SchoolSession extends ChangeNotifier {
|
||||
_reader.invalidate();
|
||||
_flow.reset();
|
||||
_timeout?.cancel();
|
||||
_poll?.cancel();
|
||||
hasOpened = false;
|
||||
pendingDraft = null;
|
||||
pendingExams = null;
|
||||
@@ -343,7 +439,11 @@ class SchoolSession extends ChangeNotifier {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
hasOpened = false;
|
||||
_pageGeneration++;
|
||||
_reader.invalidate();
|
||||
_timeout?.cancel();
|
||||
_poll?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user