File size: 5,677 Bytes
bee6636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
const scramjet = new ScramjetController({
files: {
wasm: "/scram/scramjet.wasm.wasm",
worker: "/scram/scramjet.worker.js",
client: "/scram/scramjet.client.js",
shared: "/scram/scramjet.shared.js",
sync: "/scram/scramjet.sync.js",
},
flags: {
serviceworkers: true,
syncxhr: true,
scramitize: true,
},
});
scramjet.init();
navigator.serviceWorker.register("./sw.js");
const connection = new BareMux.BareMuxConnection("/baremux/worker.js");
const flex = css`
display: flex;
`;
const col = css`
flex-direction: column;
`;
connection.setTransport(store.transport, [{ wisp: store.wispurl }]);
function PlaygroundApp() {
this.css = `
width: 100%;
height: 100%;
color: #f0fef4;
display: flex;
padding: 0.5em;
box-sizing: border-box;
gap: 0.5em;
.codesplit {
width: 50%;
height: 100%;
display: flex;
flex-direction: column;
gap: 0.5em;
}
.mcontainer {
background: #1e1e1e;
h2 {
margin: 0.1em;
}
border: 1px solid #313131;
flex-basis: 100%;
display: flex;
flex-direction: column;
}
.monaco {
flex: 1;
}
.frame {
height: 100%;
display: flex;
flex-direction: column;
gap: 0.5em;
iframe {
width: 100%;
border: 1px solid #313131;
}
}
.config {
border: 1px solid #313131;
background: #1e1e1e;
padding: 0.5em;
}
`;
this.fakeorigin = "https://sandboxedorigin.com";
this.mount = async () => {
const monaco = await import(
"https://cdn.jsdelivr.net/npm/monaco-editor/+esm"
);
const dedent = (await import("https://cdn.jsdelivr.net/npm/dedent/+esm"))
.default;
monaco.editor.setTheme("vs-dark");
const html = monaco.editor.create(this.htmlbox, {
value: dedent`
<html>
<head>
<!-- all resources are intercepted by the service worker -->
<link rel="stylesheet" href="/style.css"></link>
<script src="/script.js"></script>
<!-- external resources go through WISP (check network tab) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<h1>Scramjet Sandbox Playground</h1>
<p>
Scramjet allows any webpage to be run on the same origin in an isolated manner
</p>
<button onclick="checkOrigin()">Test emulated origin</button>
<button onclick="loadResource('https://example.com/')">Load assets through sandbox</button>
<br><br>
<h2>iframe test</h2>
<button onclick="loadiframe()">test iframe nesting</button>
<br>
</body>
</html>
`,
language: "html",
automaticLayout: true,
});
const css = monaco.editor.create(this.cssbox, {
value: dedent`
/* resources loaded by css are intercepted by service worker */
@import url('https://fonts.googleapis.com/css2?family=Hind:wght@300;400;500;600;700&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
body, html {
background: #1e1e1e;
color: white;
width: 100%;
height: 100%;
font-family: "Roboto";
}
iframe {
zoom: 0.75;
width: 50%;
height: 50%;
}
`,
language: "css",
automaticLayout: true,
});
const js = monaco.editor.create(this.jsbox, {
value: dedent`
function checkOrigin() {
// real origin is hidden from the page
alert("origin: " + window.origin);
}
// external resources fetched will be re-
// directed to the WISP server
function loadResource(url) {
fetch(url).then(r => {
console.log("loaded", r);
})
}
function loadiframe() {
if (document.getElementById("nested-frame")) return;
let frame = document.createElement("iframe");
frame.id = "nested-frame";
frame.src = "https://google.com";
document.body.appendChild(frame);
}
`,
language: "javascript",
automaticLayout: true,
});
const recompile = async () => {
(await navigator.serviceWorker.ready).active.postMessage({
type: "playgroundData",
html: html.getValue(),
css: css.getValue(),
js: js.getValue(),
origin: this.fakeorigin,
});
this.frame.src = scramjet.encodeUrl(this.fakeorigin);
};
recompile();
html.getModel().onDidChangeContent(recompile);
css.getModel().onDidChangeContent(recompile);
js.getModel().onDidChangeContent(recompile);
};
return html`
<div>
<div class="codesplit">
<div class="mcontainer">
<h2>HTML</h2>
<div class="monaco" bind:this=${use(this.htmlbox)}></div>
</div>
<div class="mcontainer">
<h2>CSS</h2>
<div class="monaco" bind:this=${use(this.cssbox)}></div>
</div>
<div class="mcontainer">
<h2>JS</h2>
<div class="monaco" bind:this=${use(this.jsbox)}></div>
</div>
</div>
<div class="frame" style="flex: 1">
<iframe style="flex: 1" bind:this=${use(this.frame)}></iframe>
<div class="config">
<h1>Config</h1>
<div>
<label>fake origin:</label>
<input bind:value=${use(this.fakeorigin)} />
<label>wisp server:</label>
<input bind:value=${use(store.wispurl)} />
</div>
</div>
</div>
</div>
`;
}
window.addEventListener("load", async () => {
const root = document.getElementById("app");
try {
root.replaceWith(h(PlaygroundApp));
} catch (e) {
root.replaceWith(document.createTextNode("" + e));
throw e;
}
});
|