Skip to content

Instantly share code, notes, and snippets.

View pinkhominid's full-sized avatar

John Ericson pinkhominid

View GitHub Profile
@pinkhominid
pinkhominid / index.js
Created May 21, 2019 19:05
One-line AngularJS get service instance (e.g. $http)
angular.element(document.body).injector().get('$http')
@pinkhominid
pinkhominid / example.js
Last active March 18, 2019 22:10
Parsing a URL (2019-edition)
const encodedDestHref = encodeURIComponent('https://example.com/#/hash/path?hparam1=a&hparam2=b');
location.href = `https://example.com/path?dest=${encodedDestHref}`;
const searchParams = new URLSearchParams(location.search);
const destHref = searchParams.get('dest');
const destUrl = new URL(destHref);
const hashRelativeUrl = new URL(destUrl.hash.substr(1), destUrl.origin); //hack
console.log(hashRelativeUrl.pathname); // '/hash/path'
console.log(hashRelativeUrl.search); // '?hparam1=a&hparam2=b'
@pinkhominid
pinkhominid / clear-style.css
Last active April 2, 2021 03:00
Clear/reset default styles of common HTML elements
/* Anchors */
a.clear-style {
color: inherit;
text-decoration: none;
}
/* Buttons */
button.clear-style,
input[type=button].clear-style,
input[type=reset].clear-style {
@pinkhominid
pinkhominid / index.js
Created February 11, 2019 15:43
Clean Fetch Example (per Google Devs)
function logResult(result) {
console.log(result);
}
function logError(error) {
console.log('Looks like there was a problem: \n', error);
}
function validateResponse(response) {
if (!response.ok) {
@pinkhominid
pinkhominid / index.html
Last active February 5, 2019 02:19
AngularJS Quick Component Demo Page
<!doctype html><html lang=en><meta charset=utf-8><title>Demo</title>
<link rel=stylesheet href=component.css>
<component></component>
<script src=https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.min.js></script>
<script>angular.module('componentModule', [])</script>
<script src=component.js></script>
<script>angular.bootstrap(document.documentElement, ['componentModule'])</script>
@pinkhominid
pinkhominid / demo-app.js
Last active January 31, 2019 01:05
Minimal Web Component Demo App Starter
const tagName = 'demo-app';
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: block;
contain: content;
}
:host([hidden]) {
@pinkhominid
pinkhominid / example.js
Created January 27, 2019 17:51
Get super class name of registered custom element
Object.getPrototypeOf(customElements.get('my-custom-element')).name
// "HTMLElement"
@pinkhominid
pinkhominid / index.html
Last active January 26, 2019 03:26
Short valid HTML starter
<!doctype html><html lang=en><meta charset=utf-8><title>Hello World!</title>
@pinkhominid
pinkhominid / abortable-http-service.js
Last active January 26, 2019 04:46
AngularJS abortable GET request
/** Aborting $http made simple */
export class AbortableHttpService {
constructor($q, $http, $timeout) {
this.$q = $q;
this.$timeout = $timeout;
this.$http = $http;
}
get(url, options = {}) {
const {$q, $http, $timeout} = this;
@pinkhominid
pinkhominid / one-line-fetch.js
Last active April 19, 2019 03:11
Fetch in console
fetch('/api/things')
.then(res => res.json())
.then(res => console.log(res))