Skip to content

Instantly share code, notes, and snippets.

const $ = (q, container = document) => [...container.querySelectorAll(q)];
$.createFragment = Range.prototype.createContextualFragment.bind(
document.createRange()
);
$.attr = (el, attrs) => {
for (let prop in attrs) {
HTMLElement.prototype.hasOwnProperty(prop)
? (el[prop] = attrs[prop])
const randomBetween = (min, max) =>
Math.floor(Math.random() * (max - min + 1) + min)
const createEventBus = () => {
const subscribers = {}
let counter = 0
const on = (name, cb) => {
if (!subscribers[name]) subscribers[name] = {}
subscribers[name][++counter] = cb
return btoa(name + '.' + counter)
}
@Ivannnnn
Ivannnnn / onLocationChange.js
Created October 10, 2019 09:01
Subscribe to url change
const onLocationChange = (() => {
const subscribers = []
const fireEvent = () => subscribers.forEach(sub => sub())
const { pushState, replaceState } = window.history
window.history.pushState = function(...args) {
pushState.apply(window.history, args)
fireEvent()
}