Skip to content

Instantly share code, notes, and snippets.

@tborychowski
tborychowski / js caret position.js
Created February 22, 2013 13:09
JS :: get and set caret position inside input box
function getCaretPosition (ctrl) {
var caretPos = 0;
// IE
if (document.selection) {
ctrl.focus ();
var sel = document.selection.createRange();
sel.moveStart ('character', -ctrl.value.length);
caretPos = sel.text.length;
}
// Firefox
@tborychowski
tborychowski / detect-ie-quickhack.js
Last active December 19, 2015 11:49
JS :: detect IE
// the quickest way - by the bug :-)
var IE = !+"\v1"
@tborychowski
tborychowski / js-overrides.js
Last active December 20, 2015 00:09
JS :: some native objects "extensions"
/**
* JS OVERRIDES
*/
(function (window) {
'use strict';
String.prototype.trim = function (str) {return this.ltrim(str).rtrim(str); };
String.prototype.ltrim = function (str) { return this.replace(new RegExp('^' + (str ? str : '\\s') + '+'), ''); };
String.prototype.rtrim = function (str) { return this.replace(new RegExp((str ? str : '\\s') + '+$'), ''); };
String.prototype.ucfirst = function () {
@tborychowski
tborychowski / js-number-format.js
Created October 11, 2013 10:39
JS :: format number as currency
(2500).toLocaleString("en-GB", {style: "currency", currency: "GBP", minimumFractionDigits: 2})
@tborychowski
tborychowski / yt-downloader.user.js
Last active October 7, 2016 12:34
JS :: yt downloader monkey script
// ==UserScript==
// @name Download YouTube Videos as MP4
// @description Adds a button that lets you download YouTube videos.
// @homepageURL https://github.com/gantt/downloadyoutube
// @author Gantt
// @version 1.8.8
// @date 2016-09-02
// @namespace http://googlesystem.blogspot.com
// @include http://www.youtube.com/*
// @include https://www.youtube.com/*
@tborychowski
tborychowski / form-validation.html
Last active November 21, 2017 10:10
form-validation.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/style.css">
<style>
html { height: 100%; }
body {
font-family: sans-serif;
font-weight: 300;
@tborychowski
tborychowski / animated-dialog.html
Created July 13, 2018 07:33
html dialog with animation
<!DOCTYPE html>
<head>
<meta charset=UTF-8>
<title>Dialog demo</title>
<style>
.dialog {
border: none;
border-radius: 3px;
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
}
@tborychowski
tborychowski / utils.js
Last active September 13, 2018 15:53
JS :: Utils - helper library
/**
* https://gist.github.com/tborychowski/3799025
*/
var UTILS = {
/*jshint maxlen: 500, onevar: false */
isTouch : (/iPhone|iPod|iPad/ig).test(navigator.userAgent),
/**
* Modified & lintified version of https://gist.github.com/527683
@tborychowski
tborychowski / fuzzy.js
Created August 22, 2014 12:51
JS :: fuzzy search
String.prototype.fuzzy = function (s) {
var hay = this.toLowerCase(), i = 0, n = -1, l;
s = s.toLowerCase();
for (; l = s[i++] ;) if (!~(n = hay.indexOf(l, n + 1))) return false;
return true;
};
('a haystack with a needle').fuzzy('hay sucks'); // false
('a haystack with a needle').fuzzy('sack hand'); // true
@tborychowski
tborychowski / img2datauri
Created November 29, 2021 11:14
JS :: convert image to data url with javascript
console.clear();
var img = new Image();
img.src = 'img/img.png';
img.onload = function () {
var canvas = document.createElement('canvas'), context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
console.log(canvas.toDataURL('image/png'));