Skip to content

Instantly share code, notes, and snippets.

View dalgard's full-sized avatar

Kristian Dalgård dalgard

  • Various Productions
  • Aarhus, Denmark
View GitHub Profile
@dalgard
dalgard / _position.scss
Last active December 21, 2015 06:09
A Sass mixin for setting position the same way that margin and padding are set (`null` is used where no value should be output)
/*
Set position like margin/padding shorthand - rules with null value
are removed from output (standard)
Example:
@include position(0 20px null, fixed);
Output:
position: fixed;
top: 0;
@dalgard
dalgard / browser_action.js
Last active April 20, 2020 13:02
Inject content-script from browser-action and communicate with it
// Get the current active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
// Send message to check whether the script has already been injected
chrome.tabs.sendMessage(tabs[0].id, "ping", function (response) {
// If no message handler exists (i.e. content-script hasn't been injected before),
// this callback is called right away with no arguments, so ...
if (typeof response === "undefined") {
// ... inject content-script (null means current active tab)
chrome.tabs.executeScript(null, { file: "content_script.js" });
}
@dalgard
dalgard / extend.js
Last active December 30, 2015 10:39
Method for shallow extension of a given object with the properties of passed-in object(s) with support for standards-compliant getters and setters
function extend(target) {
// Run through rest parameters
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (source) {
// If source is an array, only copy enumerable properties
var keys = (Array.isArray(source) ? Object.keys(source) : Object.getOwnPropertyNames(source));
// Iterate over keys
keys.forEach(function (key) {
// Standards-compliant awesomeness
@dalgard
dalgard / queryAll.js
Created December 6, 2013 01:55
Efficient wrapper for document.querySelectorAll
// Always return an array of DOM elements
function queryAll(selector) {
var id_sel = selector.match(/^#([\w-]*)$/),
class_sel = !id_sel && selector.match(/^\.([\w-]+)$/),
tag_sel = !class_sel && selector.match(/^[\w-]+$/);
if (id_sel) {
var elem = document.getElementById(id_sel[1]);
return (elem ? [elem] : []); // Always return an array
}
@dalgard
dalgard / matchesSelector.js
Last active February 20, 2018 22:47
Cross-browser wrapper for element.matchesSelector
function matchesSelector(dom_element, selector) {
var matchesSelector = dom_element.matches || dom_element.matchesSelector || dom_element.webkitMatchesSelector || dom_element.mozMatchesSelector || dom_element.msM atchesSelector || dom_element.oMatchesSelector;
return matchesSelector.call(dom_element, selector);
}
@dalgard
dalgard / createOptions.js
Last active December 31, 2015 18:49
Sync some form controls with an object (http://codepen.io/dalgard/full/CkbKw)
/*
This trick is a lazy way of two-way sync'ing some form controls with an options object.
Actually, the "sync'ing" consists in getting the values when needed and updating the fields
when changing something. It has its limits, but may work quite nicely.
Depends on: jQuery/Zepto
The code below works for checkboxes (boolean), text input (string), number input (number),
select lists (string) and json in textarea (object)
@dalgard
dalgard / _link_states.scss
Last active January 26, 2017 09:09
A Sass mixin for quickly setting link states – test it on http://sassmeister.com/ (see more examples in the comments)
@dalgard
dalgard / promiseBoilerplate.js
Last active August 29, 2015 13:56
Module/function boilerplate using promises
/*
Module boilerplate (jQuery)
- Pass in a callback OR a promise
- Always returns a promise
*/
function getStuff(arg, d) {
var callback = d, // Save possible callback
is_promise = d && d.resolve; // Check whether d is a promise
@dalgard
dalgard / Class.js
Last active August 29, 2015 13:56
Extensible constructor pattern (ES5 version) inspired by Backbone.js – NOTE: Using `this.super` multiple times in the inheritance chain creates a problem since it results in a loop. There hasn't yet been an elegant solution to this, but a proper `super` keyword will be supported in ES6. In the meantime, coupling with the super's name can be avoi…
function Class(properties) {
// This base constructor can be left empty, but a nice boilerplate might look like this
if (properties) {
// Add properties to the instance
Object.getOwnPropertyNames(properties).forEach(function (property_name) {
var descriptor = Object.getOwnPropertyDescriptor(properties, property_name);
Object.defineProperty(this, property_name, descriptor);
}, this);
}
}
@dalgard
dalgard / _icons.scss
Created March 17, 2014 11:10
Use placeholder selectors for icons
// Use embedded fonts
@font-face {
font-family: 'icons';
font-weight: normal;
font-style: normal;
src:
url(data:application/x-font-ttf;charset=utf-8;base64,...) format('truetype'),
url(data:application/font-woff;charset=utf-8;base64,...) format('woff');
}