Skip to content

Commit 1e458f4

Browse files
authored
fix: elecfans detection (#185)
1 parent e2a8618 commit 1e458f4

2 files changed

Lines changed: 43 additions & 43 deletions

File tree

packages/detection/src/configs.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,6 @@ export const XiaohongshuLoginConfig = {
295295
}),
296296
}
297297

298-
// 电子发烧友
299-
export const ElecfansLoginConfig = {
300-
api: 'https://bbs.elecfans.com/api/login/check',
301-
method: 'GET',
302-
checkLogin: (response) => response?.status === 1,
303-
getUserInfo: (response) => ({
304-
username: response?.data?.username,
305-
avatar: response?.data?.avatar,
306-
}),
307-
}
308298

309299
// 统一的 LOGIN_CHECK_CONFIG 对象(按平台 ID 索引)
310300
export const LOGIN_CHECK_CONFIG = {
@@ -334,5 +324,4 @@ export const LOGIN_CHECK_CONFIG = {
334324
alipayopen: AlipayOpenLoginConfig,
335325
douyin: DouyinLoginConfig,
336326
xiaohongshu: XiaohongshuLoginConfig,
337-
elecfans: ElecfansLoginConfig,
338327
}

packages/detection/src/platforms/elecfans.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
1+
import { convertAvatarToBase64 } from '../utils.js'
2+
13
/**
2-
* Elecfans platform detection logic
3-
* Strategy:
4-
* 1. Check auth/auth_www cookies
5-
* 2. Call checklogin API for username/avatar
4+
* Elecfans (电子发烧友) detection logic
5+
* API: /api/mobile/index.php?module=profile (Discuz standard mobile API)
6+
* Response: { Variables: { member_uid, space: { username, realname }, member_avatar } }
7+
* Uses chrome.cookies.getAll to attach cookies manually (SameSite workaround)
68
*/
79
export async function detectElecfansUser() {
8-
const platformId = 'elecfans'
910
try {
10-
const authCookie = await chrome.cookies.get({ url: 'https://www.elecfans.com', name: 'auth' })
11-
const authWwwCookie = await chrome.cookies.get({ url: 'https://www.elecfans.com', name: 'auth_www' })
12-
13-
if (!authCookie && !authWwwCookie) {
14-
return { loggedIn: false }
15-
}
16-
17-
try {
18-
const response = await fetch('https://www.elecfans.com/webapi/passport/checklogin?_=' + Date.now(), {
19-
method: 'GET',
20-
credentials: 'include',
21-
headers: { 'Accept': 'application/json, text/javascript, */*; q=0.01' }
22-
})
23-
24-
if (!response.ok) return { loggedIn: true, username: '', avatar: '' }
25-
26-
const data = await response.json()
27-
if (data && data.uid) {
28-
const username = data.username || ''
29-
const avatar = data.avatar || ''
30-
return { loggedIn: true, username, avatar }
31-
} else {
32-
return { loggedIn: true, username: '', avatar: '' }
33-
}
34-
} catch (e) {
35-
return { loggedIn: true, username: '', avatar: '' }
36-
}
11+
// Collect cookies for bbs.elecfans.com
12+
const cookies = await chrome.cookies.getAll({ domain: '.elecfans.com' })
13+
const bbsCookies = await chrome.cookies.getAll({ url: 'https://bbs.elecfans.com' })
14+
const allCookies = [...cookies, ...bbsCookies]
15+
const seen = new Set()
16+
const uniqueCookies = allCookies.filter(c => {
17+
const key = `${c.name}=${c.value}`
18+
if (seen.has(key)) return false
19+
seen.add(key)
20+
return true
21+
})
22+
const cookieStr = uniqueCookies.map(c => `${c.name}=${c.value}`).join('; ')
23+
24+
if (!cookieStr) return { loggedIn: false }
25+
26+
const response = await fetch('https://bbs.elecfans.com/api/mobile/index.php?module=profile', {
27+
method: 'GET',
28+
headers: {
29+
'Accept': 'application/json',
30+
'Cookie': cookieStr,
31+
},
32+
})
33+
34+
if (!response.ok) return { loggedIn: false }
35+
36+
const data = await response.json()
37+
if (!data?.Variables?.member_uid) return { loggedIn: false }
38+
39+
const username = data.Variables.space?.username
40+
|| data.Variables.space?.realname
41+
|| data.Variables.member_username || ''
42+
let avatar = data.Variables.member_avatar || ''
43+
44+
if (!username) return { loggedIn: false }
45+
46+
if (avatar) avatar = await convertAvatarToBase64(avatar, 'https://bbs.elecfans.com/')
47+
return { loggedIn: true, username, avatar }
3748
} catch (e) {
3849
return { loggedIn: false }
3950
}

0 commit comments

Comments
 (0)