Skip to content

Instantly share code, notes, and snippets.

@kafene
kafene / promise-script-loader.md
Last active August 29, 2015 14:22
Using Promises to load scripts

I had a few of scripts I want to load in my userscript before executing it. After some head scratching and messing around with Promises it turns out it's quite easy. This way there's no need to require a larger module loader and other dependencies, which can be a pain to work with when writing userscripts.

(function load(scripts) {
    // Wait for DOMContentLoaded/window.onload
    if (document.readyState !== "interactive" && document.readyState !== "complete") {
        document.addEventListener("DOMContentLoaded", load.bind(null, scripts));
        return;
    }
@kafene
kafene / insertAdjacentElement.md
Last active August 29, 2015 14:18
Element.prototype.insertAdjacentElement shim

This is a shim for Element.prototype.insertAdjacentElement, which is supported all major browsers except Firefox.

Example: https://jsfiddle.net/kafene/s6Lweg5k/

if ("undefined" === typeof (Element.prototype.insertAdjacentElement)) {
    Object.defineProperty(Element.prototype, "insertAdjacentElement", {
        enumerable: false,
        writable: true,
        configurable: true,
###
####### HISTIGNORE #######
###
# examples from interwebs, noob dot file notes
#export HISTIGNORE='pwd:exit:fg:bg:top:clear'
# (if try to erasedups do not ignore things want 2 pune but on OSX it no workie)
# ignore things that start with a space, and ignore the exit command
#HISTIGNORE='[ \t]*:exit'
# some slashdot dudes says
#export HISTIGNORE="&:ls:[bf]g:exit:pwd:clear:mount:umount:[ \t]*"
@kafene
kafene / monolog-pimple-whoops-examples.php
Last active August 7, 2024 05:22
Set up some Monolog handlers, a Pimple Container, and Whoops
<?php
/**
* Various configurations of Monolog and Whoops
* using a Pimple Container as service locator
*
* Monolog: https://github.com/Seldaek/monolog
* Pimple: https://github.com/fabpot/Pimple
* Whoops: https://github.com/filp/whoops
* Swift Mailer: https://github.com/swiftmailer/swiftmailer
@kafene
kafene / ifsetor.md
Created August 2, 2014 15:35
PHP 5.6+ ifsetor/coalesce userspace implementation

Ref: https://wiki.php.net/rfc/ifsetor

So I found out something sad: the variadics RFC implementation was altered so that ...$arg can only come last.

And since we can't pass a non-variable default value by reference, it can go first, just kind of confusing looking, unfortunately.

<?php

function coalesce($default, &amp;...$args) {
@kafene
kafene / distances_slc_haversine.php
Created July 25, 2014 06:56
distance calculations using Spherical Law of Cosines and Haversine Formula
<?php
# Spherical Law of Cosines
function distance_slc($lat1, $lon1, $lat2, $lon2) {
$a = sin(deg2rad($lat1)) * sin(deg2rad($lat2));
$b = cos(deg2rad($lat1)) * cos(deg2rad($lat2));
$c = cos(deg2rad($lon2 - $lon1));
$distance = rad2deg(acos($a + $b * $c));
return round($distance * 60 * 1.1515, 4);
}
@kafene
kafene / com.ubuntu.desktop.pkla
Created July 24, 2014 10:12
/var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla (see: policykit-desktop-privileges)
[Mounting, checking, etc. of internal drives]
Identity=unix-group:admin;unix-group:sudo
Action=org.freedesktop.udisks.filesystem-*;org.freedesktop.udisks.drive-ata-smart*;org.freedesktop.udisks2.filesystem-mount-system;org.freedesktop.udisks2.encrypted-unlock-system;org.freedesktop.udisks2.filesystem-fstab;
ResultActive=yes
[Change CPU Frequency scaling]
Identity=unix-group:admin;unix-group:sudo
Action=org.gnome.cpufreqselector
ResultActive=yes
@kafene
kafene / handlers.js
Created July 24, 2014 06:57
protocol and content handlers for web services
/**
* Sadly neither Chrome or Firefox fully support the functionality herein.
* Chrome supports only registerProtocolHandler, and Firefox supports
* a limited set of content handlers - at the moment it's pretty much
* just RSS feeds. These do work great in the older Opera 12 though.
*
* I've moved on from the abandoned Opera to Firefox but I put these
* here for future reference, hopefully browsers implement both
* navigator.registerProtocolHandler and navigator.registerContentHandler
* fully in the near future.