Create a script to register Scramjet’s service worker. This is essential for intercepting and proxying requests:
// Register the service workerif ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js', { scope: '/', }).then((registration) => { console.log('Scramjet service worker registered:', registration); }).catch((error) => { console.error('Service worker registration failed:', error); });}
The service worker file (sw.js) should import Scramjet using importScripts('/scramjet.all.js') and set up the fetch handler. See the quickstart guide for a complete service worker example.
The scope parameter defines the URL prefix that the service worker will intercept. By default, Scramjet uses /scramjet/.
3
Initialize the controller
After the service worker is registered and ready, load and initialize the Scramjet controller:
// Wait for service worker to be readyawait navigator.serviceWorker.ready;// Load the controller moduleconst { ScramjetController } = $scramjetLoadController();// Create a controller instance with configurationconst scramjet = new ScramjetController({ prefix: '/scramjet/', flags: { strictRewrites: true, captureErrors: true, },});// Initialize the controllerawait scramjet.init();
4
Create and use a frame
Create an isolated iframe context for proxied browsing:
// Create a new Scramjet frameconst frame = scramjet.createFrame();// Add the iframe to your pagedocument.body.appendChild(frame.frame);// Navigate to a URLframe.go('https://example.com');