Skip to content

Instantly share code, notes, and snippets.

@danbrianwhite
danbrianwhite / refreshEvery.js
Created February 15, 2017 13:15
Refresh every x seconds on the exact modulus second
function refreshEvery(secs) {
secs = typeof secs === 'undefined' && secs < 1 ? 1 : secs;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
seconds = seconds + (secs - (seconds % secs))
@danbrianwhite
danbrianwhite / obfuscate.js
Created January 10, 2017 13:28
string obfuscate + deobfuscate
String.prototype.obfuscate = function()
{
var temp = '';
for(i = 0; i < this.length; i++){temp += String.fromCharCode(this.charCodeAt(i)+parseInt((i+1)%5))};
return temp;
}
String.prototype.deobfuscate = function()
{
var temp = '';
@danbrianwhite
danbrianwhite / cssStyleToReactObject.js
Last active July 9, 2016 18:10
CSS style string to React JS Object
var camelCaseCSS = function(property) {
property = property.replace(/^-ms-/gi, 'ms-');
return property.replace(/-\w/g, function(match){
return match.charAt(1).toUpperCase();
});
};
var cssStyleToJS = function(cssStyles)
{
var _cssStyles = {};
@danbrianwhite
danbrianwhite / encodeTag.js
Last active November 16, 2015 18:38
Enocde a JS string to be used in an HTML element's attritube
String.prototype.encodeTag = function () {
return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
@danbrianwhite
danbrianwhite / TinyPNG_dl_all.js
Created July 21, 2015 20:16
TinyPNG - download all bookmarklet
/*
TinyPNG - download all bookmarklet
Copyright (c) 2015 Dan White
Licensed under the MIT license.
*/
javascript:(function(){[].forEach.call(document.getElementsByClassName('results')[0].getElementsByTagName('a'), function(e){e.click()});})();
@danbrianwhite
danbrianwhite / fontPopper.js
Last active August 29, 2015 14:22
Font Popper
/* Font Poper */
/* DanBrianWhite */
/* Triggers a callback when a font pops in and is loaded. */
/* This function enables you to register a callback to occur when a font is loaded an pops in. Hence the name font popper! Supply the font name and font style you want to watch. Then when the font pops in, the callback function will be run. This allows you to update views or any other layouts that utilize JavaScript for sizing or anything else. */
/* currently requires jQuery */
var fontPopper = function (fontName, fontStyle, callback) {
@danbrianwhite
danbrianwhite / LazyLoad.js
Last active August 29, 2015 14:00
LazyLoad JS and CSS without Ajax
var _numFilesLazyLoad;
var _loadedFilesLazyLoad;
var _lazyLoaded = true;
var lazyLoadFiles = function(fileList, noCache)
{
if(_lazyLoaded)
{
_lazyLoaded = false;
_numFilesLazyLoad = 0;
_loadedFilesLazyLoad = 0;
@danbrianwhite
danbrianwhite / saveTextAsFile.js
Last active December 17, 2015 08:29
Save a string of text as a file with HTML5 (Chrome)
function saveTextAsFile(filename, text)
{
var textToWrite = text;
var textFileAsBlob = new Blob([textToWrite], {type:'text/html'});
var fileNameToSaveAs = filename;
var downloadLink = document.createElement("a");
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.download = fileNameToSaveAs;
downloadLink.click();
}
@danbrianwhite
danbrianwhite / InternetConnectionTest.js
Last active December 16, 2015 13:48
Internet Connection Test
$.ajax({url: filePath()+'/../../connectionTest.gif', cache: false} ).fail(function() { alert("Your connection to the internet has been lost. Please reconnect to the internet to continue this course."); });
@danbrianwhite
danbrianwhite / pixelToPercent.js
Last active December 16, 2015 05:29
pixelToPercent
var pixelToPercent = function(elements)
{
var _patt=/px/;
for (var i = elements.length - 1; i >= 0; i--)
{
var _this = elements[i];
var _parentWidth = _this.parentNode.offsetWidth;
var _parentHeight = _this.parentNode.offsetHeight;
var _style = getStyle(_this);