Skip to content

Instantly share code, notes, and snippets.

@kenelliott
Created April 17, 2014 20:54
Show Gist options
  • Save kenelliott/11010954 to your computer and use it in GitHub Desktop.
Save kenelliott/11010954 to your computer and use it in GitHub Desktop.
self.encryptForm = function (form) {
var element, encryptedValue,
fieldName, hiddenField,
i, inputs;
form = extractForm(form);
inputs = findInputs(form);
while (hiddenFields.length > 0) {
try {
form.removeChild(hiddenFields[0]);
} catch (err) {}
hiddenFields.splice(0, 1);
}
for (i = 0; i < inputs.length; i++) {
element = inputs[i];
fieldName = element.getAttribute("data-encrypted-name");
encryptedValue = self.encrypt(element.value);
console.log("encryption", element.value, encryptedValue);
element.removeAttribute("name");
hiddenField = createElement("input", {
value: encryptedValue,
type: "hidden",
name: fieldName
});
hiddenFields.push(hiddenField);
form.appendChild(hiddenField);
}
};
self.onSubmitEncryptForm = function (form, callback) {
var wrappedCallback;
form = extractForm(form);
wrappedCallback = function (e) {
self.encryptForm(form);
return (!!callback) ? callback(e) : e;
};
if (window.jQuery) {
window.jQuery(form).submit(wrappedCallback);
} else if (form.addEventListener) {
form.addEventListener("submit", wrappedCallback, false);
} else if (form.attachEvent) {
form.attachEvent("onsubmit", wrappedCallback);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment