Jump to content

User:Joshurtree/talkthread.js

From Wikipedia, the free encyclopedia
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.
//<pre><nowiki>

function createMessage(section) {
  var edit = document.getElementById('wpTextBox1');
  var replyToPos = document.URL.lastIndexOf('replyto=');
  
  edit.value += '{{msg start|id=' + wgArticleId;
  if (replyToPos != -1) {
    var replyto = document.URL.slice(replyToPos + 8);
    edit.value += '|replyto=' + replyto;
    document.getElementById('wpSummary').value += 'Reply to ' + replyto;
  }

  edit.value += '}}\n';
  edit.value += '<!--- Insert message here --->\n';
  edit.value += '{{msg end|signature=~~~~|username=' 
                + wgUserName + '|section=' + section + '}}';
}

function addlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function addTab(url, name, id, title, key){
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    return addlilink(tabs, url, name, id, title, key);
}

function collapseDescendants() {
  alert("collapseDescendants");
}

function showThisThread() {
  alert("showThread");
}


function setupCollapsing(collapsed) {
  var headers = document.getElementsByName("id");

  var collapseDesc = document.createElement("a");
  collapseDesc.href = "javascript::collapseDescendants()";
  collapseDesc.innerHTML = "↑";
  collapseDesc.title = "Collapse descendants";
  
  var showThread = document.createElement("a");
  showThread.href = "javascript::showThisThread()";
  showThread.innerHTML = "↓";
  showThread.title = "Show this thread only";

  for (var i = 0; i < headers.length; ++i) {
    headers[i].insertBefore(headers[i].firstChild, showThread);
    headers[i].insertBefore(headers[i].firstChild, collapseDesc);
  }
}


function countSections(url) {
    var article = window.open(url, "_blank", "directories=no, height=100, location=no, menubar=no, scrollbars=no, resizable=no, titlebar=no, statusbar=no, toolbar=no, width=no");
    return article.document.getElementsByTagName("h2").length + 1;
}


$(function() {
  window.alert("test");
  var addSection = document.getElementById('ca-addsection');
  var section = document.getElementsByName('wpSection');
  var edit = document.getElementById('wpTextBox1');

  if (addSection && edit == null) {
    var name = 'Start thread';
    addTab(url, name, 'usStartThread', name, null);
    setupCollapsing(false);
  } else if (document.URL.indexOf("replyto") != -1) {
    createMessage(section[0].value);
  } else if (document.URL.indexOf("sectionnum=") != -1) {
    var url = document.URL;
    var sectionNum = 
      url.slice(parseInt(url.indexOf("sectionnum=")) + 11);
    if (isFinite(sectionNum)) {
      var sectionName = prompt("Enter new section name", "");
      if (sectionName != null && sectionName != "") {
        edit.value += '\n== ' + sectionName + ' ==\n';
        document.getElementById("wpSummary").value 
            += "/* " + sectionName + " */";
        createMessage(sectionNum);
      }
    }
  }
});
//</nowiki></pre>