Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pwFoo
pwFoo / Service_Workers_Messaging.md
Created October 22, 2023 12:55 — forked from Jonarod/Service_Workers_Messaging.md
Describes how to send messages between a page's main thread and a service worker thread

Page to ServiceWorker

// in page.html
navigator.serviceWorker.controller.postMessage({'hello':'world'});
// in sw.js
self.addEventListener('message', event => { 
@pwFoo
pwFoo / index.html
Created August 8, 2023 07:10 — forked from Dletta/index.html
Gun in Service Worker
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Concept Chat</title>
<link rel="stylesheet" href="index.css" >
</head>
<body>
@pwFoo
pwFoo / minimal_pubsub.js
Created July 31, 2023 06:33 — forked from sivagao/minimal_pubsub.js
minimal pubsub js
var pubsub = function(l, u, r, i) { // cool! 闭包并且初始化vars
return function(n, f) {
r = l[n] = l[n] || [], i = -1;
if (f && f.call) r.push(f);
else while (r[++i]) r[i].apply(u, arguments);
}
}({});
// subscribe to event
pubsub("eat_cookie", function() {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>capture microphone audio into buffer</title>
</head>
<body>
<audio id="player" controls></audio>
<input
type="text"
id="username"
@pwFoo
pwFoo / custom-element-minimal.js
Created October 21, 2022 18:20 — forked from drodsou/custom-element-minimal.js
minimal custom element - web component, webcomponent
/* non-shadow dom minimal custom element */
customElements.define("counter-ce", class extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<button>
${this.innerHTML} <span></span>
</button>
`;
this.update = ()=>{
@pwFoo
pwFoo / proxyftw.js
Created February 28, 2022 19:44 — forked from stelf/proxyftw.js
using ES6 proxies and async/await to dynamically, yet almost transparently connect to some remote data provider
let providerHandler = {
get: async(target, name) => {
console.log('load someting from remote...')
return new Promise( (res, rej) => {
setTimeout(() => res(42), 4200)
})
},
set: function (obj, prop, value) {
return new Promise((res, rej) => {
@pwFoo
pwFoo / jkq.js
Created February 27, 2022 19:00 — forked from jakerella/jkq.js
An ultra-light, jQuery-like micro-library for selecting DOM elements and manipulating them.
(function() {
'use strict';
/**
* Core method, similar to jQuery (only simpler)
*
* @param {String|HTMLElement} s The CSS selector to search for or HTML element to wrap with functionality
* @param {HTMLElement} root OPTIONAL An HTML element to start the element query from
* @return {Array} The collection of elements, wrapped with functionality (see API methods)
*/
@pwFoo
pwFoo / mo_vs.proxy.js
Created February 26, 2022 11:23 — forked from ebidel/mo_vs.proxy.js
MutationObserver vs. Proxy to detect .textContent changes
<!--
This demo shows two ways to detect changes to a DOM node `.textContent`, one
using a `MutationObserver` and the other using an ES2015 `Proxy`.
From testing, a `Proxy` appears to be 6-8x faster than using a MO in Chrome 50.
**Update**: removing the `Proxy` altogether speeds up the MO to be inline with the Proxy.
This has something to do with how the browser queues/prioritizes Proxies over MO.
@pwFoo
pwFoo / alpine-virtual-desktop.sh
Created January 11, 2022 21:14 — forked from misterunknown/alpine-virtual-desktop.sh
Alpine Linux: Virtual Desktop installation
#!/bin/sh
#
# This script installs a virtual desktop using Xvfb, x11vnc, mate-desktop,
# mate-session-manager and Apache Guacamole. It runs on Alpine Linux Edge.
#
# See also:
# https://www.reddit.com/r/selfhosted/comments/b6k8go/poc_a_desktop_in_a_container_on_a_server/
# This is the user, under which the MATE desktop will run
# Notice: For several reasons this shouldn't be root
@pwFoo
pwFoo / dom-events.js
Created January 8, 2022 21:06 — forked from jhthorsen/dom-events.js
emit, on, once in pure javascript
Object.prototype.emit = function(name, d) {
this.dispatchEvent(new CustomEvent(name, { detail: d }));
return this;
};
Object.prototype.on = function(name, cb) {
this.addEventListener(name, cb, false);
return cb;
};
Object.prototype.once = function(name, cb) {
var wrapper = function() {