Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//Import
importScript("User:TheFearow/qstring.js");
//Start stub script

$(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;
  }
  mw.util.addPortletLink("p-cactions", "javascript:easyTag()", "tag (t)", "ca-tagt", "Add tag (top)", "");
  mw.util.addPortletLink("p-cactions", "javascript:easyTagBottom()", "tag (b)", "ca-tagb", "Add tag (bottom)", "");
});
 
function easyTag()
{
  var title = encodeURIComponent(mw.config.get('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(mw.config.get('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;
  }