[Userscript] Display dictionary sites in the Memrise presentation screen

This displays dictionary iframes in the Memrise presentation screen. I used a different script as a base. It works but anyone familiar with Javascript(?) will know the original script writer knew what they were doing and I don’t, and this could have been done so much easier.

Anyone feel like looking at it and deleting the extra stuff? @cooljingle or @DrewSSP or @neoncube perhaps?

// ==UserScript==
// @name           Dictionary pages in Memrise presentation screen. Can be changed to work with other languages.
// @description    your description here
// @match          http://www.memrise.com/course/*/learn/*
// @match          http://www.memrise.com/garden/review/*
// @match          http://www.mdbg.net/chindict/chindict_ajax.php*
// @version        0.1
// @updateURL      none
// @downloadURL    none
// @grant          none
// ==/UserScript==


var onLoad = function($) {
  $("#central-area").on("DOMSubtreeModified", function() {  //trigger many times
    if ($("#otherRefs").size()==0 && MEMRISE.garden.session.category.name=="Chinese (Simplified)") {                         //only triggered once
      var hanzi = MEMRISE.garden.box.thing.columns[1].val;
      
// Change "Chinese (Simplified)" above to the language you are studying 
      
      
      // Append the HTML
      $(".columns").append('<div class="row column  on-show-more"><div class="row-label">Other References</div><div id="otherRefs" class="row-value"><div id="strokeIcons"></div><div id="decompIcons"></div></div>'+
                           
                           // Begin edits by Arete_Hime
                                                          
                           '<style>'+
                           
                           // Width of the wrapper div. You may want to increase this.
                          'div.mywrapper{position: relative;left: -2%; overflow: hidden; margin: 0%; width: 104%; background-color:ivory;}'+
                           
                           // styling for: iframe for the dictionary, half width
                           '.half-width-iframe {width: 49.4%;}'+
                           
                           // styling for: iframe for the dictionary, whole width
                           '.whole-width-iframe {width: 99.4%;}'+
                           
                           // styling for: both half width and whole width iframes
                           '.half-width-iframe, .whole-width-iframe{background: rgba(255,255,255,0.8) !important;height:700px; margin:0%;}'+
                           
                                               
                               
                           // hide stuff that the rest of this script tries to show...
                           '#otherRefs, #strokeIcons, #decompIcons{display:none;}'+
                           
                                                                              
                           '</style>'+
                        
                                                     
                           '<div class="mywrapper">'+
                                          
                           //your dictionary sites here
                           '<iframe class="half-width-iframe" src="http://dj.iciba.com/'+ hanzi +'-1.html"></iframe>'+      
                           '<iframe class="half-width-iframe" src="http://www.mdbg.net/chindict/chindict.php?wdqb=c:*'+ hanzi +'*"></iframe>'+  
                                                     
                           
                           '</div>'+  
                           '</div>'+  
                           '</div>'+
                                                                                 
                           //End edits by Arete_Hime
                           
                           '</div></div></div>');
      
      
      
      //Arete_Hime: I have zero knowledge of the below, so I don't know how to touch this
      

      if (hanzi.length<=3) for (var i=0; i<hanzi.length; i++){
        $("#strokeIcons").append('<div id="icon4stroke'+hanzi.substring(i,i+1)+'" class="tile" title="Stroke Order">'           +hanzi.substring(i,i+1)+'</div>');
        $("#decompIcons").append('<div id="icon4decomp'+hanzi.substring(i,i+1)+'" class="tile" title="Character Decomposition">'+hanzi.substring(i,i+1)+'</div>');
        $("#otherRefs"  ).append('<img id="stroke'     +hanzi.substring(i,i+1)+'" class="mdbg" src="http://www.mdbg.net/chindict/rsc/img/stroke_anim/'       + hanzi.substring(i,i+1).charCodeAt(0) +'.gif"/>'+
                                 '<iframe id="decomp'  +hanzi.substring(i,i+1)+'" class="mdbg" src="http://www.mdbg.net/chindict/chindict_ajax.php?c=cdcd&i='+ hanzi.substring(i,i+1)+'"></iframe>');
      }
      
      //Add styles & mouse events
      $("#strokeIcons .tile").css({background:"white url(\'https://openclipart.org/people/warszawianka/tango-style-paintbrush.svg\') no-repeat right top"});
      $("#decompIcons .tile").css({background:"white url(\'http://repurposed.files.wordpress.com/2009/08/scissors.jpg\'            ) no-repeat right top",textAlign:"center"});
      $("             .tile").css({float:"left",width:"50px",height:"50px",color:"red"})
                             .mouseover(function(){$( "#"+$(this).attr("id").substring(5) ).show().offset( $(this).offset() )});
        
      $(".mdbg" ).css({display:"none",position:"absolute",top:"200px",left:"200px","z-index":1000,width:"250px",height:"250px"})
                 .mouseleave(function(){console.log(this);$(this).hide()});
    
    }
  });
};

if (location.hostname=="www.mdbg.net") {
  // Handle the inline frame
  document.body.style.backgroundColor = "white";
  document.getElementsByClassName("characterdecomposition")[0].style.fontSize = "1.8em";
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    anchors[i].target = "_blank";
    anchors[i].style.color          = "#008";
    anchors[i].style.textDecoration = "none";
  }
  
}else{
  // Handle the main page at Memrise.com
  var injectWithJQ = function(f) {
    var script = document.createElement('script');
    script.textContent = '$(' + f.toString() + '($));';
    document.body.appendChild(script);
  };
  injectWithJQ(onLoad);
}

Could you please point me to where you got the original code?

I’m taking a quick look at it and so far all I want to say is that you should have confidence in what you’ve done here. Like perfectly written book, perfectly written code does not exist. Yes, you can make code more efficient and sometimes that is necessary but those cases are for much larger projects. Your code is good. It works, it doesn’t hurt anything, and it’s useful. Be proud of yourself!

3 Likes

Thanks :slight_smile: The original is @carpiediem’s Memrise with MDBG Dictionary Data

For it to work, you need to change “Chinese” to “Chinese (Simplified)”, and one of the Memrise @matches – corrected below.

You’ll see that I changed nothing (I think) because I don’t know how. In the past when I tried to delete stuff, the script stopped working. I only added some styling for the iframes and the iframes themselves.

http://userscripts-mirror.org/scripts/review/175657

// ==UserScript==
// @name           Memrise with MDBG Dictionary Data
// @description    Adds character decomposition diagrams, stroke order images, and links to other resourses to every review page in Chinese courses
// @match          http://www.memrise.com/course/*/garden/*
// @match          http://www.memrise.com/garden/review/*
// @match          http://www.mdbg.net/chindict/chindict_ajax.php*
// @version        0.6
// @updateURL      https://userscripts.org/scripts/source/175657.meta.js
// @downloadURL    https://userscripts.org/scripts/source/175657.user.js
// @grant          none
// ==/UserScript==

var onLoad = function($) {
  $("#central-area").on("DOMSubtreeModified", function() {  //trigger many times
    if ($("#otherRefs").size()==0 && MEMRISE.garden.session.category.name=="Chinese (Simplified)") {                         //only triggered once
      var hanzi = MEMRISE.garden.box.thing.columns[1].val;
      
      // Append the HTML
      $(".columns").append('<div class="row column  on-show-more"><div class="row-label">Other References</div><div id="otherRefs" class="row-value"><div id="strokeIcons"></div><div id="decompIcons"></div><ul style="float:left;font:0.6em sans-serif;">'+
                           '<li><a target="_blank" href="http://www.mdbg.net/chindict/chindict.php?wdqb=c:*'+ hanzi +'*">MBDG Dictionary</a></li>'+
                           '<li><a target="_blank" href="http://hanzicraft.com/character/'+ hanzi +'">HanZiCraft</a></li>'+
                           '<li><a target="_blank" href="https://en.wiktionary.org/wiki/'+ hanzi +'">Wiktionary</a></li>'+
                           '<li><a target="_blank" href="http://translate.google.com/?source=osdd#zh-CN/en/'+ hanzi +'">Google Translate</a></li>'+
                           '</ul></div></div></div>');

      if (hanzi.length<=3) for (var i=0; i<hanzi.length; i++){
        $("#strokeIcons").append('<div id="icon4stroke'+hanzi.substring(i,i+1)+'" class="tile" title="Stroke Order">'           +hanzi.substring(i,i+1)+'</div>');
        $("#decompIcons").append('<div id="icon4decomp'+hanzi.substring(i,i+1)+'" class="tile" title="Character Decomposition">'+hanzi.substring(i,i+1)+'</div>');
        $("#otherRefs"  ).append('<img id="stroke'     +hanzi.substring(i,i+1)+'" class="mdbg" src="http://www.mdbg.net/chindict/rsc/img/stroke_anim/'       + hanzi.substring(i,i+1).charCodeAt(0) +'.gif"/>'+
                                 '<iframe id="decomp'  +hanzi.substring(i,i+1)+'" class="mdbg" src="http://www.mdbg.net/chindict/chindict_ajax.php?c=cdcd&i='+ hanzi.substring(i,i+1)+'"></iframe>');
      }
      
      //Add styles & mouse events
      $("#strokeIcons .tile").css({background:"white url(\'https://openclipart.org/people/warszawianka/tango-style-paintbrush.svg\') no-repeat right top"});
      $("#decompIcons .tile").css({background:"white url(\'http://repurposed.files.wordpress.com/2009/08/scissors.jpg\'            ) no-repeat right top",textAlign:"center"});
      $("             .tile").css({float:"left",width:"50px",height:"50px",color:"red"})
                             .mouseover(function(){$( "#"+$(this).attr("id").substring(5) ).show().offset( $(this).offset() )});
        
      $(".mdbg" ).css({display:"none",position:"absolute",top:"200px",left:"200px","z-index":1000,width:"250px",height:"250px"})
                 .mouseleave(function(){console.log(this);$(this).hide()});
    
    }
  });
};

if (location.hostname=="www.mdbg.net") {
  // Handle the inline frame
  document.body.style.backgroundColor = "white";
  document.getElementsByClassName("characterdecomposition")[0].style.fontSize = "1.8em";
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    anchors[i].target = "_blank";
    anchors[i].style.color          = "#008";
    anchors[i].style.textDecoration = "none";
  }
  
}else{
  // Handle the main page at Memrise.com
  var injectWithJQ = function(f) {
    var script = document.createElement('script');
    script.textContent = '$(' + f.toString() + '($));';
    document.body.appendChild(script);
  };
  injectWithJQ(onLoad);
}

this should work:

// ==UserScript==
// @name           Dictionary pages in Memrise presentation screen. Can be changed to work with other languages.
// @description    your description here
// @match          https://www.memrise.com/course/*/learn/*
// @match          https://www.memrise.com/garden/review/*
// @match          http://www.mdbg.net/chindict/chindict_ajax.php*
// @version        0.1
// @updateURL      none
// @downloadURL    none
// @grant          none
// ==/UserScript==


var onLoad = function($) {
  $("#central-area").on("DOMSubtreeModified", function() {  //trigger many times
    if ($("#otherRefs").size()==0 && MEMRISE.garden.session.category.name=="Chinese (Simplified)") {                         //only triggered once
      var hanzi = MEMRISE.garden.box.thing.columns[1].val;
      
// Change "Chinese (Simplified)" above to the language you are studying 
      
      
      // Append the HTML
      $(".columns").append('<div class="row column  on-show-more"><div class="row-label">Other References</div><div id="otherRefs" class="row-value"></div></div>'+
                           
                           // Begin edits by Arete_Hime
                                                          
                           '<style>'+
                           
                           // Width of the wrapper div. You may want to increase this.
                          'div.mywrapper{position: relative;left: -2%; overflow: hidden; margin: 0%; width: 104%; background-color:ivory;}'+
                           
                           // styling for: iframe for the dictionary, half width
                           '.half-width-iframe {width: 49.4%;}'+
                           
                           // styling for: iframe for the dictionary, whole width
                           '.whole-width-iframe {width: 99.4%;}'+
                           
                           // styling for: both half width and whole width iframes
                           '.half-width-iframe, .whole-width-iframe{background: rgba(255,255,255,0.8) !important;height:700px; margin:0%;}'+
                                                                              
                           '</style>'+
                        
                                                     
                           '<div class="mywrapper">'+
                                          
                           //your dictionary sites here
                           '<iframe class="half-width-iframe" src="http://dj.iciba.com/'+ hanzi +'-1.html"></iframe>'+      
                           '<iframe class="half-width-iframe" src="http://www.mdbg.net/chindict/chindict.php?wdqb=c:*'+ hanzi +'*"></iframe>'+  
                                                     
                           
                           '</div>'
                                                                                 
                           //End edits by Arete_Hime
                          );
    }
  });
};

// Handle the main page at Memrise.com
var injectWithJQ = function(f) {
var script = document.createElement('script');
script.textContent = '$(' + f.toString() + '($));';
document.body.appendChild(script);
};
injectWithJQ(onLoad);

Note that the script relies on the first column of the course containing the Chinese, if this is not the case for some of your courses I can provide a function to work out which column to use (had to deal with that in my example sentences userscript).

1 Like