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

Skip to content

perf: optimize zhihu sync time#144

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

perf: optimize zhihu sync time#144
timerring merged 1 commit intomainfrom
dev

Conversation

@timerring
Copy link
Member

Summary / 概述

This PR optimizes the Zhihu (知乎) sync functionality by refactoring the platform-specific implementation from inline code to a modular sync handler pattern. The main improvement is reducing the initial page load wait time from 3000ms to a more efficient approach using MutationObserver.

Related Issue / 关联 Issue

Close #143

Type of Change / 更改类型

  • Bug fix / 修复 Bug (non-breaking change that fixes an issue / 修复问题的非破坏性更改)
  • New feature / 新功能 (non-breaking change that adds functionality / 添加功能的非破坏性更改)
  • Breaking change / 破坏性更改 (fix or feature that would cause existing functionality to not work as expected / 会导致现有功能无法正常工作的修复或功能)
  • Documentation update / 文档更新
  • Performance improvement / 性能优化
  • Code refactoring / 代码重构
  • Other / 其他 (please describe / 请描述):

Changes Made / 更改内容

  • Moved Zhihu-specific sync logic from src/background.js to src/platforms/zhihu.js (removed 108 lines, added 134 lines)
  • Replaced hardcoded 3000ms wait time with MutationObserver-based element detection
  • Implemented syncZhihuContent function following the established platform sync handler pattern
  • Added proper error handling with return values indicating success/failure
  • Improved code organization by separating concerns

Implementation Details / 实现细节

Key Changes / 主要更改:

  • Refactored sync logic: Extracted the Zhihu document import functionality from inline code in background.js to a dedicated syncZhihuContent handler in zhihu.js
  • Performance optimization: Replaced setTimeout(resolve, 3000) with MutationObserver in waitForElement function, allowing dynamic detection of DOM elements instead of fixed waiting
  • Modular architecture: Leveraged existing injectUtils to inject the waitFor helper function into the page context
  • Consistent pattern: Now Zhihu follows the same sync handler pattern as CSDN, Juejin, and WeChat platforms

Technical Notes / 技术说明:

  • The fillZhihuContent function executes in the MAIN world context to access page DOM without restrictions
  • Uses MutationObserver for efficient element detection with configurable timeout fallback
  • Maintains the document import workflow: title filling → clicking import button → clicking import document → uploading markdown file
  • File upload uses both DataTransfer API and drag-drop events for better compatibility

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. Navigate to Zhihu publish page
  2. Trigger sync operation with markdown content
  3. Verify document import workflow completes successfully
  4. Check console logs for execution flow confirmation
  5. Confirm markdown file uploads and title fills correctly

Screenshots/Videos / 截图/视频

N/A - Performance improvement, no visual changes

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 / 补充说明

The sync time optimization is achieved by eliminating unnecessary fixed wait times and using event-driven element detection. This makes the sync process faster and more reliable, as it proceeds immediately when elements become available rather than waiting for arbitrary timeouts.

@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 54547b2 into main Jan 31, 2026
@timerring timerring deleted the dev branch January 31, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

知乎同步存在固定等待时间,导致同步过程较长

1 participant