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

Skip to content

refactor: Centralize and inject common utility functions#142

Merged
timerring merged 1 commit intomainfrom
dev
Jan 31, 2026
Merged

refactor: Centralize and inject common utility functions#142
timerring merged 1 commit intomainfrom
dev

Conversation

@timerring
Copy link
Member

Summary / 概述

This PR refactors the common utility functions by centralizing them in common.js and injecting them into the page's main world context. This eliminates code duplication across platform-specific files (CSDN, Juejin, WeChat) and improves maintainability.

Related Issue / 关联 Issue

N/A

Type of Change / 更改类型

  • Code refactoring / 代码重构

Changes Made / 更改内容

  • Refactored common.js to export centralized utility functions (injectCommonUtils, injectUtils)
  • Created injectCommonUtils() function that defines window.waitFor and window.setInputValue in the page's main world
  • Improved waitFor implementation using MutationObserver for better performance and reliability
  • Enhanced setInputValue to work with React/Vue frameworks using native setters
  • Removed duplicate waitFor function implementations from csdn.js, juejin.js, and wechat.js
  • Updated all platform files to import and use injectUtils before content filling
  • Changed module export format from CommonJS to ES modules

Implementation Details / 实现细节

Key Changes / 主要更改:

  • common.js: Transformed from content filling logic to utility function provider

    • injectCommonUtils(): Injected into page main world, provides window.waitFor and window.setInputValue
    • injectUtils(): Helper function to inject utils via Chrome scripting API
    • waitFor(): Now uses MutationObserver with timeout instead of polling
    • setInputValue(): Enhanced to trigger framework change detection using native setters
  • Platform files (csdn.js, juejin.js, wechat.js):

    • Added import { injectUtils } from './common.js'
    • Removed local waitFor implementations (16-23 lines each)
    • Call await injectUtils(chrome, tab.id) before executing content filling scripts
    • Updated to use window.waitFor instead of local waitFor

Technical Notes / 技术说明:

  • The utilities are injected into the 'MAIN' world to ensure they execute in the same context as the page's JavaScript
  • MutationObserver provides better performance than polling, with proper cleanup via disconnect()
  • Native setter approach ensures React/Vue can detect value changes via Object.getOwnPropertyDescriptor
  • All platform-specific files now maintain a consistent pattern for utility injection

Testing / 测试

Testing Checklist / 测试清单

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

Manual Testing Steps / 手动测试步骤

  1. Test content synchronization on CSDN platform
  2. Test content synchronization on Juejin platform
  3. Test content synchronization on WeChat Official Account platform
  4. Verify that title and content are correctly filled on all platforms
  5. Confirm that editor change events are properly triggered

Screenshots/Videos / 截图/视频

N/A

Reviewer Checklist / 审阅者清单

  • Code follows the project's style guidelines / 代码遵循项目的风格指南
  • Changes are well-documented / 更改有良好的文档说明
  • No breaking changes or clearly documented if present / 无破坏性更改,或已清楚记录
  • Security implications have been considered / 已考虑安全影响
  • Performance impact has been evaluated / 已评估性能影响
  • All discussions have been resolved / 所有讨论已解决

Additional Notes / 补充说明

  • This refactoring reduces code duplication by approximately 50+ lines across all platform files
  • The new implementation is more robust with better error handling and timeout management
  • Future platform integrations can easily reuse these centralized utilities

…platform-specific content filling to use them.
@timerring timerring self-assigned this Jan 31, 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 34fb8dc into main Jan 31, 2026
@timerring timerring deleted the dev branch January 31, 2026 09:35
@timerring timerring restored the dev branch January 31, 2026 09:35
@timerring timerring deleted the dev branch January 31, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant