Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@Rich-Harris
Rich-Harris / service-workers.md
Last active March 21, 2025 11:42
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.

I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.

Use Canary for development instead of Chrome stable

Chrome 51 has some pretty wild behaviour related to console.log in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.

@WebReflection
WebReflection / days-since.js
Last active January 12, 2022 02:50
Days since another date
function date(year, month, day, hours, minutes, seconds, ms) {
var today = new Date();
today.setYear(year);
today.setMonth(month - 1);
today.setDate(day);
today.setHours(hours || 0);
today.setMinutes(minutes || 0);
today.setSeconds(seconds || 0);
today.setMilliseconds(ms || 0);
return today;
@WebReflection
WebReflection / 2gif.sh
Created September 1, 2016 11:36
simple ffmpeg based script to convert a generic video file to a gif
#!/usr/bin/env bash
#$ 2gif video.mp4 # creates video.gif
#$ 2gif video.mp4 out.gif # creates out.gif
extension2gif() {
local name="$1"
echo "${name%.*}.gif"
}
@WebReflection
WebReflection / packages-info.md
Last active September 20, 2016 10:03
A simple way to understand multiple dependencies between different NodeJS projects.

This snippet helps understanding packages dependencies and versions across different projects/folders.

You can pass 2 or more folders to compare their package.json

You can also pass just one fodler to check all modules in it (example: a node_modules folder)

You can pass nothing to analize the current list of packages.

#!/usr/bin/env node
@WebReflection
WebReflection / base64.js
Last active April 29, 2017 02:44
Quite possibly the smallest base 64 utility out there.
// base64.decode(base64.encode('💩'));
// Browsers
const base64 = {
encode: str => btoa(unescape(encodeURIComponent(str))),
decode: str => decodeURIComponent(escape(atob(str)))
};
/* ES3 compat version
var base64 = {
// https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce
Date.prototype.toDatetimeLocal =
function toDatetimeLocal() {
var
date = this,
ten = function (i) {
return (i < 10 ? '0' : '') + i;
},
YYYY = date.getFullYear(),
MM = ten(date.getMonth() + 1),
@WebReflection
WebReflection / randommac.sh
Last active March 20, 2018 13:27
Getting free wifi on Linux too
#!/usr/bin/env bash
# @example
# chmod a+x randommac.sh
# ./randommac.sh wlp2s0
# @credits original macOS / osX version via @rem
# https://remysharp.com/2017/05/29/getting-free-wifi
if [ "$1" != "" ]; then
@WebReflection
WebReflection / benchmark-dom-classes.js
Last active November 13, 2021 20:52
Classes VS DOM Events Handling Benchmark
// Players
class ClickCounter {
constructor() { this.clicks = 0; }
onclick(e) { this.clicks += (e.type === 'click') ? 1 : -1; }
}
class Handler extends ClickCounter {
constructor(currentTarget) {
super();
currentTarget.addEventListener('click', this);
@WebReflection
WebReflection / hyperhtml-firebase.html
Last active December 16, 2019 08:15
A basic hyperHTML + Firebase example. 100% client side.
<!doctype html>
<html lang="en">
<head>
<title>hyperHTML &amp; Firebase</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
html { font-family: sans-serif; }
html, body, ul, p { padding: 0; margin: 0; }
ul, li { list-style: none; }
@WebReflection
WebReflection / template-content.js
Created August 30, 2017 08:30
micro shim for template content
(function (document, TEMPLATE) {
var container = document.createElement(TEMPLATE);
if ('content' in container) return;
var createElement = document.createElement;
var descriptor = {
get: function () { return this.content ? this.content.innerHTML : ''; },
set: function (html) {
var el = createElement.call(document, TEMPLATE);
if (/^[^\S]*?<(t(?:head|body|foot|r|d|h))/i.test(html)) {
var selector = RegExp.$1;