Skip to content

Instantly share code, notes, and snippets.

@fbecart
Last active May 1, 2024 19:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fbecart/7abfde329e3f536160c0af1016e02b8b to your computer and use it in GitHub Desktop.
Save fbecart/7abfde329e3f536160c0af1016e02b8b to your computer and use it in GitHub Desktop.
Bookmarklet to export Leo Trainer words to an Anki deck.
/**
* This bookmarklet makes it possible to transfer words from Leo Trainer to Anki.
* It is configured for fr-de (French to German) translations, but can easily be
* adapted to other languages.
*
* Prerequisites:
* - a Leo account (http://www.leo.org/) with a few words saved in the trainer
* - an Anki account (https://ankiweb.net/account/register)
*
* 1. Crunch the following code and add it to your bookmarks
* (see http://ted.mielczarek.org/code/mozilla/bookmarklet.html)
* 2. Run the bookmarklet
* (you might have to run it twice, as the first run will lead you to the correct Leo page)
* 3. Import the downloaded file in Anki using \t as field separator and allowing HTML in fields
**/
// This is where you can adapt the script for other languages.
// `fromLang` is the language you know already, whereas `toLang` is the language you're learning.
const fromLang = 'fr';
const toLang = 'de';
function extractVocabulary() {
const output = [];
const matches = document.body.querySelectorAll('.tb-bg-alt-lightgray > tbody > tr');
for (let index = 0; index < matches.length; index++) {
const item = matches[index];
output.push([
item.querySelector(`td[lang=${fromLang}]`).innerHTML,
item.querySelector(`td[lang=${toLang}]`).innerHTML,
]);
}
return output;
}
function downloadTsvFile(filename, content) {
const tsv = content.reduce((acc, cur) => acc + cur.join('\t') + '\n', '');
const a = document.createElement('a');
a.setAttribute('href', 'data:text/tab-separated-values;charset=utf-8,' + encodeURIComponent(tsv));
a.setAttribute('download', filename);
document.body.appendChild(a);
a.click();
}
const manageFolderUrl = 'https://dict.leo.org/trainer/manageFolder.php' +
`?lp=${fromLang}${toLang}&lang=${toLang}`;
if (location != manageFolderUrl) {
location = manageFolderUrl;
} else {
downloadTsvFile('vokabeln.csv', extractVocabulary());
}
@kuelshammer
Copy link

Hi Florent,

I would like to use your bookmarklet to create Anki Cards for my son. We are from Germany and he learns French. I followed your instructions but can't find the csv-File named vokabeln.csv...

Are you sure that bookmarklet is still working?

Max

@fbecart
Copy link
Author

fbecart commented Sep 5, 2019

Hi Max,

Thanks for trying this script and leaving some feedback. It appears that Leo updated their HTML, and as a result the script stopped working. I am not using this script myself at the moment, and I hadn't realized there was an issue.

I updated the query selector line 16, and it should fix the problem. Would you mind trying again and letting me know how it goes?

Best,
Florent

@kuelshammer
Copy link

Hi Florent,

now your bookmarklet works. Would it be possible to extract the sound files form LEO?

Max

@fbecart
Copy link
Author

fbecart commented Sep 8, 2019

Hi Max,

I'm happy it worked for you. Thanks for the bug report, hopefully the script will serve more people thanks to you.

I took a brief look at the sound files. It seems doable to extract them too, but it would require additional work.
As I stopped using the script some time ago, I don't have any incentive to bring such a change at the moment. But this is definitely a good improvement idea.

Cheers,
Florent

@lamyergeier
Copy link

@fbecart Hi Florent

It stopped working again. For English to German Dictionary, I replaced fr to en as follows:

/**
 * Source: [Bookmarklet to export Leo Trainer words to an Anki deck.](https://gist.github.com/fbecart/7abfde329e3f536160c0af1016e02b8b)
 * This bookmarklet makes it possible to transfer words from Leo Trainer to Anki.
 * It only works for en-de translations. Contact me for other languages.
 *
 * Prerequisites:
 * - a Leo account (http://www.leo.org/) with a few words saved in the trainer
 * - an Anki account (https://ankiweb.net/account/register)
 *
 * 1. Crunch the following code and add it to your bookmarks http://ted.mielczarek.org/code/mozilla/bookmarklet.html
 * 2. Run the bookmarklet on this page https://dict.leo.org/trainer/manageFolder.php?lp=ende&lang=en
 * 3. Import the downloaded file in Anki using \t as field separator and allowing HTML in fields
 **/

function extractVocabulary() {
  var output = []
  var matches = document.body.querySelectorAll('.tb-bg-alt-lightgray > tbody > tr')

  for (var index = 0; index < matches.length; index++) {
    let item = matches[index]
    output.push([item.querySelector('td[lang=en]').innerHTML, item.querySelector('td[lang=de]').innerHTML])
  }

  return output
}

function downloadTsvFile(filename, content) {
  var tsv = content.reduce(function (acc, cur) {
    return acc + cur.join('\t') + '\n'
  }, '')

  var a = document.createElement('a')
  a.setAttribute('href', 'data:text/tab-separated-values;charset=utf-8,' + encodeURIComponent(tsv))
  a.setAttribute('download', filename)
  document.body.appendChild(a)
  a.click()
}

var manageFolderUrl = 'https://dict.leo.org/trainer/manageFolder.php?lp=ende&lang=en'
if (location != manageFolderUrl) location = manageFolderUrl
else downloadTsvFile('vokabeln.csv', extractVocabulary())

@fbecart
Copy link
Author

fbecart commented Jan 18, 2021

@anishmittal2020 Your adaptation is correct except for the manageFolderUrl which should be 'https://dict.leo.org/trainer/manageFolder.php?lp=ende&lang=de'.

@eriwst
Copy link

eriwst commented Feb 4, 2021

Dear Florent,

I would also like to export the English-German vocabulary from the LEO Trainer. May you please provide a working solution?

Kind regards

Eric

@fbecart
Copy link
Author

fbecart commented Feb 4, 2021

Hi @eriwst, I updated the code to make it easier to customize the languages. Is that working for you?

@paulwiniecki
Copy link

Hi @fbecart,

I'm hoping to use this process to download my Leo dictionary and add it to Anki (English to German). However, nothing seems to happen when using the follow script as a bookmarklet. Does anything need to be updated? All your help is much appreciated!

`/**

function extractVocabulary() {
var output = []
var matches = document.body.querySelectorAll('.tb-bg-alt-lightgray > tbody > tr')

for (var index = 0; index < matches.length; index++) {
let item = matches[index]
output.push([item.querySelector('td[lang=en]').innerHTML, item.querySelector('td[lang=de]').innerHTML])
}

return output
}

function downloadTsvFile(filename, content) {
var tsv = content.reduce(function (acc, cur) {
return acc + cur.join('\t') + '\n'
}, '')

var a = document.createElement('a')
a.setAttribute('href', 'data:text/tab-separated-values;charset=utf-8,' + encodeURIComponent(tsv))
a.setAttribute('download', filename)
document.body.appendChild(a)
a.click()
}

var manageFolderUrl = 'https://dict.leo.org/trainer/manageFolder.php?lp=ende&lang=de'
if (location != manageFolderUrl) location = manageFolderUrl
else downloadTsvFile('vokabeln.csv', extractVocabulary())`

@fbecart
Copy link
Author

fbecart commented Oct 12, 2023

@paulwiniecki I just tested the bookmarklet, and it seems to be still working.

The code snippet you posted is hard to read because of incorrect markdown formatting. I can see however that you have made more modifications than needed, and this is likely the cause of your troubles.

I suggest you restart from the code I shared at the very top. You only need to change line 19, so that it looks like:

const fromLang = 'en';

@paulwiniecki
Copy link

@fbecart thank you so much for your prompt response. I was able to get it to work following your instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment