This guide will walk you through setting up a basic Scramjet proxy. You’ll learn how to register a service worker, initialize the controller, and create a proxied iframe.
Service worker - Intercepts and rewrites network requests
Controller - Manages frames and communicates with the service worker
1
Create the service worker
Create a file called sw.js in your static directory:
sw.js
importScripts("/scramjet/scramjet.all.js");const { ScramjetServiceWorker } = $scramjetLoadWorker();const scramjet = new ScramjetServiceWorker();async function handleRequest(event) { await scramjet.loadConfig(); if (scramjet.route(event)) { return scramjet.fetch(event); } return fetch(event.request);}self.addEventListener("fetch", (event) => { event.respondWith(handleRequest(event));});
This service worker intercepts all fetch requests. If a request matches Scramjet’s routing (based on the configured prefix), it’s proxied. Otherwise, it’s passed through normally.
2
Set up your HTML page
Create an HTML page that will load Scramjet and register the service worker:
Scramjet provides a ScramjetFrame class for managing proxied iframes:
// Create a frame (Scramjet creates the iframe element)const frame = scramjet.createFrame();document.body.appendChild(frame.frame);// Or use an existing iframe elementconst existingIframe = document.getElementById("myframe");const frame = scramjet.createFrame(existingIframe);// Navigate to a URLframe.navigate("https://example.com");
You can encode and decode URLs using the controller:
// Encode a URL for use in the proxyconst proxiedUrl = scramjet.encodeUrl("https://example.com");// Returns: "/scramjet/https%3A%2F%2Fexample.com"// Decode a proxied URL back to the originalconst originalUrl = scramjet.decodeUrl(proxiedUrl);// Returns: "https://example.com"
If CAPTCHAs aren’t working, make sure you’re not hosting on a datacenter IP. Residential IPs work best for CAPTCHA-heavy sites like Google and YouTube.
Service workers require HTTPS in production environments (except for localhost). Make sure your site is served over HTTPS.