Jump to content

User:Arlen22/collectionparser.js

From Wikisource
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// Adapted from User:Pediapress/collection-parser.js
 
function putOutList(list) {
  var articleName, bookName = list[0];
  var out = '{{saved book}}\n';
  out += '== ' + bookName.replace(/_/g, " ") +' ==\n';
  out += ':[[' + bookName.replace(/_/g, " ") + ']]\n';
  for (var i = 1; i < list.length; i++) {
    if (list[i] != null && list[i].match(/^http:/)) {
      articleName = getArticleId(list[i], bookName);
      out += ":[[" + bookName + "/" + articleName + "|" + articleName.replace(/_/g, " ") + "]]\n";
    } else { //kick out empty headlines and those without links
      if ((i < list.length-1 && list[i+1].match(/^http:/)) && list[i] != " ") {
          out += ";" + list[i] + "\n";
      }
    }
  }
  out += "\n[[Category:Collections]]";
 
  //print the collectionmarkup into a textarea
  var collectionName = prompt("Name your collection:", "");
  if (collectionName) {
    var w = window.open(wgServer + "/w/index.php?title=User:" + wgUserName + "/Collections/" + collectionName + "&action=edit");
    if (w.attachEvent) {
      w.attachEvent("onload", function() {refreshTextArea(w, out)});
    } else if (window.addEventListener) {
      w.addEventListener("load", function() {refreshTextArea(w, out)}, false);
    } else {
      w.document.addEventListener("load", function() {refreshTextArea(w, out)}, false);
    }
  }
}
 
function refreshTextArea(w, out) {
  var txt = w.document.getElementById('wpTextbox1');
  txt.value = out;
} 
 
function getArticleId(link, bookName) {
  var linkList = link.split("/");
  var isId = 0;
  var output = "";
  for (var i = 0; i < linkList.length; i++) {
    if (isId == 0 && linkList[i] == bookName) {
      isId++;
    } else {
      if (isId == 1) {
        output += linkList[i];
        isId++;
      } else {
        if (isId > 1) {
          output += "/" + linkList[i];
        }
      }
    }
  }
  return output;
}
 
//look for headlines and links
function parseContent () {
  var bookLinks = initBooklinks();  
  var contDiv = document.getElementById('bodyContent');
  var bodyElements = contDiv.getElementsByTagName("*");
  var element;
 
  for (var i = 0; i < bodyElements.length; i++) {
    element = bodyElements[i];
    if (element.nodeName.match(/^H\d/)) {
      bookLinks[bookLinks.length] = getHeadingText(element);
    } else {
      if (element.nodeName == "A") {
        if (isBookInternalLink(element)) {
          bookLinks[bookLinks.length] = element.href;
        }
      }
    }
  }
  return bookLinks;
}
 
//extracts the text from a headline
function getHeadingText(heading) {
  if (heading.id == "siteSub") {
    return " ";
  } else {
    var elements = heading.childNodes;
    for (var i = 0; i < elements.length; i++) {
      if (elements[i].nodeName == "SPAN" && elements[i].getAttribute("class") == "mw-headline") {
        return elements[i].innerHTML;
      }
    }
  }
  return "chapter";
}
 
function initBooklinks() {
  var bookName = wgPageName.split("/");
  if (bookName.length > 1) {
    return [bookName[0], "http://" + wgPageName];
  } else {
    return [wgPageName];
  }
}
 
//checks links for targets inside the wikibook
function isBookInternalLink(link) {
  var articleBaseURL = mw.config.get('wgServer') + mw.config.get('wgArticlePath');
  var bookName = wgPageName;
  var articleURL = articleBaseURL.substring(0, articleBaseURL.length-2) + bookName.split("/")[0];
  if (link.href.substring(0,articleURL.length) != articleURL || link.href.match(/#/) || link.href.match(/Print_version/i)) {
    return false;
  } else {
    return true;
  }
}
 
//added link has been clicked
function getCollection() {
  var bookLinks = parseContent(); 
  putOutList(bookLinks);
}
 
//add an link at thr right upper corner
function addPediapressLinks() {
  if (wgNamespaceNumber == 0 || wgNamespaceNumber == 102 || wgNamespaceNumber == 110) {
    addPortletLink ('p-personal', 'javascript:getCollection()', 'get collection');
  }
}
 
$(addPediapressLinks);