Skip to content

Instantly share code, notes, and snippets.

View pseudosavant's full-sized avatar

Paul Ellis pseudosavant

View GitHub Profile
@pseudosavant
pseudosavant / throttle.js
Created October 8, 2019 18:40
Really basic throttling function that returns a throttled function wrapped around the provided function
function throttle(fn, duration) {
var t;
return function(...args) {
clearTimeout(t);
t = setTimeout(fn.bind(null, ...args), duration);
}
};
@pseudosavant
pseudosavant / wifi-backup.bat
Created September 21, 2019 21:15
Windows commands for backing up and restoring the stored Wi-Fi networks
REM This command will export all wifi profiles into individual XML files
netsh wlan export profile key=clear
@pseudosavant
pseudosavant / createImageBitmap.js
Last active March 20, 2022 12:30 — forked from MonsieurV/createImageBitmap.js
createImageBitmap polyfill with support for CanvasImageSource (img, video, canvas) sources, Blobs, and ImageData. https://createimagebitmap-polyfill.glitch.me/
/*
* Polyfill for createImageBitmap
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap
*
* Supports CanvasImageSource (img, video, canvas) sources, Blobs, and ImageData.
*
* From:
* - https://dev.to/nektro/createimagebitmap-polyfill-for-safari-and-edge-228
* - https://gist.github.com/MonsieurV/fb640c29084c171b4444184858a91bc7
* Updated by:
@pseudosavant
pseudosavant / localStorage-data-usage.js
Last active September 20, 2019 21:02
Easy way to determine currently used space in localStorage
const totalSize = Object.values(localStorage).reduce((acc, c) => acc += c.length, 0);
const length = Object.values(localStorage).length;
console.log(`${length} entries using ${totalSize} bytes (${Math.floor(totalSize/length)} bytes/entry)`);
@pseudosavant
pseudosavant / index.html
Last active September 21, 2019 21:21
Test of using
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="message"></div>
<script src="script.js"></script
</body>
</html>
@pseudosavant
pseudosavant / searchItBookmarklet.js
Created May 23, 2019 21:58
Bookmarklet code for performing the same search you are already doing but on a different engine. Example: I search for 'shoes' on DuckDuckGo but I want to see if Bing or Google have more/better results.
(function(){
'use strict';
var engines = {
bing: 'https://www.bing.com/search?q=',
duckduckgo: 'https://duckduckgo.com/?q=',
google: 'https://www.google.com/search?q='
};
var engine = engines.duckduckgo; // Change to specify a different search engine
@pseudosavant
pseudosavant / jSugar.js
Last active May 24, 2024 01:07
Utility function that adds in some jQuery-like syntactic sugar
// jQuery-like syntactic sugar. Only queries for one element. Does not loop over multiple like jQuery
export function $(query) {
if (typeof query === 'undefined') throw 'No query provided to $';
var el;
if (typeof query.nodeType === 'string') {
el = query;
} else if (typeof query === 'string' && query[0] === '<') {
const container = document.createElement('div');
container.innerHTML = query;
@pseudosavant
pseudosavant / createColorDataURI.js
Created May 9, 2019 16:40
Utility function for generating 1px x 1px PNGs data URI with a solid color. Useful for things like setting `<video>` `poster` to a color since CSS can't style that
// Usage example: document.querySelector('video').poster = createColorDataURI(50,100,150);
function createColorDataURI(r, g, b, a) {
const canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
const context = canvas.getContext('2d');
const imageData = context.getImageData(0, 0, 1, 1);
const subPixels = imageData.data;
@pseudosavant
pseudosavant / jsonWithComments.js
Last active March 13, 2018 00:45
JSON with comments POC. Example here: http://jsbin.com/favarow/
// jshint esnext:true
/*
Every JSON entry is removed if the key is "_comment". "_comment" can optionally be followed
by underscores, letters, or digits to make the key more unique (e.g. "_comments_name", "_comments_todo").
The JSON is still valid JSON with the "_comment", and it will parse fine anywhere. It will
just have the extra "_comment" entries.
See example here: http://jsbin.com/favarow/
*/
@pseudosavant
pseudosavant / string-divider.js
Last active January 26, 2022 16:58
Divide any number/string every nth characters
const divide = (str, divider, n) => String(str)
.split('')
.reverse()
.reduce((acc, cv, i) => cv + (i % n === 0 ? divider : '') + acc);
let num = 4000000;
divide(num, '|', 2); // 4|00|00|00
divide(num, ',', 3); // 4,000,000
divide(num, '-', 1); // 4-0-0-0-0-0-0