Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: split detection logic#164

Merged
timerring merged 1 commit intomainfrom
dev
Feb 8, 2026
Merged

refactor: split detection logic#164
timerring merged 1 commit intomainfrom
dev

Conversation

@timerring
Copy link
Member

Summary / 概述

This PR refactors the detection module by splitting the monolithic detectors.js file into separate platform-specific detection modules. Each special platform detector now has its own dedicated file, improving code organization and maintainability.

Related Issue / 关联 Issue

N/A

Type of Change / 更改类型

  • Code refactoring / 代码重构

Changes Made / 更改内容

  • Deleted packages/detection/src/detectors.js (644 lines) - monolithic file containing all special detectors
  • Created 14 individual platform detection modules in packages/detection/src/platforms/:
    • alipay.js (94 lines) - Alipay Open platform detection with cache strategy
    • aliyun.js (24 lines) - Aliyun Developer platform detection
    • elecfans.js (40 lines) - Elecfans platform detection with cookie-based auth
    • huaweicloud.js (50 lines) - Huawei Cloud detection with CSRF token handling
    • huaweidev.js (51 lines) - Huawei Developer platform detection
    • medium.js (38 lines) - Medium platform detection with regex parsing
    • qianfan.js (24 lines) - Baidu Qianfan platform detection
    • sohu.js (31 lines) - Sohu (搜狐号) platform detection
    • sspai.js (26 lines) - Sspai (少数派) platform with JWT token
    • tencentcloud.js (37 lines) - Tencent Cloud Developer platform
    • twitter.js (38 lines) - Twitter/X platform detection
    • wechat.js (104 lines) - WeChat Official Account with caching and multi-strategy detection
    • weibo.js (90 lines) - Weibo platform with complex cookie handling
    • xiaohongshu.js (64 lines) - Xiaohongshu (Little Red Book) with long-term caching
  • Updated packages/detection/src/detect.js (64 changes):
    • Removed wildcard import from detectors.js
    • Added individual imports for each platform detector
    • Refactored PLATFORM_DETECTORS map to use explicit imports instead of dynamic property access
    • Improved code clarity by making detector dependencies explicit

Implementation Details / 实现细节

Key Changes / 主要更改:

  • File-per-Platform Architecture: Each special platform detector is now isolated in its own file:

    • Easier to locate and modify platform-specific logic
    • Clear boundaries between different platform implementations
    • Each file documents its specific detection strategy at the top
  • Explicit Imports: Changed from import * as specialDetectors from './detectors.js' to individual named imports:

    • Better tree-shaking potential
    • Clearer dependency tracking
    • IDE can provide better autocomplete and navigation
  • Consistent Module Structure: Each platform detector follows the same pattern:

    • Exports a single async function named detect{Platform}User()
    • Includes JSDoc comment explaining detection strategy
    • Returns standardized { loggedIn, username?, avatar? } object
  • Detection Strategies Preserved:

    • Cache-based: Alipay (1 hour), WeChat (1 hour), Xiaohongshu (7 days)
    • Cookie-based: Aliyun, Elecfans, Sspai, Sohu
    • Tab injection: Huawei Cloud, Huawei Dev, WeChat, Xiaohongshu, Alipay
    • HTML parsing: Medium, Tencent Cloud, Twitter, Weibo

Technical Notes / 技术说明:

  • No functional changes - all detection logic preserved exactly
  • Total line count remains similar (644 deleted, 711 added across 14 files)
  • Each platform file is self-contained and testable independently
  • Better code locality - all related code for a platform is in one place
  • Facilitates parallel development - multiple developers can work on different platforms without conflicts

Testing / 测试

Testing Checklist / 测试清单

  • I have tested this code locally / 我已在本地测试此代码
  • All existing tests pass / 所有现有测试通过
  • I have tested on the affected platform(s) / 我已在受影响的平台上测试
  • I have verified the changes work in the target browser(s) / 我已验证更改在目标浏览器中有效

Manual Testing Steps / 手动测试步骤

  1. Build the extension with the refactored detection module
  2. Test login detection for each refactored platform (Alipay, Weibo, WeChat, etc.)
  3. Verify that all platform detectors still work correctly
  4. Confirm cache mechanisms work for platforms with caching (Alipay, WeChat, Xiaohongshu)
  5. Test that username and avatar information is correctly retrieved for all platforms

Reviewer Checklist / 审阅者清单

  • Code follows the project's style guidelines / 代码遵循项目的风格指南
  • Changes are well-documented / 更改有良好的文档说明
  • No breaking changes or clearly documented if present / 无破坏性更改,或已清楚记录
  • Performance impact has been evaluated / 已评估性能影响

Additional Notes / 补充说明

This refactoring improves the codebase structure by:

  • Reducing cognitive load: Developers only need to understand one platform's detection logic at a time
  • Improving maintainability: Each platform can be updated independently without affecting others
  • Enabling easier testing: Individual platform detectors can be unit tested in isolation
  • Facilitating code review: Changes to specific platforms are easier to review when isolated
  • Supporting future growth: New platforms can be added as new files without modifying existing detectors

@timerring timerring self-assigned this Feb 8, 2026
@timerring timerring added the enhancement New feature or request label Feb 8, 2026
Copy link
Member Author

@timerring timerring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@timerring timerring merged commit 3759428 into main Feb 8, 2026
@timerring timerring deleted the dev branch February 8, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant