Skip to content

Commit 3a9addc

Browse files
fix: 修复Linux webkit环境下错误使用localstorage使得无法添加作业 和 崩溃窗口弹出后整个app冻结的bug
1 parent 5f2b503 commit 3a9addc

7 files changed

Lines changed: 96 additions & 15 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ jobs:
151151
echo "Cannot detect executable in ${PKG}/opt/${APP_NAME}" >&2
152152
exit 1
153153
fi
154+
chmod +x "${PKG}/opt/${APP_NAME}/${BIN_NAME}"
154155
155156
cat > "${PKG}/usr/bin/${APP_NAME}" <<LAUNCHEOF
156157
#!/bin/bash

__pycache__/build.cpython-313.pyc

-16.2 KB
Binary file not shown.

__pycache__/main.cpython-313.pyc

-61.1 KB
Binary file not shown.

__pycache__/main.cpython-38.pyc

-40.3 KB
Binary file not shown.

htmls/settingspage/settingswindow.html

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,47 @@
396396

397397
let activeTab = 'mainset';
398398
const contentFrame = document.getElementById('contentFrame');
399+
const fallbackStorage = Object.create(null);
399400
const contentBody = document.getElementById('contentBody');
400401
const searchInput = document.getElementById('settingsSearch');
401402
const searchHint = document.getElementById('searchHint');
402403
const sidebarToggleBtn = document.getElementById('sidebarToggleBtn');
403404

405+
function storageGet(key) {
406+
try {
407+
if (typeof localStorage !== 'undefined' && localStorage !== null) {
408+
return localStorage.getItem(key);
409+
}
410+
} catch (e) {
411+
console.warn('localStorage.getItem 不可用,使用内存存储:', e);
412+
}
413+
return Object.prototype.hasOwnProperty.call(fallbackStorage, key) ? fallbackStorage[key] : null;
414+
}
415+
416+
function storageSet(key, value) {
417+
const normalizedValue = String(value);
418+
try {
419+
if (typeof localStorage !== 'undefined' && localStorage !== null) {
420+
localStorage.setItem(key, normalizedValue);
421+
return;
422+
}
423+
} catch (e) {
424+
console.warn('localStorage.setItem 不可用,使用内存存储:', e);
425+
}
426+
fallbackStorage[key] = normalizedValue;
427+
}
428+
404429
function markLoading(loading) {
405430
contentBody.classList.toggle('loading', loading);
406431
}
407432

433+
function bindFrameLoad(frame) {
434+
frame.addEventListener('load', () => {
435+
markLoading(false);
436+
applySearchToFrame(searchInput.value.trim());
437+
});
438+
}
439+
408440
function switchTab(tabName) {
409441
const tab = TABS[tabName] ? tabName : 'mainset';
410442
activeTab = tab;
@@ -414,7 +446,7 @@
414446
});
415447

416448
document.getElementById('pageTitle').textContent = TABS[tab].title;
417-
localStorage.setItem(TAB_STORAGE_KEY, tab);
449+
storageSet(TAB_STORAGE_KEY, tab);
418450
window.location.hash = tab;
419451

420452
if (!contentFrame.src.includes(TABS[tab].src)) {
@@ -463,7 +495,7 @@
463495
function setSidebarCollapsed(collapsed) {
464496
document.body.classList.toggle('sidebar-collapsed', collapsed);
465497
sidebarToggleBtn.title = collapsed ? '展开导航栏' : '收缩导航栏';
466-
localStorage.setItem(SIDEBAR_COLLAPSED_KEY, collapsed ? '1' : '0');
498+
storageSet(SIDEBAR_COLLAPSED_KEY, collapsed ? '1' : '0');
467499
}
468500

469501
function toggleSidebar() {
@@ -472,21 +504,18 @@
472504
}
473505

474506
function initSidebarState() {
475-
const collapsed = localStorage.getItem(SIDEBAR_COLLAPSED_KEY) === '1';
507+
const collapsed = storageGet(SIDEBAR_COLLAPSED_KEY) === '1';
476508
setSidebarCollapsed(collapsed);
477509
}
478510

479511
function initTab() {
480512
const hashTab = (window.location.hash || '').replace('#', '').trim();
481-
const storedTab = localStorage.getItem(TAB_STORAGE_KEY);
513+
const storedTab = storageGet(TAB_STORAGE_KEY);
482514
const initialTab = TABS[hashTab] ? hashTab : (TABS[storedTab] ? storedTab : 'mainset');
483515
switchTab(initialTab);
484516
}
485517

486-
contentFrame.addEventListener('load', () => {
487-
markLoading(false);
488-
applySearchToFrame(searchInput.value.trim());
489-
});
518+
bindFrameLoad(contentFrame);
490519

491520
searchInput.addEventListener('input', handleSearchInput);
492521
sidebarToggleBtn.addEventListener('click', toggleSidebar);

index.html

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,31 @@ <h1 id="timeDisplay">Wow 伙伴!</h1>
832832
}
833833

834834
// 作业数据管理
835+
const fallbackStorage = Object.create(null);
836+
function storageGet(key) {
837+
try {
838+
if (typeof localStorage !== 'undefined' && localStorage !== null) {
839+
return localStorage.getItem(key);
840+
}
841+
} catch (e) {
842+
console.warn('localStorage.getItem 不可用,使用内存存储:', e);
843+
}
844+
return Object.prototype.hasOwnProperty.call(fallbackStorage, key) ? fallbackStorage[key] : null;
845+
}
846+
847+
function storageSet(key, value) {
848+
const normalizedValue = String(value);
849+
try {
850+
if (typeof localStorage !== 'undefined' && localStorage !== null) {
851+
localStorage.setItem(key, normalizedValue);
852+
return;
853+
}
854+
} catch (e) {
855+
console.warn('localStorage.setItem 不可用,使用内存存储:', e);
856+
}
857+
fallbackStorage[key] = normalizedValue;
858+
}
859+
835860
// 作业数据缓存
836861
let homeworkCache = null;
837862

@@ -843,15 +868,15 @@ <h1 id="timeDisplay">Wow 伙伴!</h1>
843868
if (result.success) {
844869
homeworkCache = result.data || [];
845870
// 同步到 localStorage
846-
localStorage.setItem('homeworkList', JSON.stringify(homeworkCache));
871+
storageSet('homeworkList', JSON.stringify(homeworkCache));
847872
return homeworkCache;
848873
}
849874
}
850875
} catch (error) {
851876
console.error('从服务器加载作业失败:', error);
852877
}
853878
// 如果服务器加载失败,从 localStorage 加载
854-
const data = localStorage.getItem('homeworkList');
879+
const data = storageGet('homeworkList');
855880
homeworkCache = data ? JSON.parse(data) : [];
856881
return homeworkCache;
857882
}
@@ -862,7 +887,7 @@ <h1 id="timeDisplay">Wow 伙伴!</h1>
862887
return homeworkCache;
863888
}
864889
// 否则从 localStorage 加载
865-
const data = localStorage.getItem('homeworkList');
890+
const data = storageGet('homeworkList');
866891
return data ? JSON.parse(data) : [];
867892
}
868893

@@ -889,14 +914,14 @@ <h1 id="timeDisplay">Wow 伙伴!</h1>
889914
list.push(homework);
890915
}
891916

892-
localStorage.setItem('homeworkList', JSON.stringify(list));
917+
storageSet('homeworkList', JSON.stringify(list));
893918
homeworkCache = list;
894919
}
895920

896921
function deleteHomework(id) {
897922
const list = getHomeworkList();
898923
const newList = list.filter(h => h.id !== id);
899-
localStorage.setItem('homeworkList', JSON.stringify(newList));
924+
storageSet('homeworkList', JSON.stringify(newList));
900925
homeworkCache = newList;
901926
syncHomeworkDataToServer();
902927
renderHomeworkCards();

main.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
}
6161

6262

63+
def is_wsl_environment():
64+
"""检测是否运行在 WSL 环境"""
65+
release = platform.release().lower()
66+
return (
67+
'WSL_DISTRO_NAME' in os.environ
68+
or 'microsoft' in release
69+
or 'wsl' in release
70+
)
71+
72+
6373
def get_data_dir():
6474
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
6575

@@ -1521,6 +1531,15 @@ def saveHomeworkToAutoSave(self, homework_list):
15211531
except Exception as e:
15221532
log(f"读取启动设置失败: {str(e)}", "error")
15231533

1534+
# WSLg 下通常没有可用托盘,禁用托盘相关逻辑以避免初始化异常导致崩溃窗口
1535+
tray_enabled = True
1536+
if system == 'Linux' and is_wsl_environment():
1537+
tray_enabled = False
1538+
log("检测到 WSL 环境:禁用系统托盘", "warning")
1539+
if start_minimized:
1540+
log("WSL 环境下托盘不可用:忽略启动最小化设置", "warning")
1541+
start_minimized = False
1542+
15241543
# 根据屏幕大小自适应主窗口尺寸,避免高缩放下超出可视区域被系统强制铺满
15251544
base_width, base_height = 2296, 1136
15261545
screen_width, screen_height = get_screen_size()
@@ -1556,15 +1575,22 @@ def saveHomeworkToAutoSave(self, homework_list):
15561575
main_window.hide()
15571576

15581577
# 在主线程设置托盘图标(必须在start之前)
1559-
setup_tray_icon(main_window)
1578+
if tray_enabled:
1579+
try:
1580+
setup_tray_icon(main_window)
1581+
except Exception as e:
1582+
tray_icon = None
1583+
log(f"系统托盘初始化失败,继续无托盘运行: {str(e)}", "warning")
15601584

15611585
# 注册拖拽区域
15621586
def on_loaded():
15631587
log("注册拖拽区域", "info")
15641588
# 仅允许在html标签内拖拽
15651589
main_window.evaluate_js("""
15661590
document.querySelector('html').addEventListener('mousedown', function(e) {
1567-
window.dragStart();
1591+
if (typeof window.dragStart === 'function') {
1592+
window.dragStart();
1593+
}
15681594
});
15691595
""")
15701596

0 commit comments

Comments
 (0)