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

Skip to content

refactor: add detection module#163

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

refactor: add detection module#163
timerring merged 1 commit intomainfrom
dev

Conversation

@timerring
Copy link
Member

Summary / 概述

This PR refactors the detection module by reorganizing the code structure into a proper package architecture. All platform detection logic has been modularized and moved from a single monolithic file into organized submodules.

Related Issue / 关联 Issue

N/A

Type of Change / 更改类型

  • Code refactoring / 代码重构

Changes Made / 更改内容

  • Extracted platform configuration from packages/detection/index.js (373 lines) into packages/detection/src/configs.js (361 lines)
  • Created new packages/detection/src/detect.js for centralized detection logic with specialized and generic detection strategies
  • Moved utility functions to packages/detection/src/utils.js (121 lines) including checkLoginByCookie and detectByApi
  • Created platform-specific detection modules:
    • packages/detection/src/platforms/csdn.js (53 lines) - CSDN cookie-based detection
    • packages/detection/src/platforms/oschina.js (75 lines) - OSChina HTML parsing detection
  • Created packages/detection/src/detectors.js (644 lines) for special platform detectors
  • Updated packages/detection/index.js to export from submodules using ES6 module syntax
  • Updated apps/extension/package.json to add @cose/detection as a workspace dependency
  • Refactored apps/extension/src/background.js to use the new modular detection package

Implementation Details / 实现细节

Key Changes / 主要更改:

  • Modular Architecture: Detection logic now follows a clear separation of concerns:

    • configs.js: Configuration objects for all platforms
    • detect.js: Main detection orchestration with strategy pattern
    • utils.js: Reusable utility functions for cookie and API detection
    • platforms/*: Platform-specific detection implementations
    • detectors.js: Special detectors that require custom logic
  • Detection Strategy: The new detectUser() function implements a three-tier strategy:

    1. Specialized detectors for complex platforms (CSDN, OSChina)
    2. Special detector mapping for platforms with unique requirements (Alipay, Weibo, Wechat, etc.)
    3. Generic config-based detection for standard API/cookie-based platforms
  • Improved Maintainability:

    • Each platform's detection logic is now isolated and easier to debug
    • New platforms can be added by simply adding configuration or creating a detector
    • Code is more testable with clear module boundaries

Technical Notes / 技术说明:

  • Maintains backward compatibility - all existing detection functionality preserved
  • No breaking changes to external API
  • File size reduction in main index.js (from 373 lines to 3 lines)
  • Better code organization enables future enhancements like parallel detection

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 multiple platforms (CSDN, Juejin, Zhihu, etc.)
  3. Verify that user information (username, avatar) is correctly retrieved
  4. Confirm that background.js correctly imports and uses the detection module

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 lays the groundwork for future improvements:

  • Easier to add new platform support
  • Better code reusability across different detection methods
  • Potential for implementing concurrent detection across multiple platforms
  • Simplified debugging and maintenance

@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 3d4e6b4 into main Feb 8, 2026
@timerring timerring deleted the dev branch February 8, 2026 09:05
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