Last Updated: February 25, 2016
·
866
· timoteus78

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>");});})()}})();

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside