User:Ashlux/relatedchangestab.js

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.
/* <nowiki> */
// modify this to suite your needs... Indexes should coorespond.

var talkTemplates = new Array();
talkTemplates[0] = 'WikiProject Oklahoma';
talkTemplates[1] = 'WikiProject Arizona';

var relatedChangesPages = new Array();
relatedChangesPages[0] = 'Wikipedia:WikiProject Oklahoma/Oklahoma recent changes';
relatedChangesPages[1] = 'Wikipedia:WikiProject Arizona/Arizona recent changes';

//////////////////////////////////////////
// Tabs by Korath
//////////////////////////////////////////

/**** Add tab as menu ****/
function addlimenu(tabs, name, id)
{
    var na = document.createElement('a');
    na.href = '';
    var mn = document.createElement('ul');
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.className = 'tabmenu';
    li.appendChild(na);
    li.appendChild(mn);
    tabs.appendChild(li);
    return li;
}

/**** Add generic tab ****/
function addlilink(tabs, url, name, id){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    return li;
}

/**** add talk message ****/
function autoMessage(msgtext, summary, saveopt, top)
{
    var txt = document.editform.wpTextbox1;
    document.editform.wpSummary.value = summary;
    if(txt.value.length > 0) txt.value += '\n';

    if (top == 'true')
    {
        txt.value = msgtext + txt.value;
    }
    else 
    {
        txt.value += msgtext;
    }

    if(saveopt == 'save')
    {
        document.editform.submit();
    }
    else if(saveopt == 'preview')
    {
        document.editform.wpPreview.click();
    }
    else if(saveopt == 'diff')
    {
        document.editform.wpDiff.click();
    }
}

function autoWikiProjectTemplateOnTalkPage()
{
    if (document.title.indexOf('Editing ') == 0)
    {
      var action = '';
      var target = '';
      if (location.search)
      {
        var l = location.search.substring(1).split('&');
        for (var i = 0; i < l.length; ++i)
        {
          var eq = l[i].indexOf('=');
          var name = l[i].substring(0, eq);
          if (name == 'fakeaction')
          {
            action = l[i].substring(eq + 1);
          }
          else if (name == 'faketarget')
          {
            target = unescape(l[i].substring(eq + 1)).replace(/_/g, ' ');
          }
        } // end for
      } // end if "location.search"
      

      if (action == 'addtemplatetotop')
      {
        if (document.getElementById('wpTextbox1').innerHTML.indexOf("{{" + target + "}}") == -1) {
          autoMessage("{{" + target + "}}\n", "Added {{tl|" + target + "}} to talk pages using [[User:Ashlux/RelatedChangesTab|RelatedChangesTab]].", "save", "true");
        }
      }
      else if (action == 'addtorelatedchanges')
      {
        if (document.getElementById('wpTextbox1').innerHTML.indexOf("[[" + target + "]]") == -1) {
          autoMessage("* [[" + target + "]]\n", "Add [[" + target + "]] to recent changes list using [[User:Ashlux/RelatedChangesTab|RelatedChangesTab]].", "save", "false");
        }
      } // end if "action"
    } // end if "Editing"
}

function wikiProjectTemplateOnTalkPage(index)
{

  // TODO: THIS WILL NOT WORK FOR ALL LANGUAGES!
  var pagename = document.title.replace(" - Wikipedia, the free encyclopedia", "");

  // open window to talk page
  window.open('/w/index.php?title=Talk:' + pagename + '&action=edit&fakeaction=addtemplatetotop&faketarget=' + talkTemplates[index], pagename + ' add template', 'status,toolbar,location,menubar,directories,resizeable,scrollbars');

  // open window to related changes page
  window.open('/w/index.php?title=' + relatedChangesPages[index] + '&action=edit&fakeaction=addtorelatedchanges&faketarget=' + pagename, pagename + ' add to recent changes', 'status,toolbar,location,menubar,directories,resizeable,scrollbars');

}

function setup()
{
  if (document.title.indexOf('talk:') != -1) return;
  if (document.title.indexOf('User:') == 0) return;
  if (document.title.indexOf('Editing') == 0) return;
  if (document.title.indexOf('Special') == 0) return;
  if (document.title == 'Related changes') return;

  var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
  addlimenu(tabs, 'RCt', 'RCt');
  var RCt = document.getElementById('RCt').getElementsByTagName('ul')[0];

  for (var i = 0; i < relatedChangesPages.length; ++i)
  {
    addlilink(RCt, 'javascript:wikiProjectTemplateOnTalkPage(' + i + ')', talkTemplates[i], talkTemplates[i]);
  }
}

addOnloadHook(setup);

if (window.addEventListener) window.addEventListener('load', autoWikiProjectTemplateOnTalkPage, false);
else if (window.attachEvent) window.attachEvent('onload', autoWikiProjectTemplateOnTalkPage);
/* </nowiki> */