This project provides a bundled version of Polly.JS specifically configured for use in Tampermonkey (or Greasemonkey/Violentmonkey) scripts.
It bundles the core PollyJS library along with Fetch and XHR adapters, and a custom In-Memory Persister, making it easy to intercept and mock network requests within userscripts.
To use this bundle in your userscript, include it via the @require directive. We recommend using jsDelivr because GitHub's raw file serving (raw.githubusercontent.com) frequently returns 429 (Too Many Requests) errors, even with low traffic (see this discussion).
// ==UserScript==
// @name My Script with PollyJS
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Intercept requests with PollyJS
// @author You
// @match https://example.com/*
// @require https://cdn.jsdelivr.net/gh/rafaelmfonseca/pollyjs-esbundle@main/dist/bundle.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Initialize PollyJS and setup interceptors
const { pollyInstance, stop } = window.$pollyIntercept({
request: (req) => {
console.log('Request:', req.method, req.url);
},
response: (req, res) => {
console.log('Response:', res.statusCode);
}
});
})();The bundle exposes the following global variables:
The Polly class constructor. You can use this to instantiate Polly manually or access static methods.
Initializes PollyJS and registers interceptors.
Parameters:
options(Object):request(Function): A callback function invoked on every request. Receives(req, res).response(Function): A callback function invoked on every response. Receives(req, res).
Returns:
Object:pollyInstance: ThePollyinstance.stop(Function): An async function to flush and stop the Polly instance.
-
Install dependencies:
npm install
-
Build the bundle:
npm run build
This command uses
esbuildto bundle the source intodist/bundle.js.
MIT