Skip to content

Instantly share code, notes, and snippets.

const ticketRegexp = /[A-Z]+-\d+/;
document.addEventListener('contextmenu', (e) => {
const link = e.target.closest('a');
const [ticketKey] = link.href.match(ticketRegexp);
if (!ticketKey) return;
document.documentElement.focus();
@ErBlack
ErBlack / incapsulateVerticalScroll.ls
Created February 17, 2017 08:18
Prevent vertical scroll outside of element, while scrolling on element.
/**
* Prevent vertical scroll outside of elem, while scrolling on elem
* @param {HTMLElement} elem
*/
function incapsulateVerticalScroll(elem) {
elem.addEventListener('wheel', (event) => {
if (event.deltaX !== 0) {
return; // horizontal
}
@ErBlack
ErBlack / query.js
Last active August 29, 2015 14:21
query parser
function query() {
location.search
.substr(1)
.split('&')
.reduce(function (query, component) {
component = component.length && component.split('=');
if (component.length) {
try {
var key = decodeURIComponent(component[0]),
@ErBlack
ErBlack / mimeCheck
Last active September 14, 2015 12:45
Function that checks MIME type support in browser. (@see demo http://jsfiddle.net/ErBlack/e2Ldgz7a/embedded/result/)
var mimeCheck = function (type) {
return Array.prototype.some.call(navigator.plugins, function (plugin) {
return Array.prototype.some.call(plugin, function (mime) {
return mime.type == type;
});
});
};
@ErBlack
ErBlack / format
Created August 8, 2014 17:46
Format prtint ob json
var format = function(value, indent) {
indent = indent || 1;
var tab = new Array(indent).join(' ');
switch (typeof value) {
case 'number':
case 'boolean':
return value;
break;
case 'string':
@ErBlack
ErBlack / rectify
Created May 8, 2014 13:31
Contverts multulevel array like ["a", ["b", "c"], [["d"], ["e", "f", [["g", ["h"]]]]]] to linear array like ["a", "b", "c", "d", "e", "f", "g", "h"]
Array.prototype.rectify = function() {
var result = [];
this.forEach(function(item) {
if (item instanceof Array) {
item.forEach(arguments.callee);
} else {
result.push(item);
}
});
@ErBlack
ErBlack / input filter
Last active August 29, 2015 13:58
Allows to filter and strip input data (uses jQuery UI widget factory, but it's not required)
(function($, undefined) {
/**
* filter input in fields
*/
$.widget('namespace.inputFilter', {
options: {
/**
* @type {Boolean} — add .error for empty required fields
*/
required: true,
@ErBlack
ErBlack / autoinit.js
Created March 11, 2014 12:48
Allows to register set of handlers for automaticaly init some DOM elements in passed root
var autoinit = function() {
var autoinit = function() {
var handlers = [];
this.add = function(foo) {
Array.prototype.forEach.call(arguments, function(foo) {
typeof foo == 'function' && handlers.push(foo);
});
}
@ErBlack
ErBlack / Might And Magick Columns
Created March 5, 2014 19:41
Might And Magick columns solution (brutal)
var rules = {
l: [0,-1,1,1],
r: [1,-1,-1,0],
b: [-1,1,0,1]
},
go = function(start, direction) {
return start.map(function(value, index) {
var val = value + rules[direction][index];
if (val < 0) val = 4 + val;
@ErBlack
ErBlack / gist:9097598
Created February 19, 2014 17:59
OneDrive encryption quest
var crypt = function() {
var decryptMap = {
3: 'A',
0: 'B',
Q: 'C',
S: 'D',
2: 'E',
P: 'F',
6: 'G',
I: 'H',