// ==UserScript==
// @name          Simplify DevMo links
// @include       http://developer-test.mozilla.org/*action=*
// @description   Simplifies links in the edit box for DevMo wiki (replacing current article name in links with '.')
// @exclude
// ==/UserScript==

(function() {
  var pageTitle, form, textarea;
  function init() {
    // extract page title from the URL
    var res = /\?title=(.*?)&/.exec(document.location);
    if(!("1" in res)) throw "Cannot extract page title";
    pageTitle = res[1].replace(/_/g, " ");

    form = document.getElementById("editform");
    if(!form || !("wpTextbox1" in form)) 
      throw "oops: form="+form+"; wpTextbox1="+wpTextbox1;
    textarea = form.wpTextbox1;

    form.addEventListener("submit", submitHandler, false);

    textarea.value = textarea.value.replace(
      new RegExp("\\[\\[" + pageTitle + "([|:# ])", "g"), "[[.$1");
  }

  function submitHandler(e) {
    if(!textarea) return;
    textarea.value = textarea.value.replace(
      new RegExp("\\[\\[\\.([|:# ])", "g"), "[["+ pageTitle + "$1");
  }

  try {
    init();
  } catch(e) {
  }

})();