Memrise LaTeX Integration

This will convert any text that looks like LaTeX inline math (beginning and ending with $) into an image of a well-formatted math expression

  1. // ==UserScript==
  2. // @name Memrise LaTeX Integration
  3. // @description This will convert any text that looks like LaTeX inline math (beginning and ending with $) into an image of a well-formatted math expression
  4. // @match http://www.memrise.com/course/*/*/
  5. // @match http://www.memrise.com/course/*/garden/*
  6. // @match http://www.memrise.com/garden/*
  7. // @require https://gf.qytechs.cn/scripts/5908-cross-platform-object-watch-js/code/cross-platform-objectwatchjs.js?version=22033
  8. // @version 1.0
  9. // @namespace https://gf.qytechs.cn/users/5238-carpiediem
  10. // @grant none
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. //jsLaTeX v1.2.2 - jQuery plugin Copyright (c) 2009 Andreas Grech <https://github.com/dreasgrech/jsLatex>
  15. (function($){var attachToImage=function(){return $("<img/>").attr({src:this.src})},formats={'gif':attachToImage,'png':attachToImage,'swf':function(){return $("<embed/>").attr({src:this.src,type:'application/x-shockwave-flash'})}},sections={'{f}':'format','{e}':'equation'},escapes={'+':'2B','=':'3D'};$.fn.latex=function(opts){opts=$.extend({},$.fn.latex.defaults,opts);opts.format=formats[opts.format]?opts.format:'gif';return this.each(function(){var $this=$(this),format,s,element,url=opts.url;opts.equation=$.trim($this.text());for(s in sections){if(sections.hasOwnProperty(s)&&(format=url.indexOf(s))>=0){url=url.replace(s,opts[sections[s]])}}for(s in escapes){if(escapes.hasOwnProperty(s)&&(format=url.indexOf(s))>=0){url=url.replace(s,'%'+escapes[s])}}opts.src=url;element=formats[opts.format].call(opts);$this.html('').append(element);if(opts.callback){opts.callback.call(element)}})};$.fn.latex.defaults={format:'gif',url:'http://latex.codecogs.com/{f}.latex?{e}'}}(jQuery));
  16.  
  17. var re_LaTeX = /^\s*\$(.+)\$\s*$/;
  18.  
  19. // On the level list page, check each row in both columns A & B
  20. var levelListCells = $(".col_a div, .col_b div");
  21. for (var i=0; i<levelListCells.size(); i++) {
  22. var reArr = levelListCells.eq(i).text().match(re_LaTeX);
  23. if (reArr) levelListCells.eq(i).text(reArr[1]).latex({format:"png"});
  24. }
  25.  
  26.  
  27. // On the garden page, trigger every time the page content changes
  28. if (typeof(MEMRISE.garden)!="undefined") MEMRISE.garden.watch('box', function (id, oldval, newval) {
  29. if (typeof(newval)==undefined) return false;
  30. //console.log('The box template is now ' + newval.box_dict.template);
  31. setTimeout(function(){
  32. var gardenText = $("div.row-value, div.qquestion, li.choice span.val");
  33. for (var i=0; i<gardenText.size(); i++) {
  34. var reArr = gardenText.eq(i).text().match(re_LaTeX);
  35. if (reArr) {
  36. gardenText.eq(i).text( gardenText.eq(i).text().replace(/\$/g,"") ); //For some reason, $ are pushed into the image URL in this case, but not on the level list
  37. gardenText.eq(i).latex({format:"png"});
  38. }
  39. }
  40. }, 200);
  41. return newval;
  42. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址