Skip to content

Instantly share code, notes, and snippets.

View matijs's full-sized avatar
:dependabot:
Scouting turtles

matijs matijs

:dependabot:
Scouting turtles
View GitHub Profile
@matijs
matijs / qsaHelper.js
Created June 23, 2016 05:36
Helper function to return querySelectorAll results as an array
function $_( selectors, baseElement ) {
var elements = (baseElement || document).querySelectorAll( selectors );
return Array.prototype.slice.call( elements );
}
@matijs
matijs / insert-script.js
Created March 9, 2016 18:37
Sure fire DOM Element insertion
function insertScript(url, onload, onerror) {
var ref = document.getElementsByTagName('script')[0];
var script = document.createElement('script');
script.src = url;
if (typeof onload === 'function') {
script.onload = onload;
}
if (typeof onerror === 'function') {
script.onerror = onerror;
}
@matijs
matijs / makefile
Last active October 1, 2021 12:10
Minimalist makefile using PostCSS
# some variables
POSTCSS = ./node_modules/.bin/postcss
POSTCSS_FLAGS = --use autoprefixer autoprefixer.browsers "> 2%"
# wildcard expansion is performed only in targets and in prerequisites so here we need $(wildcard)
SOURCES = $(wildcard src/css/*.css)
# use $(SOURCES) to determine what we actually need using a bit of pattern substitution
TARGETS = $(patsubst src%, build%, $(SOURCES))
# our default target (see below)
all: $(TARGETS)
@matijs
matijs / .editorconfig
Last active February 9, 2016 14:21
Sensible EditorConfig defaults
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
@matijs
matijs / textarea-keyboard-save.js
Created January 26, 2016 12:26
Trigger a click on a 'save' button by pressing cmd/ctrl-s in a textarea (adapt as needed)
(function() {
'use strict';
document.addEventListener('keydown', function(event) {
var S = 83,
activeElement = document.activeElement,
saveButtonId,
saveButton;
if ((event.key === S || event.keyCode === S) && (event.metaKey || event.ctrlKey) && activeElement.nodeName === 'TEXTAREA') {
<p>
<a href="#" class="button">Foo Bar</a>
<button type="button" class="button">Baz Quux</button>
</p>
@matijs
matijs / slow_server.py
Created February 25, 2015 13:28
Enhanced Python SimpleHTTPServer to slowly serve a specific mimetypes
"""Simple HTTP Server.
This module builds on BaseHTTPServer by implementing the standard GET
and HEAD requests in a fairly straightforward manner.
"""
__version__ = "0.6"
@matijs
matijs / loadCSS.js
Created February 23, 2015 11:28
load CSS using JS
function loadCSS(href, options){
'use strict';
options = options || {};
var styleSheet = document.createElement('link');
var ref = options.before || document.getElementsByTagName('script')[0];
styleSheet.media = 'not all';
styleSheet.rel = 'stylesheet';
styleSheet.href = href;
styleSheet.onload = function () {
styleSheet.onload = null; // only run once
;(function(handlers) {
if (!handlers) {
throw new Error('Nothing to handle');
}
document.documentElement.addEventListener('click', function(event) {
var handler = event.originalTarget.getAttribute('data-handler');
if (!handler) {
// nothing to do
return;
@matijs
matijs / enhancer.js
Created November 18, 2014 08:57
jQuery-based enhancing of components / elements declared with a data-attribute
$(function() {
'use strict';
// kick off js enhancements
$('[data-enhancer]').each(function() {
var enhancer = this.getAttribute('data-enhancer');
if (enhancers && typeof enhancers[enhancer] === 'function') {
enhancers[enhancer].call(this);
}
else {
if (window.console && typeof console.log === 'function') {