Skip to content

Instantly share code, notes, and snippets.

@Timoteus78
Timoteus78 / xss_prevent.js
Created August 17, 2014 16:34
Preventing cross-site scripting by @thomasdavis
function htmlEncode(value){
return $('<div/>').text(value).html();
}
function htmlDecode(value){
return $('<div/>').html(value).text();
}
@Timoteus78
Timoteus78 / serializeObject.js
Created August 17, 2014 02:27
Serialize Object. Good for serializing forms to a map (elementName = elementValue). Found in a backbone tutorial by @thomasdavis
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
@Timoteus78
Timoteus78 / designer.html
Created July 21, 2014 00:42
Played around with Polymers online designer tool. Handy as a tool to learn Polymer.
<link rel="import" href="../core-scaffold/core-scaffold.html">
<link rel="import" href="../core-header-panel/core-header-panel.html">
<link rel="import" href="../core-menu/core-menu.html">
<link rel="import" href="../core-item/core-item.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-field/core-field.html">
<link rel="import" href="../core-icon/core-icon.html">
<link rel="import" href="../core-input/core-input.html">
<link rel="import" href="../core-icons/core-icons.html">
@Timoteus78
Timoteus78 / macroGenerator_bookmarklet
Created March 25, 2014 08:52
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') === '