User:Lupo Test/js code

From Wikipedia, the free encyclopedia
/* Return the topmost function of the call stack */
function top_most_function (f)
{
  var func = f;
  
  while (func.caller) func = func.caller;
  return func;
}

/* Return the event that triggered it all. */
function get_event ()
{
   var evt = window.event;

   if (!evt) {
      /* No event? We're on Mozilla... get the event from the
         argument of the topmost function on the call stack.
      var func = top_most_function (get_event.caller);
      if (func && func.arguments.length > 0)
        return func.arguments[0];
   }
   return evt;
}

/* Stash away the original submit here: */
var original_submit;

/* "Safe" submit function: submit only if the triggering event
   had not been an "onload" event. */
function safe_submit ()
{
  var top_event = get_event ();
  if (top_event.type.indexOf ("load") >= 0) {
    /* Oops: called from within an "onload" event: do not allow submitting
       forms from onload as this is being used by vandals. */
    if (document.forms && document.forms[0] && document.forms[0].wpTextbox1)
      document.forms[0].wpTextbox1.value += "\nSUBMIT BLOCKED";
  } else {
    /* Not found, it's ok: call the stashed-away submit */
    original_submit ();
  } 
}

/* Replace the standard sumbit function by our own, "safe" variant. */
if (document.forms && document.forms[0]) {
  original_submit = document.forms[0].submit;
  document.forms[0].submit = safe_submit;
}