-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Live Proxy Example + injectScripts via query args (#136)
* README: cleanup readme, point to new examples dir - add live proxy rewriting example in ./examples/live-proxy/ - demonstrates minimum functionality for using wabac.js as a live proxy, including with custom inject scripts * config: support passing injectScripts as query params to sw.js, eg. sw.js?injectScripts=path/to/script-1.js,path/another-script.js
- Loading branch information
Showing
8 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sw.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Live Proxy Example | ||
|
||
This example demonstrates the minimal use of `wabac.js` set up as a live rewriting proxy using a CORS proxy, and supporting | ||
custom script injection. | ||
|
||
The example consists of an `index.html` which loads wabac.js in a frame and `index.js`, which includes the setup | ||
for initializing a single collection that loads data through a CORS proxy. The service worker is assumed to be located in the root of this directory | ||
as well. | ||
|
||
|
||
### Usage | ||
|
||
1. Copy the latest build of `sw.js` to this directory, eg. `cp dist/sw.js examples/live-proxy/sw.js`. | ||
|
||
2. Start an http server, eg. `http-server -p 10001` | ||
|
||
3. Load `http://localhost:10001/`. The page should proxy `https://example.com/` by default. | ||
|
||
4. Specify a different URL to proxy by specifying the URL in the hashtag, eg: `http://localhost:10001/#https://iana.org/` | ||
|
||
### CORS Proxy | ||
|
||
All loading is done through a CORS proxy, which is presumed to be an instance of [wabac-cors-proxy](https://github.com/webrecorder/wabac-cors-proxy) | ||
|
||
The `proxyPrefix` in index.js should be configured to the proper endpoint for this proxy. We recommend deploying your own version of this proxy for production use. (The default endpoint is accessible for testing from `http://localhost:10001` and certain Webrecorder domains) | ||
|
||
|
||
### Custom Script Injection | ||
|
||
The example also demonstrates injecting a custom script into each replay page, by loading `sw.js?injectScripts=custom.js`. | ||
|
||
The custom script is not rewritten and can be used to add additional functionality to the proxy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Custom Script, Location is: " + window.location.href); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<style> | ||
html { | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
} | ||
body { | ||
width: 100%; | ||
} | ||
iframe { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
</style> | ||
<script src="./index.js"></script> | ||
</head> | ||
<body> | ||
<iframe id="content"></iframe> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const proxyPrefix = "https://wabac-cors-proxy.webrecorder.workers.dev/proxy/"; | ||
|
||
|
||
class WabacLiveProxy | ||
{ | ||
constructor() { | ||
this.url = ""; | ||
} | ||
|
||
async init() { | ||
const scope = "./"; | ||
|
||
// also add inject of custom.js as a script into each replayed page | ||
await navigator.serviceWorker.register("./sw.js?injectScripts=./custom.js", {scope}); | ||
|
||
let initedResolve = null; | ||
|
||
const inited = new Promise((resolve) => initedResolve = resolve); | ||
|
||
navigator.serviceWorker.addEventListener("message", (event) => { | ||
if (event.data.msg_type === "collAdded") { | ||
// the replay is ready to be loaded when this message is received | ||
initedResolve(); | ||
} | ||
}); | ||
|
||
const baseUrl = new URL(window.location); | ||
baseUrl.hash = ""; | ||
|
||
const msg = { | ||
msg_type: "addColl", | ||
name: "liveproxy", | ||
type: "live", | ||
file: {"sourceUrl": `proxy:${proxyPrefix}`}, | ||
skipExisting: false, | ||
extraConfig: { | ||
"prefix": proxyPrefix, | ||
"isLive": true, | ||
"baseUrl": baseUrl.href, | ||
"baseUrlHashReplay": true, | ||
"noPostToGet": true | ||
}, | ||
}; | ||
|
||
if (!navigator.serviceWorker.controller) { | ||
navigator.serviceWorker.addEventListener("controllerchange", () => { | ||
navigator.serviceWorker.controller.postMessage(msg); | ||
}); | ||
} else { | ||
navigator.serviceWorker.controller.postMessage(msg); | ||
} | ||
|
||
if (inited) { | ||
await inited; | ||
} | ||
|
||
window.addEventListener("load", () => { | ||
this.onHashChange(); | ||
}); | ||
|
||
window.addEventListener("hashchange", () => { | ||
this.onHashChange(); | ||
}); | ||
|
||
this.onHashChange(); | ||
} | ||
|
||
onHashChange () { | ||
const m = window.location.hash.slice(1).match(/\/?(?:([\d]+)\/)?(.*)/); | ||
|
||
const url = m && m[2] || "https://example.com/"; | ||
|
||
// don't change if same url | ||
if (url === this.url) { | ||
return; | ||
} | ||
|
||
let iframeUrl = `/w/liveproxy/mp_/${url}`; | ||
|
||
const iframe = document.querySelector("#content"); | ||
iframe.src = iframeUrl; | ||
|
||
this.url = url; | ||
|
||
window.location.hash = `#${url}`; | ||
} | ||
} | ||
|
||
new WabacLiveProxy().init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters