
A Chrome extension for uploading images from web pages to a Cloudflare R2 bucket via right-click
- Clone this repository
cd
into the project directory- Run
pnpm install
to install dependencies - Run
pnpm dev
to run in development mode - Run
pnpm build
to build for production - After building the extension, open Chrome browser and visit
chrome://extensions
- Enable "Developer mode" in the top right corner
- Click "Load unpacked extension" and select the
.output/chrome-mv3
folder in the project directory
This extension requires a Cloudflare Worker to function. Here are the complete deployment steps:
You can find the Worker code example in worker_sample.js.
This extension uses the Worker-bound R2 bucket pattern:
- Worker is bound to a fixed R2 bucket, managing all actual operations
- Extension frontend only needs to configure Cloudflare ID and Worker URL
- Optional folder path configuration for storing in different paths within the bucket
- Log in to Cloudflare Dashboard
- Select "R2" from the sidebar
- Click "Create bucket" and enter a bucket name
- Note down the bucket name for later configuration in the extension and Worker
- On the R2 page, click "Manage R2 API Tokens"
- Click "Create API Token"
- Select "Admin Read & Write" permissions
- After creation, note down the
Access Key ID
andSecret Access Key
- Note: This is your only chance to see the Secret Key, please save it securely
-
Select "Workers & Pages" from the Cloudflare Dashboard sidebar
-
Click "Create application" and select "Worker"
-
Name your Worker and click "Create Worker"
-
After deployment, click the Worker name to enter details page
-
Click the "Settings" tab, then:
a. Bind R2 Bucket:
- In the "Variables" section, click "R2 Bucket Bindings"
- Click "Add binding"
- Enter
BUCKET_NAME
as the variable name (must match the name used in Worker code) - Select the previously created R2 bucket
- Click "Save"
b. Configure Environment Variables:
- In the "Variables" section, click "Environment Variables"
- Add the following environment variables:
R2_ACCESS_KEY_ID
: Enter the previously saved Access Key IDR2_SECRET_ACCESS_KEY
: Enter the previously saved Secret Access KeyALLOWED_CLOUDFLARE_ID
: Enter your Cloudflare Account ID (32-character hex string)- This is a critical security measure that ensures only your account ID is authorized
- You can find your Account ID in the Cloudflare Dashboard URL or in the right sidebar
- Click "Save"
-
Click "Quick edit" button and paste the Worker code provided in this project
-
Update the
ALLOWED_ORIGINS
array inwxt.config.ts
with your extension ID (optional) -
Click "Save and deploy"
-
Install Wrangler CLI:
npm install -g wrangler
-
Log in to Cloudflare account:
wrangler login
-
Create Worker project:
mkdir d2r2-worker && cd d2r2-worker wrangler init
-
Create and configure
wrangler.toml
:name = "d2r2-worker" main = "src/index.js" compatibility_date = "2025-04-0" # R2 bucket binding [[r2_buckets]] binding = "BUCKET_NAME" # Must match the name in Worker code bucket_name = "your-bucket-name" # Your R2 bucket name # Environment variables (sensitive info, use wrangler secret instead of writing here) [vars] # You can set non-sensitive environment variables here
-
Save the Worker code provided in this project to
src/index.js
-
Update the
ALLOWED_ORIGINS
array in the code with your extension ID -
Add secrets (do not add these to the config file):
wrangler secret put R2_ACCESS_KEY_ID # Enter your Access Key ID when prompted wrangler secret put R2_SECRET_ACCESS_KEY # Enter your Secret Access Key when prompted wrangler secret put ALLOWED_CLOUDFLARE_ID # Enter your Cloudflare Account ID (32-character hex string) when prompted # This is a critical security measure to prevent unauthorized usage
-
Publish Worker:
wrangler publish
-
Wrangler will output your Worker URL, note it down for later configuration in the extension
- Never store R2 access keys in frontend code
- Ensure
ALLOWED_ORIGINS
restricts access to only your extension - Always configure the
ALLOWED_CLOUDFLARE_ID
environment variable as an authorization requirement- This ensures only requests with your specific Cloudflare Account ID are processed
- Prevents unauthorized use even if someone discovers your Worker URL
- Consider adding additional authentication mechanisms, such as custom tokens
- Regularly rotate R2 access keys
- Install and open the extension
- In the popup configuration page:
- Enter your Cloudflare Account ID
- Enter Worker URL, format:
https://your-worker-name.your-username.workers.dev
- Storage Path Configuration:
- If left empty: Images will be uploaded to the root directory of your R2 bucket
- If filled: A secondary menu will appear allowing you to choose between:
- Root directory: Upload directly to bucket root
- Custom folder: Upload to the specified folder path
- Save configuration
- Invalid or unexpected token: Check if environment variables are correctly configured, ensure using
env.variable_name
format - Unauthorized (403): Confirm extension ID is correctly added to
ALLOWED_ORIGINS
array - Server configuration error (500): Check if
ALLOWED_CLOUDFLARE_ID
environment variable is properly set - Invalid or unauthorized Cloudflare ID: Ensure the Cloudflare ID in your extension matches the one set in
ALLOWED_CLOUDFLARE_ID
- Failed to fetch image: Check if image URL is accessible, some websites may restrict cross-origin requests
- Service bind failed: Ensure R2 bucket binding name is
BUCKET_NAME
, must match Worker code - URL build error: Need to correctly configure
BUCKET_NAME_META
environment variable or access actual bucket name
MIT