User:TheFearow/tagger.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
//Import
importScript("User:TheFearow/qstring.js");
//Start stub script
 
addOnloadHook(function()
{
  //check if called from stub request
  if (wgAction == 'edit')
  {
    if (QueryString.exists("tag"))
    {
      if(QueryString.exists("redir") && QueryString.get("redir") != "" && getCookie("redir") == ""){
       //alert("Setting cookie and making stick ("+unescape(QueryString.get("redir")) + ")");
       var redir = QueryString.get("redir");
       setCookie("redir", unescape(redir), 1);
       //window.location.href = "index.php?title=" + QueryString.get("title") + "&action=" + QueryString.get("action") + "&tag=" + QueryString.get("tag");
       //return;
      }
      document.getElementById("contentSub").innerHTML = "<h" + "2>The Save button has automatically been clicked, please wait while you are redirected.</h" + "2>";
      var loc = 1;
      if(QueryString.get("loc") == "bottom"){
           loc = 0;
      }
      var type = QueryString.get("tag");
      if(document.editform.wpTextbox1.value.indexOf("{" + "{"+type+"}}") != -1){
          window.alert("Page already has tag");
          history.go(-1);
          return;
      }
      if(loc == 1){
           document.editform.wpTextbox1.value = "{" + "{" + type + "}}\n"+document.editform.wpTextbox1.value;
      }else{
           document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + "\n{"+"{"+type+"}}";
      }
      document.editform.wpMinoredit.checked = true;
      document.editform.wpSummary.value = "Added tag {"+"{"+type+"}}";
 
      document.editform.wpSave.click();
      return;
    }
  }
  if (wgCanonicalNamespace == "Special"){
    return;
  }
  addPortletLink("p-cactions", "javascript:easyTag()", "tag (t)", "ca-tagt", "Add tag (top)", "");
  addPortletLink("p-cactions", "javascript:easyTagBottom()", "tag (b)", "ca-tagb", "Add tag (bottom)", "");
});
 
function easyTag()
{
  var title = encodeURIComponent(wgPageName);
  var tag = window.prompt("Enter template name");
  if(tag == null){
     return;
  }
  location.assign("/w/index.php?action=edit&tag=" + tag + "&title=" + title);
}
function easyTagBottom()
{
  var title = encodeURIComponent(wgPageName);
  var tag = window.prompt("Enter template name");
  if(tag == null){
     return;
  }
  location.assign("/w/index.php?action=edit&tag=" + tag + "&loc=bottom&title=" + title);
}
 
 
//End tag script
//
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}
 
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"
}
 
 
  if(getCookie("redir") != "" && wgAction != "edit"){
   //alert("Redirecting");
   var target = getCookie("redir");
   setCookie("redir", "", 1);
   //alert("Target: "+target);
   document.location.href = target;
  }