@@ -62,41 +62,49 @@ function showNotification(message, type) {
6262
6363// 页面加载时恢复顺序
6464function restoreOrder ( storageKey , elementClass ) {
65- var savedOrder = localStorage . getItem ( storageKey ) ;
66- if ( savedOrder ) {
67- var itemIds = JSON . parse ( savedOrder ) ;
68- var container = document . querySelector ( `.${ elementClass } ` ) ;
69-
70- // 按照保存的顺序重新排列元素
71- itemIds . forEach ( function ( id ) {
72- var element = document . querySelector ( '[data-id="' + id + '"]' ) ;
73- if ( element ) {
74- container . appendChild ( element ) ;
75- }
76- } ) ;
65+ try {
66+ var savedOrder = localStorage . getItem ( storageKey ) ;
67+ if ( savedOrder ) {
68+ var itemIds = JSON . parse ( savedOrder ) ;
69+ var container = document . querySelector ( `.${ elementClass } ` ) ;
70+
71+ // 按照保存的顺序重新排列元素
72+ itemIds . forEach ( function ( id ) {
73+ var element = document . querySelector ( '[data-id="' + id + '"]' ) ;
74+ if ( element ) {
75+ container . appendChild ( element ) ;
76+ }
77+ } ) ;
78+ }
79+ } catch ( e ) {
80+ // localStorage 不可用,跳过恢复顺序
7781 }
7882}
7983
8084// 删除指定前缀的所有 localStorage 键
8185function deleteKeysByPrefix ( prefix ) {
8286 const keysToDelete = [ ] ;
8387
84- // 遍历 localStorage 中的所有键
85- for ( let i = 0 ; i < localStorage . length ; i ++ ) {
86- const key = localStorage . key ( i ) ;
87-
88- // 检查键是否以指定前缀开头
89- if ( key . startsWith ( prefix ) ) {
90- keysToDelete . push ( key ) ;
88+ try {
89+ // 遍历 localStorage 中的所有键
90+ for ( let i = 0 ; i < localStorage . length ; i ++ ) {
91+ const key = localStorage . key ( i ) ;
92+
93+ // 检查键是否以指定前缀开头
94+ if ( key . startsWith ( prefix ) ) {
95+ keysToDelete . push ( key ) ;
96+ }
9197 }
98+
99+ // 删除匹配的键
100+ keysToDelete . forEach ( key => {
101+ localStorage . removeItem ( key ) ;
102+ console . log ( `Deleted: ${ key } ` ) ;
103+ } ) ;
104+ } catch ( e ) {
105+ // localStorage 不可用,跳过删除
92106 }
93107
94- // 删除匹配的键
95- keysToDelete . forEach ( key => {
96- localStorage . removeItem ( key ) ;
97- console . log ( `Deleted: ${ key } ` ) ;
98- } ) ;
99-
100108 return keysToDelete . length ;
101109}
102110
@@ -139,27 +147,39 @@ function initScript() {
139147 // 拖拽
140148 var el = document . querySelector ( '.container' ) ;
141149 if ( ! isKindleMode ( ) ) {
142- var sortable = Sortable . create ( el , {
143- delay : 300 , // 延迟300ms后才开始拖动,避免移动端滑动时误触发
144- delayOnTouchOnly : true , // 只在触摸设备上应用延迟
145- filter : '.toc-container' , // 允许直接选择文字
146- preventOnFilter : false , // 过滤时不阻止默认行为
147- onEnd : function ( evt ) {
148- // 获取所有项目的ID
149- var itemIds = Array . from ( evt . from . children ) . map ( function ( child ) {
150- return child . dataset . id ;
150+ try {
151+ var sortable = Sortable . create ( el , {
152+ delay : 300 , // 延迟300ms后才开始拖动,避免移动端滑动时误触发
153+ delayOnTouchOnly : true , // 只在触摸设备上应用延迟
154+ filter : '.toc-container' , // 允许直接选择文字
155+ preventOnFilter : false , // 过滤时不阻止默认行为
156+ onEnd : function ( evt ) {
157+ // 获取所有项目的ID
158+ var itemIds = Array . from ( evt . from . children ) . map ( function ( child ) {
159+ return child . dataset . id ;
160+ } ) ;
161+ // 保存到 localStorage
162+ try {
163+ localStorage . setItem ( storageKeySortableContainer , JSON . stringify ( itemIds ) ) ;
164+ } catch ( e ) {
165+ // localStorage 不可用,跳过保存
166+ }
167+ }
151168 } ) ;
152- // 保存到 localStorage
153- localStorage . setItem ( storageKeySortableContainer , JSON . stringify ( itemIds ) ) ;
169+ } catch ( e ) {
170+ // 拖拽功能初始化失败,跳过
154171 }
155- } ) ;
156172 }
157173
158174 // 书籍目录锚点删除
159175 const anchor = window . location . hash ;
160176 if ( ! isKindleMode ( ) ) {
161177 if ( anchor === '' || ! anchor . startsWith ( '#chapter_' ) ) {
162- localStorage . removeItem ( book_hash ) ; // 此时 lastPart 就是 book_hash
178+ try {
179+ localStorage . removeItem ( book_hash ) ; // 此时 lastPart 就是 book_hash
180+ } catch ( e ) {
181+ // localStorage 不可用,跳过
182+ }
163183 }
164184 } else {
165185 if ( anchor === '' || ! anchor . startsWith ( '#chapter_' ) ) {
@@ -176,9 +196,13 @@ function initScript() {
176196 // 检查本地存储中的主题设置
177197 let currentTheme = 'light' ;
178198 if ( ! isKindleMode ( ) ) {
179- currentTheme = localStorage . getItem ( 'theme' ) ;
180- fontFamily = localStorage . getItem ( 'font_family' ) || "system-ui, -apple-system, sans-serif" ;
181- fontFamilyInput = localStorage . getItem ( 'font_family_input' ) ;
199+ try {
200+ currentTheme = localStorage . getItem ( 'theme' ) ;
201+ fontFamily = localStorage . getItem ( 'font_family' ) || "system-ui, -apple-system, sans-serif" ;
202+ fontFamilyInput = localStorage . getItem ( 'font_family_input' ) ;
203+ } catch ( e ) {
204+ // localStorage 不可用,使用默认值
205+ }
182206 } else {
183207 currentTheme = getCookie ( 'theme' ) ;
184208 fontFamily = getCookie ( 'font_family' ) || "system-ui, -apple-system, sans-serif" ;
@@ -202,15 +226,23 @@ function initScript() {
202226 themeIcon . classList . remove ( 'fa-moon' ) ;
203227 themeIcon . classList . add ( 'fa-sun' ) ;
204228 if ( ! isKindleMode ( ) ) {
205- localStorage . setItem ( 'theme' , 'dark' ) ;
229+ try {
230+ localStorage . setItem ( 'theme' , 'dark' ) ;
231+ } catch ( e ) {
232+ // localStorage 不可用,跳过保存
233+ }
206234 } else {
207235 setCookie ( 'theme' , 'dark' ) ;
208236 }
209237 } else {
210238 themeIcon . classList . remove ( 'fa-sun' ) ;
211239 themeIcon . classList . add ( 'fa-moon' ) ;
212240 if ( ! isKindleMode ( ) ) {
213- localStorage . setItem ( 'theme' , 'light' ) ;
241+ try {
242+ localStorage . setItem ( 'theme' , 'light' ) ;
243+ } catch ( e ) {
244+ // localStorage 不可用,跳过保存
245+ }
214246 } else {
215247 setCookie ( 'theme' , 'light' ) ;
216248 }
@@ -252,16 +284,24 @@ function initBookShelfButton(bookHash) {
252284
253285 // 获取书架数据
254286 function getBookshelf ( ) {
255- const data = localStorage . getItem ( BOOKSHELF_KEY ) ;
256- if ( data ) {
257- return JSON . parse ( data ) ;
287+ try {
288+ const data = localStorage . getItem ( BOOKSHELF_KEY ) ;
289+ if ( data ) {
290+ return JSON . parse ( data ) ;
291+ }
292+ } catch ( e ) {
293+ // localStorage 不可用,返回空书架
258294 }
259295 return { items : [ ] , groups : { } } ;
260296 }
261297
262298 // 保存书架数据
263299 function saveBookshelf ( data ) {
264- localStorage . setItem ( BOOKSHELF_KEY , JSON . stringify ( data ) ) ;
300+ try {
301+ localStorage . setItem ( BOOKSHELF_KEY , JSON . stringify ( data ) ) ;
302+ } catch ( e ) {
303+ // localStorage 不可用,跳过保存
304+ }
265305 }
266306
267307 // 检查书籍是否在书架中(包括所有分组)
0 commit comments