Skip to content

Instantly share code, notes, and snippets.

@softwarespot
softwarespot / adblockerDetectAsync.js
Last active April 22, 2019 07:54
Detect if an adblocker exists
/**
* Detects if an adblocker exists
*
* @example
* adblockerDetectAsync()
* .then(() => console.log('Detected adblocker:', hasAdblocker))
* .catch((err) => console.error('[adblockerDetectAsync]', err));
*
* @returns {Promise<boolean>} Returns a Returns a {@link Promise|Promise} that resolves with either true, if an adblocker exists; otherwise, false
*/
@softwarespot
softwarespot / createProcessingQueue.js
Last active March 29, 2019 14:16
Create an a queue that can be pushed to and processed asynchronously and in chunks
// Found similar article URL: https://humanwhocodes.com/blog/2009/08/11/timed-array-processing-in-javascript/
function createProcessingQueue(onHandleItem, onHandleTimeout, timeout = 50) {
const state = {
queue: [],
timerId: 0,
}
const api = {
push(...args) {
@softwarespot
softwarespot / promise.js
Last active March 26, 2019 06:05
Non-spec compliant Promise implementation
// NOTE: The following is a non-spec compliant Promise implementation
// Utils
const asyncRun = createAsyncRunner()
const isFunction = fn => typeof fn === 'function'
const isObject = obj => Object(obj) === obj
const noop = () => {}
const STATE_PENDING = 'pending'
@softwarespot
softwarespot / queue.js
Last active June 14, 2018 20:43
Simple queue implementation
// For Quokka, as localStorage is not available in Node.js
const localStorage = {
getItem(name) {
return localStorage[name];
},
setItem(name, value) {
localStorage[name] = value;
},
};
@softwarespot
softwarespot / window.onerror.js
Created May 27, 2018 17:53
Listen for uncaught errors using window.addEventListener('error', ...)
<script>
window.addEventListener('error', (event) => {
const { message, filename, lineno, colno, error } = event;
console.log('Captured uncaught error:', message, filename, lineno, colno, error.stack);
});
setTimeout(() => {
try {
throw new Error('An unexpected error occurred (2)');
} catch (ex) {
@softwarespot
softwarespot / guard.js
Last active May 29, 2018 20:39
Guard a function and execute a function guarded with optional zone information
// Guard a function, with optional zone information.
// Note: Any function which is guarded inside an already guarded function
// and does not provide any zone information, will inherit from the outer
// guarded function
const defaultName = '<root>';
guard._zone = {
name: defaultName,
// Hidden property, due to being prefixed with "$"
@softwarespot
softwarespot / zone.js
Last active May 23, 2018 07:55
Basic zones implementation
// https://github.com/angular/zone.js/blob/2b3779d39a8f1930f59911c0d7e9e827f0fb5bc9/zone.js
// https://github.com/angular/zone.js/blob/d40a9376a825c702335d03e4122be8518e636791/zone.js
// https://github.com/angular/zone.js/blob/bee8a60d918fd6d737f8f525cb8d886f69bf06af/lib/zone.ts
// https://github.com/angular/zone.js/blob/3ad2445da420da2ede9cdb28c62986e86694a6fe/lib/core.ts
var Zone = (function Zone() {
// https://domenic.github.io/zones/#sec-zone-constructor
function Zone(parent, zoneSpec) {
zoneSpec = zoneSpec !== undefined ? zoneSpec : {};
@softwarespot
softwarespot / ideas.js
Last active November 14, 2017 04:42
Ideas
https://github.com/Microsoft/monaco-editor
https://github.com/fczbkk/css-selector-generator
https://github.com/ericclemmons/unique-selector
https://github.com/bertyhell/cheerio-get-css-selector/blob/master/src/get-unique-selector.js
https://github.com/rollup/rollupjs.org/blob/f717def625241f4f70810fdc4b07cc0d6ed80b44/universal/routes/Repl/index.html
https://preactjs.com/
@softwarespot
softwarespot / app.redux.js
Created November 6, 2017 09:42
Basic redux implementation
/* eslint-disable */
var app = {
utils: {
isArray() {
return Array.isArray
},
each(arr, fn) {
return arr.forEach(fn);
},
@softwarespot
softwarespot / htmlParse.js
Created October 2, 2017 20:04
Parse HTML
const parse5 = require('parse5');
const document = parse5.parse(`
<!DOCTYPE html>
<html>
<body>
<h1 "j"="k">My First Heading</h1>
<script>var x = 1
console.log('')