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

Skip to content

fix: bypass cors restriction#165

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

fix: bypass cors restriction#165
timerring merged 1 commit intomainfrom
dev

Conversation

@timerring
Copy link
Member

Summary / 概述

This PR fixes CORS (Cross-Origin Resource Sharing) restrictions when loading Weibo user avatars from sinaimg.cn. The avatars are now converted to base64 data URLs to bypass CORS restrictions, allowing them to be properly displayed in the extension.

Related Issue / 关联 Issue

N/A

Type of Change / 更改类型

  • Bug fix / 修复 Bug (non-breaking change that fixes an issue / 修复问题的非破坏性更改)

Changes Made / 更改内容

  • Added logic to detect if the avatar URL is from sinaimg.cn domain
  • Implemented avatar conversion from remote URL to base64 data URL
  • Added proper error handling with try-catch block and console logging
  • Used Referer: https://weibo.com/ header when fetching the avatar to comply with Weibo's referrer policy

Implementation Details / 实现细节

Key Changes / 主要更改:

  1. CORS Detection: Check if avatar URL contains sinaimg.cn
  2. Image Fetching: Fetch the image with proper Referer header set to https://weibo.com/
  3. Base64 Conversion:
    • Convert response to blob
    • Convert blob to ArrayBuffer
    • Create Uint8Array from buffer
    • Iterate through bytes and build binary string using String.fromCharCode()
    • Encode binary string to base64 using btoa()
  4. Data URL Creation: Generate data URL with format data:{mime};base64,{base64}
  5. Error Handling: Wrap conversion logic in try-catch with console logging

Technical Notes / 技术说明:

  • The service worker context allows fetching with custom headers (Referer) which bypasses Weibo's referrer restriction
  • MIME type is extracted from blob.type, defaulting to image/jpeg if unavailable
  • The conversion happens before returning the user detection result
  • If conversion fails, the original avatar URL is preserved (graceful degradation)

Testing / 测试

  • Tested on Weibo platform with logged-in user
  • Verified avatar displays correctly after conversion
  • Confirmed error handling works when conversion fails

@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 114833d into main Feb 8, 2026
@timerring timerring deleted the dev branch February 8, 2026 10:23
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