// ==UserScript==
// @name          MemriseUtilities
// @namespace     http://ligature.me
// @version       0.9.1
// @grant         none
// @description   Various helper functions for Memrise and some other EO sites.
// @icon          http://cdn.altrn.tv/icons/memrise_10088.png?width=50&height=50&mode=crop&anchor=middlecenter
// @homepageURL   https://github.com/scytalezero/MemriseUtilities
// @updateURL     https://github.com/scytalezero/MemriseUtilities/raw/master/MemriseUtilities.user.js
// @downloadURL   https://github.com/scytalezero/MemriseUtilities/raw/master/MemriseUtilities.user.js
// @match         http://*.memrise.com/course/*
// @match         http://akademio-de-esperanto.org/akademia_vortaro/*
// @copyright     2012+, ScytaleZero
// ==/UserScript==

switch (document.domain) {
  case "akademio-de-esperanto.org":
    $("<input type='button' value='List Words' />")
      .click(AkademioList)
      .prependTo("h3.trafonombro");
    
    function AkademioList() {
      $("h3.trafonombro").append("<div><textarea id='Wordlist' style='width:800px; height:400px;'></textarea></div>");
      $("#serchorezultotabelo tr").each(function(Index) {
        if (Index === 0) return;
        $("#Wordlist").append( $($(this).find("td")[1]).text() + "\t" + $($(this).find("td")[4]).text() + "\n" );
      });
    }
    break;
  case "www.memrise.com":
    if ($("li.active > a.tab").length === 0) {
      //This isn't a course levels page
      return;
    }

    var Wordlist = {};
    var Promises = [];
    var CourseTag = SanitizeTag($("h1.course-name").text().trim())

    //Insert the terms extractor button
    $("ul.nav-pills").append("<li><a id='WordListButton'>Word list</a></li>");
    $("#WordListButton").click(SpiderLevels);

    //Iterate through the levels and kick off the retrieves.
    function SpiderLevels() {
      //Add our UI
      $("div.container-main").prepend("<div id='WordListContainer'><h2>Working</h2></div>");
      $("a.level").each(function(Index) {
        var URL = "http://www.memrise.com" + $(this).attr("href");
        console.log("Dispatching URL: " + URL);
        //if (Index > 0) return; //Debug
        Promises.push($.ajax({
          dataType: "html",
          url: URL
        }).done(ExtractTerms));
      });
      
      $.when.apply($, Promises).done(SpiderDone);
    }

    //All levels have been parsed and the wordlist is done.
    function SpiderDone() {
      var WordlistText = "", LCV;
      
      console.log(Wordlist);
      //Add the wordlist text area.
      $("#WordListContainer").empty().append("<div><h2>Course Wordlist as TSV</h2><textarea id='Wordlist' style='width:800px; height:400px;'></textarea><p>&nbsp;</p></div>");
      for (var Level in Wordlist) {
        for (LCV=0; LCV<Wordlist[Level].length; LCV++) {
          WordlistText += Wordlist[Level][LCV].Word + "\t" + Wordlist[Level][LCV].Translation + "\t" + 
            CourseTag + "," + SanitizeTag(Level) + "\n";
        }
      }
      $("#Wordlist").val(WordlistText.trimRight());
    }

    //Pull words and definitions from a level page.
    function ExtractTerms(Data) {
      var LevelName = $(Data).find("strong.level-number").text().trim();
      console.log("Level: " + LevelName);
      Wordlist[LevelName] = [];
      $(Data).find("div.text-text").each(function() {
        Wordlist[LevelName].push({
          "Word": $(this).find("div.col_a").text(),
          "Translation": $(this).find("div.col_b").text()
        });
      });
      $("#WordListContainer h2").append(".");
    }

    //Format text appropriately for LWT tags.
    function SanitizeTag(Buffer) {
      var Tag = Buffer.replace(/[^\w]/g, "");
      if (Tag.length > 20) Tag = Tag.substring(0, 20);
      return Tag;
    }
    break;
}

@@ -105,17 +105,17 @@
     0.9.
-1
+2
 %0A// @gra
@@ -1362,16 +1362,18 @@
 :%0A    if
+ (
  ($(%22li.
@@ -1402,16 +1402,55 @@
 h === 0)
+ %7C%7C ($(%22.container %3E h2%22).length %3E 0) )
  %7B%0A     

Exclude Memrise edit page