Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ubiquity command: redirect-http
//////////////////////////////////////////////////////////////////////////////
//
// RedirectHttp
//
// Note Ubiquity makes these objects available (the list is incomplete, but
// contains the objects that are used in this script):
//
// - Application:: the FUEL[http://developer.mozilla.org/en/FUEL] Application object
// - CmdUtils:: Ubiquity command utilities
//
//////////////////////////////////////////////////////////////////////////////
var RedirectHttp = {
redirect: function(redirect_action) {
// Get the document representing the current HTML page
var objDocument = Application.activeWindow.activeTab.document;
// Revert if forms have already been redirected
var divs = objDocument.getElementsByTagName('div');
for(i=0; i<divs.length; i++) {
if (divs[i].getAttribute('id') == '__redirecthttp_header') {
var execute_revert_script = objDocument.createElement("script");
execute_revert_script.type = "text/javascript";
execute_revert_script.innerHTML = "redirecthttp_revert_forms();"
objDocument.getElementById('__redirecthttp_header').appendChild(execute_revert_script);
}
}
var original_action_name = "__original_action";
var redirect_label = "Redirecting HTTP Requests";
var error_label = "Redirect HTTP Error";
var error_message = "The webpage may have been corrupted; be sure to refresh.";
// Add notifications
var css_style = objDocument.createElement("style");
css_style.type = 'text/css';
css_style.rel = 'stylesheet';
css_style.innerHTML = "" +
".__notification_div { position:relative;text-align:center;background-color:#EFE3BC;border: thin solid white;}" +
"#__redirecthttp_header div.__redirecthttp_message { position:absolute;left:0; }" +
".__notification_div div.__redirecthttp_message { display:inline;font-weight:bold; }" +
".__notification_div form.__redirecthttp_url { display:inline; }" +
".__notification_div div.__redirecthttp_links { display:inline;position:absolute;right:0;font-size:0.8em;font-weight:normal;text-decoration:none; }";
var header_div = objDocument.createElement("div");
header_div.setAttribute("id", "__redirecthttp_header");
header_div.setAttribute("class", "__notification_div");
header_div.innerHTML = "" +
"<div class='__redirecthttp_message'>" + redirect_label + "</div>" +
"<form action='" + redirect_action + "' method='get' class='__redirecthttp_url'>" +
"<input type='text' name='" + original_action_name + "' value='" + objDocument.URL + "'>" +
"<input type='submit' value='Send'>" +
"</form>" +
"<div class='__redirecthttp_links'>" +
"<a href='javascript:redirecthttp_revert_forms();'>close</a>" +
"</div>";
var footer_div = objDocument.createElement("div");
footer_div.setAttribute("id", "__redirecthttp_footer");
footer_div.setAttribute("class", "__notification_div");
footer_div.innerHTML = "" +
"<div class='__redirecthttp_message'>" + redirect_label + "</div>" +
"<div class='__redirecthttp_links'>" +
"<a href='javascript:redirecthttp_revert_forms();'>close</a>" +
"</div>";
var revert_script = objDocument.createElement("script");
revert_script.type = "text/javascript";
revert_script.innerHTML = "function redirecthttp_revert_forms() {" +
"try {" +
// remove notifications
"document.body.removeChild( document.getElementById('__redirecthttp_header') );" +
"document.body.removeChild( document.getElementById('__redirecthttp_footer') );" +
// redirect forms back to original actions
"var forms = document.getElementsByTagName('form');" +
"for(i=0; i<forms.length; i++) {" +
"var form = forms[i];" +
"var form_index = form.getAttribute('__redirecthttp_form_index');" +
"var original_action_input = document.getElementById('__redirecthttp_original_action_' + form_index);" +
"form.setAttribute('action', original_action_input.value);" +
"form.removeAttribute('__redirecthttp_form_index');" +
"form.removeChild(original_action_input);" +
//"alert(form.innerHTML);" +
"}" +
// redirect anchors back to original hrefs
"var anchors = document.getElementsByTagName('a');" +
"for(i=0; i<anchors.length; i++) {" +
"var anchor = anchors[i];" +
"anchor.setAttribute('href', anchor.getAttribute('__redirecthttp_original_href'));" +
"anchor.removeAttribute('__redirecthttp_original_href');" +
"}" +
"} catch(ex) {" +
"alert('" + error_label + ": ' + ex.message + \"\\n\\n\" + '" + error_message + "');" +
"}" +
"}";
try {
// redirect forms to the redirect_action
var forms = objDocument.getElementsByTagName('form');
for(i=0; i<forms.length; i++) {
var form = forms[i];
var original_action_input = objDocument.createElement("input");
original_action_input.setAttribute("id", "__redirecthttp_original_action_" + i);
original_action_input.setAttribute("name", original_action_name);
original_action_input.setAttribute("type", "hidden");
original_action_input.setAttribute("value", form.action);
form.setAttribute("action", redirect_action);
form.setAttribute("__redirecthttp_form_index", i);
form.appendChild(original_action_input);
//alert(form.innerHTML);
}
// redirect anchors to the redirect_action
var anchors = objDocument.getElementsByTagName('a');
for(i=0; i<anchors.length; i++) {
var anchor = anchors[i];
anchor.setAttribute("__redirecthttp_original_href", anchor.href);
anchor.setAttribute("href", redirect_action + "?" + original_action_name + "=" + escape(anchor.href));
}
// add notifications
header_div.appendChild(revert_script);
header_div.appendChild(css_style);
objDocument.body.insertBefore(header_div, objDocument.body.firstChild)
objDocument.body.appendChild(footer_div)
} catch(ex) {
displayMessage(error_label + ": " + ex.message + "\n\n" + error_message);
}
}
}
//////////////////////////////////////////////////////////////////////////////
//
// redirect_http command
//
//////////////////////////////////////////////////////////////////////////////
CmdUtils.CreateCommand({
name: "redirect_http",
author: { name: "Simon Chiang", email: "simon.a.chiang@gmail.com"},
license: "MIT",
description: "Redirects html forms to a custom url",
help: "Redirect a page by typing 'redirect <new url>'. Reset using the controls that appear on the page",
takes: {"url": noun_arb_text},
execute: function(url) {
RedirectHttp.redirect(url.html);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.