npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

chrome-extension-fetch-proxy

v1.0.1

Published

A Chrome extension fetch proxy library for handling cross-origin requests through background scripts

Readme

Chrome Extension Fetch Proxy

npm version License: MIT

A lightweight library for handling cross-origin fetch requests in Chrome extensions through background script communication.

Features

  • 🔄 Seamless cross-origin request handling
  • 🛡️ Built-in timeout and error handling
  • 🔧 Flexible configuration options
  • 📦 Multiple build formats (UMD, CommonJS, ES Modules)
  • 📝 TypeScript support with full type definitions
  • 🎯 Zero dependencies
  • 🔍 Debug mode for development

Installation

npm install chrome-extension-fetch-proxy

Quick Start

1. Content Script Usage

import ChromeExtensionFetchProxy from 'chrome-extension-fetch-proxy';

// Initialize the proxy
const fetchProxy = new ChromeExtensionFetchProxy({
  debug: true, // Enable debug logging
  timeout: 10000 // 10 second timeout
});

// Send request to background script
try {
  const response = await fetchProxy.sendFetchRequestToBackground(
    'https://api.example.com/data',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ key: 'value' })
    }
  );
  
  console.log('Response:', response);
} catch (error) {
  console.error('Request failed:', error);
}

2. Background Script Setup

import ChromeExtensionFetchProxy from 'chrome-extension-fetch-proxy';

// Initialize the proxy
const fetchProxy = new ChromeExtensionFetchProxy({
  debug: true
});

// Register message listener (uses default handler)
fetchProxy.registerBackgroundListener();

// Or use custom handler
fetchProxy.registerBackgroundListener((message, sender, sendResponse) => {
  // Custom processing logic
  console.log('Custom handler received:', message);
  
  // Your custom fetch logic here
  fetch(message.url, message.options)
    .then(response => response.json())
    .then(data => sendResponse({ data }))
    .catch(error => sendResponse({ error: error.message }));
    
  return true; // Keep message channel open
});

API Reference

ChromeExtensionFetchProxy

Constructor

new ChromeExtensionFetchProxy(options)

Options:

  • messageType (string): Message type for communication (default: 'SANDBOX_FETCH')
  • timeout (number): Request timeout in milliseconds (default: 10000)
  • debug (boolean): Enable debug logging (default: false)

Methods

sendFetchRequestToBackground(url, options)

Send a fetch request from content script to background script.

Parameters:

  • url (string): Request URL
  • options (Object): Fetch options (same as native fetch)

Returns: Promise resolving to response object

createBackgroundMessageHandler(customHandler)

Create a background message handler function.

Parameters:

  • customHandler (Function): Custom handler function (optional)

Returns: Message handler function

registerBackgroundListener(customHandler)

Register background message listener.

Parameters:

  • customHandler (Function): Custom handler function (optional)
unregisterBackgroundListener()

Unregister background message listener.

Response Format

The library returns responses in the following format:

{
  status: 200,           // HTTP status code
  headers: {             // Response headers
    'content-type': 'application/json',
    // ... other headers
  },
  data: {                // Response data (parsed JSON or text)
    // ... response data
  }
}

Error Handling

The library provides comprehensive error handling:

try {
  const response = await fetchProxy.sendFetchRequestToBackground(url, options);
  // Handle successful response
} catch (error) {
  if (error.message === 'Request timeout') {
    // Handle timeout
  } else if (error.message.includes('Chrome runtime API not available')) {
    // Handle missing Chrome API
  } else {
    // Handle other errors
  }
}

TypeScript Support

Full TypeScript support is included:

import ChromeExtensionFetchProxy, { FetchOptions, FetchResponse } from 'chrome-extension-fetch-proxy';

const fetchProxy = new ChromeExtensionFetchProxy();

const options: FetchOptions = {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer token'
  }
};

const response: FetchResponse = await fetchProxy.sendFetchRequestToBackground(
  'https://api.example.com/data',
  options
);

Browser Compatibility

  • Chrome 42+
  • Edge 79+
  • Firefox 48+ (with manifest v3 limitations)

Development

# Install dependencies
npm install

# Build the project
npm run build

# Development mode (watch files)
npm run dev

# Run tests
npm test

Example Chrome Extension Manifest

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0",
  "permissions": [
    "storage",
    "activeTab"
  ],
  "host_permissions": [
    "https://*/*"
  ],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }]
}

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please open an issue on GitHub.