Skip to content

Instantly share code, notes, and snippets.

View wanderview's full-sized avatar

Ben Kelly wanderview

View GitHub Profile
@wanderview
wanderview / sw.js
Last active January 29, 2021 08:16
Example "no-op" navigation preload service worker
self.addEventListener('fetch', evt => {
// Fallback for subresource requests or in browsers that do not
// support navigation preload.
if (evt.request.mode !== "navigate" || !evt.preloadResponse)
return;
evt.respondWith(async _ => {
// Try to get the navigation preload response.
let r = await evt.preloadResponse;
@wanderview
wanderview / gist.cpp
Created June 25, 2018 14:21
firefox CSP impl gist
//
// nsILoadInfo.idl
//
interface nsILoadInfo
{
// CSP data that was parsed from the LoadInfo's channel. This is purely
// for communicating the value back to the client that triggered the
// load. This should only be set for non-subresource loads. This
// should not be used for enforcing CSP on subresource loads. Instead
// use GetClientInfo() and the CSP value there.
@wanderview
wanderview / frame.html
Created October 26, 2017 13:02
Common about:blank replacement case
<!DOCTYPE html>
<html>
<script>
// self.bar === 'foobar'
</script>
</html>
@wanderview
wanderview / sri-sw.js
Created October 17, 2017 14:54
SRI debugging service worker
self.skipWaiting();
addEventListener('fetch', evt => {
evt.respondWith(async function() {
try {
let response = await fetch(evt.request);
console.log(`==> SUCCESS for url:${evt.request.url} integity:${evt.request.integrity}`);
return response;
} catch(e) {
console.log(`==> FAILURE for url:${evt.request.url} integity:${evt.request.integrity}`);
let headers = new Headers();
headers.append('X-Food', 'Candy Corn');
headers.append('X-Drink', 'Cider');
let request = new Request('/order.html');
autoHeaders.put(request, headers);
void
AssertAndSuppressCleanupFunc(ErrorResult* aRv)
{
aRv->AssertReportedOrSuppressed();
aRv->SuppressException();
}
template <typename CleanupFunc = AssertAndSuppressCleanupFunc>
class ErrorResult
{
@wanderview
wanderview / index.html
Created November 22, 2015 03:57
data URI baseURL
<html>
<body>
<script>
var s = document.createElement('script');
s.src = 'data:text/javascript,console.log("data URI baseURI: " + document.currentScript.baseURI);'
document.body.appendChild(s);
console.log('### ### script with ' + s.src);
</script>
</body>
</html>
@wanderview
wanderview / index.html
Last active October 14, 2015 19:09
PoC to extend service worker lifetime past end of fetch event by (ab)using postMessage()
<html>
<head></head>
<body>
<script>
navigator.serviceWorker.addEventListener('message', function(evt) {
if (evt.data === 'CALL ME') {
navigator.serviceWorker.controller.postMessage('RING RING');
}
});
navigator.serviceWorker.register('sw.js');
myStream.read(function handleChunk(chunk) {
if (!chunk) {
processDone();
return;
}
processChunk(chunk);
myStream.read(handleChunk);
});
@wanderview
wanderview / gist:ac6052184c62d2f165ee
Created April 6, 2015 18:23
Request body writing as a stream
var req = new Request('https://example.com' { bodyAsWriter: true });
// throws because writer is not set yet
req.bodyWriter.write(/*...*/);
// fetch calls setWriter
fetch(req).then(/*...*/);
req.bodyWriter.write(/*...*/)