Skip to content

Commit 52518f4

Browse files
authored
Merge pull request #15 from 2214962083/dev
修复今日热榜登录不生效问题
2 parents ea0670d + 7698f5d commit 52518f4

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/scripts/newsTop.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
showNotification,
1111
showPreviewOptions,
1212
useStorage,
13+
request,
1314
} from '@app/lib/help'
1415
import {FC} from 'react'
1516

@@ -45,6 +46,9 @@ interface PageInfo {
4546
/**文章列表*/
4647
articleList: ArticleInfo[]
4748

49+
/**cookie*/
50+
cookie: string | null
51+
4852
/**js执行的错误信息*/
4953
err: Error
5054
}
@@ -384,14 +388,28 @@ class NewsTop {
384388

385389
// 获取热榜数据
386390
async getNewsTop(url: string): Promise<PageInfo> {
391+
const cookieHeader: Record<string, string> = isLaunchInsideApp() ? {} : {cookie: getStorage<string>('cookie') || ''}
392+
const html =
393+
(
394+
await request<string>({
395+
url,
396+
dataType: 'text',
397+
header: {
398+
'user-agent':
399+
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
400+
...cookieHeader,
401+
},
402+
})
403+
).data || ''
387404
const webview = new WebView()
388-
await webview.loadURL(url)
405+
await webview.loadHTML(html, url)
389406
await webview.waitForLoad()
390-
const {title = '今日热榜', logo = 'flame.fill', articleList = [], err} = (await webview.evaluateJavaScript(
407+
const {title = '今日热榜', logo = 'flame.fill', articleList = [], cookie, err} = (await webview.evaluateJavaScript(
391408
`
392409
let title = ''
393410
let logo = ''
394411
let articleList = []
412+
let cookie = document.cookie
395413
let err = ''
396414
try {
397415
title = document.title.split(' ')[0]
@@ -406,11 +424,12 @@ class NewsTop {
406424
} catch(err) {
407425
err = err
408426
}
409-
Object.assign({}, {title, logo, articleList, err})
427+
Object.assign({}, {title, logo, articleList, cookie, err})
410428
`,
411429
)) as PageInfo
412430
err && console.warn(`热榜获取出错: ${err}`)
413-
return {title, logo, articleList, err}
431+
if (isLaunchInsideApp() && cookie) setStorage('cookie', cookie)
432+
return {title, logo, articleList, cookie, err}
414433
}
415434

416435
// 获取要显示的榜单 url

打包好的成品/今日热榜.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* github: https://github.com/2214962083/ios-scriptable-tsx
66
*/
77

8-
// @编译时间 1608170866803
8+
// @编译时间 1608188212533
99
const MODULE = module
1010
let __topLevelAwait__ = () => Promise.resolve()
1111
function EndAwait(promiseFunc) {
@@ -1135,13 +1135,27 @@ ${topList.map(top => top.title).join('、')}`,
11351135
}
11361136
}
11371137
async getNewsTop(url) {
1138+
const cookieHeader = isLaunchInsideApp() ? {} : {cookie: getStorage2('cookie') || ''}
1139+
const html =
1140+
(
1141+
await request({
1142+
url,
1143+
dataType: 'text',
1144+
header: {
1145+
'user-agent':
1146+
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
1147+
...cookieHeader,
1148+
},
1149+
})
1150+
).data || ''
11381151
const webview = new WebView()
1139-
await webview.loadURL(url)
1152+
await webview.loadHTML(html, url)
11401153
await webview.waitForLoad()
1141-
const {title = '今日热榜', logo = 'flame.fill', articleList = [], err} = await webview.evaluateJavaScript(`
1154+
const {title = '今日热榜', logo = 'flame.fill', articleList = [], cookie, err} = await webview.evaluateJavaScript(`
11421155
let title = ''
11431156
let logo = ''
11441157
let articleList = []
1158+
let cookie = document.cookie
11451159
let err = ''
11461160
try {
11471161
title = document.title.split(' ')[0]
@@ -1156,10 +1170,11 @@ ${topList.map(top => top.title).join('、')}`,
11561170
} catch(err) {
11571171
err = err
11581172
}
1159-
Object.assign({}, {title, logo, articleList, err})
1173+
Object.assign({}, {title, logo, articleList, cookie, err})
11601174
`)
11611175
err && console.warn(`热榜获取出错: ${err}`)
1162-
return {title, logo, articleList, err}
1176+
if (isLaunchInsideApp() && cookie) setStorage2('cookie', cookie)
1177+
return {title, logo, articleList, cookie, err}
11631178
}
11641179
getTopUrl() {
11651180
const defaultUrl = 'https://tophub.today/n/mproPpoq6O'

0 commit comments

Comments
 (0)