Skip to content

Instantly share code, notes, and snippets.

View Offirmo's full-sized avatar
⚔️
Coding a RPG… (as a hobby)

Offirmo Offirmo

⚔️
Coding a RPG… (as a hobby)
View GitHub Profile
@Offirmo
Offirmo / navigation.js
Last active October 5, 2017 05:59
[Intercepting navigation in JavaScript] #JavaScript #browser #growth
function hookNavigationEvent(onNavigate) {
// always provide a full location object to the final callback
function uniformizedOnNavigate(locationOrUrl) {
try {
const location = (typeof locationOrUrl === 'string')
? new URL(
locationOrUrl[0] === '/' // relative
? document.location.origin + locationOrUrl
: locationOrUrl
@Offirmo
Offirmo / umd.js
Last active August 29, 2017 01:33
[Improved UMD template to work with webpack 2] #JavaScript #umd #webpack
// Iterating on the UMD template from here:
// https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
// But experimentally improving it so that it works for webpack 2
// UMD template from https://gist.github.com/Offirmo/ec5c7ec9c44377c202f9f8abcacf1061#file-umd-js
(function (root, factory) {
var LIB_NAME = 'Foo'
if (typeof define === 'function' && define.amd) {
@Offirmo
Offirmo / lib.js
Last active December 2, 2018 07:51
[browser micro-lib] #JavaScript #browser #growth #frontend
https://github.com/terkelg/facon
// in Offirmo monorepo!
function poll(predicate, options) {
// early check to save an initial poll period
let result = predicate();
if (result)
return Promise.resolve(result);
@Offirmo
Offirmo / lib.js
Last active July 7, 2020 03:34
[useful browser snippets] #JavaScript #browser #growth #frontend
// debg
let DEBUG = false
try { // defensive!
DEBUG = DEBUG || !!window.localStorage.getItem(`${APP}.debug`)
} catch (e) { /* swallow */ }
if (DEBUG) console.info(`Hello from ${APP}`)
// prepare an url
// https://devdocs.io/javascript/global_objects/encodeuri
@Offirmo
Offirmo / singleton.js
Last active December 2, 2018 07:54
[global browser singleton] #JavaScript #browser #growth #frontend
var global_module_instance;
Object.defineProperty(window, 'global_ng_module', {
enumerable: true, // why not ?
set: function() {
throw new Error('You can’t assign window.global_module !');
},
get: function() {
if(global_module_instance) return global_module_instance; // already OK
global_module_instance = angular.module('global_ng_module', [
'ui.bootstrap'
@Offirmo
Offirmo / es6.js
Last active March 28, 2018 14:12
[rare ES6 stuff I forget all the time] #JavaScript
// imports
import { Random, Engine } from '@offirmo/random'
import {
State as MetaState,
factory as meta_state_factory,
} from '@oh-my-rpg/state-meta'
// http://www.benmvp.com/learning-es6-enhanced-object-literals/
return {
@Offirmo
Offirmo / read_cookie.js
Last active December 2, 2018 07:53
[read cookie in JS] #JavaScript #browser #growth #frontend
@Offirmo
Offirmo / intercept.js
Last active May 13, 2024 20:53
[Intercepting fetch and/or XHR in JavaScript] #JavaScript #browser #growth
/////// fetch ///////
const originalFetch = window.fetch;
window.fetch = async (...args) => {
const fetchee = await originalFetch(...args);
return new Proxy(fetchee, {});
};
new Proxy(target, {
@Offirmo
Offirmo / intercept.js
Last active June 21, 2018 00:23
[Function call interception (wrapping)] #JavaScript #growth
// new
return (...args) => {
try {
return handler.call(this, ...args);
} catch(e) {
return this.onError(e);
}
};
// old school
@Offirmo
Offirmo / dom_active.js
Last active May 9, 2024 06:49
[DOM manipulation: WRITING] #JavaScript #dom #browser #frontend
// http://youmightnotneedjquery.com/
// navigating
// This works in all browsers:
location.assign('...')
location.href = '...'
location = '...'
location.reload()
// If you wanted to change the page without it reflecting in the browser back history, you can do
location.replace('...')