Skip to content

Instantly share code, notes, and snippets.

@c-kick
c-kick / doubleClickHandler.js
Last active May 12, 2023 12:03
doubleClickHandler - handles double-click events on elements
View doubleClickHandler.js
/**
* Handles the click event to detect single or double clicks.
* @param {MouseEvent} event - The click event object.
*/
function doubleClickHandler(event) {
const target = event.target;
if (new Date().getTime() - (target._lastTouch || 0) > 500) {
// Not a double click
target._lastTouch = new Date().getTime();
@c-kick
c-kick / hnl.isVisible.mjs
Created May 8, 2023 11:31
Check if an element is visible
View hnl.isVisible.mjs
/** isVisible
*
* Determines whether an element is visible within the (specified, or Window) viewport. Executes the callback based
* on that result. The callback is called with two parameters: a boolean that's true if the element is visible, along
* with the element's bounding box, including 'pageY' and 'pageX' which contain the element's position as relative to
* the whole document. Useful for scrolling into view.
*
* The viewport can be omitted, specified partially or completely.
*
* Usage: isVisible(myElement, callback, {viewport object});
@c-kick
c-kick / debouncer.mjs
Created March 14, 2023 13:43
Rate-limiter/debouncer module for JavaScript. See https://code.hnldesign.nl/demo/hnl.debounce.html
View debouncer.mjs
/**
*
* debounceThis ES6 module v1.2 (03-2023)
* Debounces/rate-limits the provided function (callback)
*
* Example usage:
*
* import {debounceThis} from "debouncer.mjs";
*
* window.addEventListener('scroll', debounceThis((e)=> {
@c-kick
c-kick / schedule_booleans.yaml
Last active October 31, 2022 09:59
Automation which uses schedules to set two indicators (booleans): a morning schedule and a home indicator. Also provides schedule/calendar overrides. See description for usage.
View schedule_booleans.yaml
#Blueprint by Klaas Leussink / hnldesign.nl (c) 2022
blueprint:
name: Set schedule-related booleans
source_url: https://gist.github.com/c-kick/b4dcdae9d9fa85ca8c51cdedd7293bf5
description: >-
This automation uses schedules to set two indicators (booleans): a morning schedule (useful for short morning routines on working days) and a home indicator (useful for indicating people are home, until they go to bed). It also includes an option to use a holiday calendar, which sets the home indicator on the specified start and end times if the current day is a holiday. Another option is a vacation override, which - when set to active - ignores *all* schedules/calendars and uses the specified 'vacation schedule' instead.
domain: automation
input:
morning_routine_schedules:
name: Morning schedule(s)
@c-kick
c-kick / motion_light_with_override.yaml
Last active October 31, 2022 09:41
A Home Assistant blueprint that can be used to have a motion sensor switch on (a) device(s), but allow the turning off procedure (after no motion is detected anymore) to be overridden by a switch/button. Useful if you need to activate a light upon entry, and turn off after no motion (most cases), but keep the option to force the light to stay on.
View motion_light_with_override.yaml
#Blueprint by Klaas Leussink / hnldesign.nl (c) 2022
blueprint:
name: Motion Light with override
source_url: https://gist.github.com/c-kick/32234ea474f6b854a861853b17b881ce
description: >-
This blueprint can be used to have a motion sensor switch on (a) device(s), but allow the turning off procedure (after no motion is detected anymore) to be overridden by a (ZHA) switch/button. Useful if you need to activate a light upon entry, and turn off after no motion (most cases), but keep the option to make the light stay on if you're planning on spending more time in the area (e.g. a shed).
Note: if you need to know what is happening, uncomment the debug message lines.
domain: automation
input:
motion_sensor:
@c-kick
c-kick / arc-vt.js
Last active October 19, 2022 08:59
This nodejs script is originally aimed at (Plex) LG OLED users that don't have DTS decoding. It auto-converts audio in video files to something other than DTS (AC3) if applicable (see description in file)
View arc-vt.js
#!/usr/bin/env node
/*
ARC-VT: Audio-stream Reorganizer, Converter & Video Transcoder by Klaas Leussink / hnldesign (C) 2022-2023
This nodejs script is originally aimed at non-DTS LG OLED owners running Plex on a Synology Diskstation.
The problem being that Plex will stubbornly offer the DTS track for playback, producing no sound. And when
asked to convert, playback is choppy/hangs every x seconds. The most solid solution is to make sure the
first available audio stream is a non-DTS (e.g. AC3) codec. This script automates that process:
@c-kick
c-kick / zha_ikea_tradfri_4button_remote_color.yaml
Last active October 31, 2022 09:59 — forked from orcema/zha_ikea_tradfri_4button_remote_color.yaml
blueprint zha_ikea_tradfri_4button_remote_color
View zha_ikea_tradfri_4button_remote_color.yaml
#Blueprint by Klaas Leussink / hnldesign.nl (c) 2022
blueprint:
name: ZHA - IKEA TRADFRI - 4 Button Remote - Color Lights
source_url: https://gist.github.com/c-kick/299999c9512c4260fe6de1cd843efdde
description: This automation simulates the use of the IKEA TRADFRI remote control
connected through ZHA. Original blueprint by orcema, modified by hnldesign/c-kick,
- added brightness and color control inputs (for usage with IKEA TRADFRI bulbs)
- more fine-grained dimming
- color_temp changing
domain: automation
@c-kick
c-kick / hnl.stickyStuck
Last active June 23, 2023 13:06
adds class 'stuck' to elements that are stuck. Uses Bootstraps' `position-sticky` class as a selector.
View hnl.stickyStuck
const stickes = document.body.querySelectorAll('.position-sticky');
window.addEventListener('scroll', function(){
stickes.forEach(function(el){
let stuck = false;
const rect = el.getBoundingClientRect();
if (window.getComputedStyle(el).bottom == 'auto') {
stuck = rect.top === parseInt(window.getComputedStyle(el).top, 10);
} else if (window.getComputedStyle(el).top == 'auto') {
stuck = rect.height + rect.top < window.innerHeight;
@c-kick
c-kick / hnl.setBreakpoints.js
Last active March 23, 2022 10:11
setBreakpoints - Adds the current breakpoint-name (as specified in the 'breakpoints' const) to the body class and notifies anyone listening of the change via the 'breakPointChanged' event.
View hnl.setBreakpoints.js
/** This script adds the current breakpoint-name (as specified in the 'breakpoints' const)
* to the body class and notifies anyone listening of the change via the 'breakPointChanged' event.
*
* Default breakpoint names and cutoff pixel values taken from Bootstrap 5's default responsive breakpoints:
* https://getbootstrap.com/docs/5.0/layout/breakpoints/
*
* Usage:
* - include the script into your page and the class is updated, and will continue to update whenever
* the width of the window changes enough to trigger another breakpoint.
* - optionally listen for the event via:
@c-kick
c-kick / csp-generator.php
Last active December 10, 2020 20:55
Generates a Content Security Policy using easy definable arrays. See code docs for example.
View csp-generator.php
/**
* CSP Generator v0.2 (C) 2020-10-12 hnldesign.nl / Klaas Leussink
* @param array $rules
* Rules, specified as an array with rulesets (array) or rules (string). Note that 'hosts' are specified as an array inside the ruleset:
* note: it's easier to define hosts in the $domains array (see second param)
* array(
* 'default-src' => array (
* 'self',
* 'data',
* 'unsafe-eval',