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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ext-corb-workaround

v2.0.0

Published

A work-around for CORB restrictions in Chrome extensions

Downloads

58

Readme

ext-corb-workaround

GitHub license npm version CI

This package is a work-around for a bug with Cross-Origin Request Blocking (CORB) as implemented in Chrome extensions.

According to https://www.chromium.org/Home/chromium-security/extension-content-script-fetches, "content scripts should be subject to the same request rules as the page they are running within", but currently Chrome blocks requests from content scripts if the extension has permissions to the requested domain, regardless of whether the page it's running within also has permissions to the requested domain because of CORS.

In order to work around this issue, this package allows creating a replacement XMLHttpRequest object for a content script to use which proxies its connections through the page's context, so the request is treated exactly as a request from the parent page instead of from the extension.

Because this proxies the connection through the web page's javascript context, it is possible for the web page to modify the content of the request. This package should only be used by extensions that trust the web page the content script is running in. This makes this package a good fit for an extension that adds features to one specific site. It is not recommended to use this package in a content script that executes inside all web pages.

If possible, follow the recommendations inside the "Recommended Developer Actions" section of Chrome's CORB/extension documentation instead of using this package. This package should only be used where you specifically want the request to run as if it came from the content script's page rather than as the extension.

Setup

In order to use this library, you must execute a script in the page's main world which calls an initializer function from this library. This requires a separate content script, background script, and page world script in your extension.

In an extension content script:

chrome.runtime.sendMessage({ type: 'injectPageWorld' });

In an extension background service worker:

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === 'injectPageWorld' && sender.tab) {
    chrome.scripting.executeScript({
      target: { tabId: sender.tab.id },
      world: 'MAIN',
      files: ['pageWorld.js'],
    });
  }
});

In pageWorld.js:

import { init } from 'ext-corb-workaround/dist/src/pageWorld';
init();

API

This library exports two functions:

getXMLHttpRequest(): typeof XMLHttpRequest

This function returns an XMLHttpRequest-like object which can be used like XMLHttpRequest, but with all of its requests proxied through the page's context.

Usage example:

import { getXMLHttpRequest } from 'ext-corb-workaround';

function getWithCorbWorkaround(url) {
  return new Promise((resolve, reject) => {
    const XMLHttpRequest = getXMLHttpRequest();
    const xhr = new XMLHttpRequest();
    xhr.addEventListener('loadend', () => {
      if (xhr.status !== 200) {
        reject(new Error(`Error fetching: ${xhr.statusText}`));
      } else {
        resolve(xhr.responseText);
      }
    });
    xhr.open('GET', url);
    xhr.send();
  });
}

installGlobally(): void

This is a convenience function which replaces window.XMLHttpRequest with the value returned by getXMLHttpRequest(). This may be useful for an extension content script that wants to globally opt into proxying its connections through the page.

Types

TypeScript type definitions for this module are included.