225 points · by MagicPropmaker · 7y ago · 35 comments
navigator.getUserMedia = navigator.getUserMedia ||
navigator.mozGetUserMedia;
window.onload = () => {
// Create favicon link element
const favicon = document.createElement('link');
favicon.rel = 'shortcut icon';
favicon.type = 'image/png';
favicon.href = '../../images/favicon.ico';
document.getElementsByTagName('head') . [0].appendChild(favicon);
// Create hidden canvas
const w = 32;
const h = 32;
const canvas = document.createElement('canvas');
canvas.style = 'display: none';
canvas.width = w;
canvas.height = h;
document.body.appendChild(canvas);
// Grab canvas context
const ctx = canvas.getContext('2d');
// Create hidden video element
const video = document.createElement('video');
video.style = 'display: none';
video.width = canvas.width;
video.height = canvas.height;
document.body.appendChild(video);
// Assign user media to video and start loop
navigator.getUserMedia({video: true}, stream => {
video.srcObject = stream;
video.play();
loop();
}, () => {});
// Loop forever
const loop = () => {
// Copy video to canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Set canvas to favicon
favicon.setAttribute('href', canvas.toDataURL());
// Loop
setTimeout(loop, 100);
};
};
From https://github.com/wybiral/code-art/blob/master/projects/tin...Basically the favicon connection to the server stays open sending a periodic junk-update so that the server can detect when the tab is closed. You can do it with any asset but the favicon hides the loading indicator.
Is there an equivalent with any other type of resource, that avoids deferring onLoad?
If JS is disabled the event doesn't really matter and any asset can be handled this way. I just use the favicon because it stops the browser from showing a loading spinner so everything looks still and nobody assumes there's anything going on in the background.
You can use persistent connections like this in other strange ways too. For instance this is a no-JS chat that's even able to update the connection count by sending a bit of CSS lazily: https://harmless.herokuapp.com/main
https://mobile.twitter.com/jake_albaugh/status/1118611365508...
In the initial setup:
ctx.scale( -1, 1 );
In the drawing loop: ctx.drawImage( video, 0, 0, canvas.width * -1, canvas.height );
Something like that anyway. We have guests arriving soon, so no time to test right now, but if anyone tries it I will be curious!It basically keeps sending iframe's with data URL's for download, see code: https://github.com/wybiral/crash
On Chrome it blocks request popups after the first but FF usually crashes (even Tor Browser).
The interesting thing about this is that you can keep the connection open as long as you want and then send the iframes on command.
What do you mean?