* 处理`页面`请求
()
| 142 | * 处理`页面`请求 |
| 143 | */ |
| 144 | async function handlePage() { |
| 145 | // 获取 BoxJs 数据 |
| 146 | const boxdata = getBoxData() |
| 147 | boxdata.syscfgs.isDebugMode = false |
| 148 | |
| 149 | // 调试模式: 是否每次都获取新的页面 |
| 150 | const isDebugWeb = [true, 'true'].includes( |
| 151 | $.getdata('@chavy_boxjs_userCfgs.isDebugWeb') |
| 152 | ) |
| 153 | const debugger_web = $.getdata('@chavy_boxjs_userCfgs.debugger_web') |
| 154 | const cache = $.getjson($.KEY_web_cache, null) |
| 155 | |
| 156 | // 如果没有开启调试模式,且当前版本与缓存版本一致,且直接取缓存 |
| 157 | if (!isDebugWeb && cache && cache.version === $.version) { |
| 158 | $.html = cache.cache |
| 159 | } |
| 160 | // 如果开启了调试模式,并指定了 `debugger_web` 则从指定的地址获取页面 |
| 161 | else { |
| 162 | if (isDebugWeb && debugger_web) { |
| 163 | // 调试地址后面拼时间缀, 避免 GET 缓存 |
| 164 | const isQueryUrl = debugger_web.includes('?') |
| 165 | $.web = `${debugger_web}${ |
| 166 | isQueryUrl ? '&' : '?' |
| 167 | }_=${new Date().getTime()}` |
| 168 | boxdata.syscfgs.isDebugMode = true |
| 169 | console.log(`[WARN] 调试模式: $.web = : ${$.web}`) |
| 170 | } |
| 171 | // 如果调用这个方法来获取缓存, 且标记为`非调试模式` |
| 172 | const getcache = () => { |
| 173 | console.log(`[ERROR] 调试模式: 正在使用缓存的页面!`) |
| 174 | boxdata.syscfgs.isDebugMode = false |
| 175 | return $.getjson($.KEY_web_cache).cache |
| 176 | } |
| 177 | await $.http.get($.web).then( |
| 178 | (resp) => { |
| 179 | if (/<title>BoxJs<\/title>/.test(resp.body)) { |
| 180 | // 返回页面源码, 并马上存储到持久化仓库 |
| 181 | $.html = resp.body |
| 182 | const cache = { version: $.version, cache: $.html } |
| 183 | $.setjson(cache, $.KEY_web_cache) |
| 184 | } else { |
| 185 | // 如果返回的页面源码不是预期的, 则从持久化仓库中获取 |
| 186 | $.html = getcache() |
| 187 | } |
| 188 | }, |
| 189 | // 如果获取页面源码失败, 则从持久化仓库中获取 |
| 190 | () => ($.html = getcache()) |
| 191 | ) |
| 192 | } |
| 193 | // 根据偏好设置, 替换首屏颜色 (如果是`auto`则交由页面自适应) |
| 194 | const theme = $.getdata('@chavy_boxjs_userCfgs.theme') |
| 195 | if (theme === 'light') { |
| 196 | $.html = $.html.replace('#121212', '#fff') |
| 197 | } else if (theme === 'dark') { |
| 198 | $.html = $.html.replace('#fff', '#121212') |
| 199 | } |
| 200 | /** |
| 201 | * 后端渲染数据, 感谢 https://t.me/eslint 提供帮助 |
no test coverage detected
searching dependent graphs…