Generate macros in bookmarklet format
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>");});})()}})();
Written by Timo van der Schuit
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#