Skip to content

Instantly share code, notes, and snippets.

@Timoteus78
Created March 25, 2014 08:52
Show Gist options
  • Save Timoteus78/9757593 to your computer and use it in GitHub Desktop.
Save Timoteus78/9757593 to your computer and use it in GitHub Desktop.
Bookmarklet for recording form inputs. Outputs a new bookmarklet with the recorded form. Used it a lot when filling forms on mobile devices or IE6/7.
javascript:(function(){var formRecord = {},prefix = "javascript:(function(){",postfix = "})();",setFormRecord=function(){var elValue = "";var tag = $(this).prop("tagName");if ( "SELECT" === tag || "INPUT" === tag){formRecord[$(this).attr("id")] = $(this).val();}else if ( "FIELDSET" === tag){$(this).find("input[type=radio]").each(function(){formRecord[$(this).attr("id")] = $(this).is(":checked");});}else if("BUTTON" === tag){formRecord[$(this).attr("id")] = "";}},generateScriptLet = function(){var strFormRecord = JSON.stringify(formRecord);var scriptLet = "var fD = " + strFormRecord + "; ";scriptLet += "$(document).ajaxSuccess(function(event,xhr,settings){var $markup=$(xhr.responseText);$markup.find('select').each(function(){var elId=$(this).attr('id');if(fD[elId]){var $that=$(this);setTimeout(function(){$('#' + elId).val(fD[elId]);}, 1000);}});});for(var k in fD){var selector ='#'+k;var $el=$(selector);if($el.prop('type') === 'radio'){$el.prop('checked',fD[k]).trigger('change');}else if($el.prop('type') === 'submit'){$el.click();}else{$el.val(fD[k]).trigger('change');}}";return prefix + scriptLet + postfix;};return {start: (function(){$(document).on("change","select, input[type!=radio], fieldset", setFormRecord);$("button").on("click", setFormRecord);$("body").prepend("<div id='macroContainer' style='background:orange;'><button id='stopRecordingBtn' style='background:red;'>stopRecording</button><p>Recording has started...</p></div>");$("#stopRecordingBtn").click(function(){$('#macroContainer').append("<textarea>" + generateScriptLet() + "</textarea>");});})()}})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment