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 / 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 / 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 / _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;