How to Automatize HTTP Requests Using Greasemonkey
Thursday, May 31st, 2007There are some sites that I use daily. The sad thing is, many of them don't provide me any mechanisms to log in automatically, so I have to always do it manually. I also had to fill the same form in on these sites and carry on the same operation over and over again. This was very frustrating for me so I decided to come up with a solution. Greasemonkey seemed to have the potential to solve my burning need.
I want to share the core function I've written that can help you in such scenarios. It's a simple, but very useful one.
function submit_form(method, action, elements, target)
{
var form = document.createElement('form');
document.body.appendChild(form);
form.method = method;
form.action = action;
for (element_i in elements) {
var element = document.createElement('input');
element.setAttribute('type', 'hidden');
element.setAttribute('name', element_i);
element.setAttribute('value', elements[element_i]);
form.appendChild(element);
}
if (target) {
form.target = target;
}
form.submit();
}
Requests are typically triggered based on the content of window.location.href or document.referrer.
Greasemonkey has some ugly annoyances, but fortunately most of them can be worked around. The LiveHTTPHeaders add-on can also help you tremendously constructing the fields of the forms.



