Skip to content

Instantly share code, notes, and snippets.

@michaelfox
Created May 22, 2014 18:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfox/2a5913b2268c48c03154 to your computer and use it in GitHub Desktop.
Save michaelfox/2a5913b2268c48c03154 to your computer and use it in GitHub Desktop.
Form Save / Restore Bookmarklets
javascript:(function(e%2Ct%2Ci%2Co%2Cn%2Ca%2Cr%2Cc)%7Bif(!(n%3De.jQuery)%7C%7Ci%3En.fn.jquery%7C%7Co(n))%7Ba%3Dt.createElement(%22script%22)%3Ba.type%3D%22text%2Fjavascript%22%3Ba.src%3D%22http%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fjquery%2F%22%2Bi%2B%22%2Fjquery.min.js%22%3Ba.onload%3Da.onreadystatechange%3Dfunction()%7Bif(!r%26%26(!(c%3Dthis.readyState)%7C%7C%22loaded%22%3D%3D%3Dc%7C%7C%22complete%22%3D%3D%3Dc))%7Bo((n%3De.jQuery).noConflict(1)%2Cr%3D1)%3Bn(a).remove()%7D%7D%3Bt.documentElement.childNodes%5B0%5D.appendChild(a)%7D%7D)(window%2Cdocument%2C%221.11.0%22%2Cfunction(e)%7Bfunction%20t(t)%7Bvar%20i%3De(t)%2Co%3D%5B%22id%22%2C%22name%22%2C%22action%22%5D%2Cn%3Dfalse%3Be.each(o%2Cfunction(e%2Ct)%7Bif(!n%26%26i.attr(t))n%3Di.attr(t)%7D)%3Breturn%20n%7De.unserialize%3Dfunction(e)%7Bfor(var%20t%2Ci%2Co%3DdecodeURI(e.replace(%2F%5C%2B%2Fg%2C%22%20%22))%2Cn%3Do.split(%22%26%22)%2Ca%3D%7B%7D%2Cr%3D0%2Cc%3Dn.length%3Br%3Cc%3Br%2B%2B)%7Bt%3Dn%5Br%5D.split(%22%3D%22)%3Bi%3Dt%5B0%5D%3Bif(void%200%3D%3D%3Da%5Bi%5D)a%5Bi%5D%3DdecodeURIComponent(t%5B1%5D)%3Belse%7Bif(%22string%22%3D%3D%3Dtypeof%20a%5Bi%5D)a%5Bi%5D%3D%5Ba%5Bi%5D%5D%3Ba%5Bi%5D.push(decodeURIComponent(t%5B1%5D))%7D%7Dreturn%20a%7D%3Be.fn.unserializeForm%3Dfunction(t)%7Bif(!t)return%20this%3Bt%3De.unserialize(t)%3Be(this).find(%22%3Ainput%22).not('%3Abutton%2C%3Adisabled%2C%5Btype%3D%22hidden%22%5D').each(function(i%2Co)%7Bvar%20n%3De(o)%2Ca%3Dn.attr(%22name%22)%2Cr%3Dt%5Ba%5D%3Bif(void%200!%3D%3Dr)%7Bn.trigger(%22focus%22)%3Bif(%22radio%22%3D%3D%3Do.type%7C%7C%22checkbox%22%3D%3D%3Do.type)n.prop(%22checked%22%2Cn.prop(%22value%22)%26%26o.value%3D%3D%3Dr%7C%7C!n.prop(%22value%22))%3Belse%20n.val(r)%3Bn.trigger(%22change%22).trigger(%22blur%22)%7D%7D)%7D%3Bif(localStorage.savedFormData)%7Bvar%20i%2Co%3DJSON.parse(localStorage.savedFormData)%3Bif(i%3Do%5Bwindow.location.href%5D)e(%22form%22).each(function(o%2Cn)%7Bvar%20a%3Dt(n)%7C%7C%22form-%22%2Bo%3Bif(i%5Ba%5D)e(n).unserializeForm(i%5Ba%5D)%7D)%7D%7D)%3B
(function( window, document, req_version, callback, $, script, done, readystate ){
if ( !($ = window.jQuery) || req_version > $.fn.jquery || callback( $ ) ) {
script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + req_version + '/jquery.min.js';
script.onload = script.onreadystatechange = function() {
if ( !done && ( !( readystate = this.readyState ) || readystate === 'loaded' || readystate === 'complete' ) ) {
callback( ($ = window.jQuery).noConflict(1), done = 1 );
$( script ).remove();
}
};
// Add the script element to either the head or body, it doesn't matter.
document.documentElement.childNodes[0].appendChild( script );
}
})( window, document, '1.11.0',
function( $, L /* boolean - external jQuery file was loaded */ ) {
$.unserialize = function(encStr){
var str = decodeURI(encStr.replace(/\+/g, ' ')),
pairs = str.split('&'),
obj = {}, p, idx;
for (var i=0, n=pairs.length; i < n; i++) {
p = pairs[i].split('=');
idx = p[0];
if (obj[idx] === undefined) {
obj[idx] = decodeURIComponent(p[1]);
} else {
if (typeof obj[idx] === "string") {
obj[idx]=[obj[idx]];
}
obj[idx].push(decodeURIComponent(p[1]));
}
}
return obj;
};
/*!
* takes a form data and repopulates form
*/
$.fn.unserializeForm = function(data) {
if (!data) {
return this;
}
data = $.unserialize(data);
$(this).find(":input").not(':button,:disabled,[type="hidden"]').each(function(idx, input) {
var $input = $(input),
key = $input.attr("name"),
value = data[key];
if (value !== undefined) {
$input.trigger('focus');
if (input.type === 'radio' || input.type === 'checkbox') {
// set them only if they have matching value or no value set at all
$input.prop( 'checked', ( $input.prop( 'value' ) && input.value === value ) || ! $input.prop( 'value' ) );
} else {
$input.val( value );
}
$input.trigger('change').trigger('blur');
}
});
};
function getFormIdentifier(el) {
var $el = $(el),
attrs = ['id', 'name', 'action'],
ident = false;
$.each(attrs, function(attrIdx, attr) {
if (!ident && $el.attr(attr)) {
ident = $el.attr(attr);
}
});
return ident;
}
if (localStorage["savedFormData"]) {
var pageFormData,
savedFormData = JSON.parse(localStorage.savedFormData);
if(pageFormData = savedFormData[window.location.href]) {
$('form').each(function(idx, el) {
var formId = getFormIdentifier(el) || "form-" + idx;
if (pageFormData[formId]) {
$(el).unserializeForm(pageFormData[formId]);
}
});
}
}
}
);
javascript:(function(e%2Ca%2Ct%2Co%2Cn%2Cr%2Ci%2Cc)%7Bif(!(n%3De.jQuery)%7C%7Ct%3En.fn.jquery%7C%7Co(n))%7Br%3Da.createElement(%22script%22)%3Br.type%3D%22text%2Fjavascript%22%3Br.src%3D%22http%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fjquery%2F%22%2Bt%2B%22%2Fjquery.min.js%22%3Br.onload%3Dr.onreadystatechange%3Dfunction()%7Bif(!i%26%26(!(c%3Dthis.readyState)%7C%7C%22loaded%22%3D%3D%3Dc%7C%7C%22complete%22%3D%3D%3Dc))%7Bo((n%3De.jQuery).noConflict(1)%2Ci%3D1)%3Bn(r).remove()%7D%7D%3Ba.documentElement.childNodes%5B0%5D.appendChild(r)%7D%7D)(window%2Cdocument%2C%221.11.0%22%2Cfunction(e)%7Bfunction%20a(a)%7Bvar%20t%3De(a)%2Co%3D%5B%22id%22%2C%22name%22%2C%22action%22%5D%2Cn%3Dfalse%3Be.each(o%2Cfunction(e%2Ca)%7Bif(!n%26%26t.attr(a))return%20n%3Dt.attr(a)%7D)%3Breturn%20n%7Dvar%20t%2Co%3D%7B%7D%3Be(%22form%22).each(function(t%2Cn)%7Bvar%20r%3Da(n)%7C%7C%22form-%22%2Bt%3Bo%5Br%5D%3De(n).serialize()%3Bconsole.log(r%2Co%5Br%5D)%7D)%3Bt%3DJSON.parse(localStorage.savedFormData%7C%7C%22%7B%7D%22)%3Bt%5Bwindow.location.href%5D%3Do%3BlocalStorage.savedFormData%3DJSON.stringify(t)%7D)%3B
(function( window, document, req_version, callback, $, script, done, readystate ){
if ( !($ = window.jQuery) || req_version > $.fn.jquery || callback( $ ) ) {
script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + req_version + '/jquery.min.js';
script.onload = script.onreadystatechange = function() {
if ( !done && ( !( readystate = this.readyState ) || readystate === 'loaded' || readystate === 'complete' ) ) {
callback( ($ = window.jQuery).noConflict(1), done = 1 );
$( script ).remove();
}
};
// Add the script element to either the head or body, it doesn't matter.
document.documentElement.childNodes[0].appendChild( script );
}
})( window, document, '1.11.0',
function( $, L /* boolean - external jQuery file was loaded */ ) {
var savedFormData,
pageFormData = {};
function getFormIdentifier(el) {
var $el = $(el),
attrs = ['id', 'name', 'action'],
ident = false;
$.each(attrs, function(attrIdx, attr) {
if (!ident && $el.attr(attr)) {
return ident = $el.attr(attr);
}
});
return ident;
}
$('form').each(function(idx, el) {
var formId = getFormIdentifier(el) || "form-" + idx;
pageFormData[formId] = $(el).serialize();
console.log(formId, pageFormData[formId]);
});
savedFormData = JSON.parse(localStorage.savedFormData || "{}");
savedFormData[window.location.href] = pageFormData;
localStorage.savedFormData = JSON.stringify(savedFormData);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment