Update glossary support #1

Merged
Androz2091 merged 9 commits from KaKi87/deepl-scraper:glossary-v2 into glossary-v2 2021-03-18 17:13:53 +01:00
2 changed files with 68 additions and 5 deletions
Showing only changes of commit c2b4e002c8 - Show all commits

View file

@ -30,7 +30,13 @@ sudo sysctl -w kernel.unprivileged_userns_clone=1
### Usage
```js
const { translate, getSupportedLanguages, setGlossary, quit } = require('deepl-scraper');
const {
translate,
getSupportedLanguages,
setGlossary,
getSupportedGlossaryLanguages,
quit
} = require('deepl-scraper');
translate(sentence, source, target).then(console.log).catch(console.error);
```
@ -107,6 +113,40 @@ await setGlossary({
Note: at this time, the only way to reset the glossary is to quit the browser.
As glossary doesn't support all translation-supported languages, you can get the list using the following method :
```js
await getSupportedGlossaryLanguages();
```
```json
[
{
"sourceLanguage": "en",
"targetLanguage": "de"
},
{
"sourceLanguage": "de",
"targetLanguage": "en"
},
{
"sourceLanguage": "en",
"targetLanguage": "fr"
},
{
"sourceLanguage": "fr",
"targetLanguage": "en"
},
{
"sourceLanguage": "en",
"targetLanguage": "es"
},
{
"sourceLanguage": "es",
"targetLanguage": "en"
}
]
```
#### Quitting
Since this module uses a headless browser, it won't quit as long as your main script is ronning or until you quit it using the following code :

View file

@ -11,7 +11,9 @@ const getBrowser = async () => {
const getNewPage = async () => await (await getBrowser()).newPage();
const glossaryButton = `.lmt__glossary_onOffSwitch_label`;
const
glossaryButton = `.lmt__glossary_onOffSwitch_label`,
glossaryLangSelect = `.lmt__glossary_newEntry_langButton`;
module.exports = {
translate: async (sentence, sourceLanguage = 'auto', targetLanguage) => {
@ -98,7 +100,6 @@ module.exports = {
targetLanguage = targetLanguage.toUpperCase();
const
langSelect = `.lmt__glossary_newEntry_langButton`,
sourceGlossaryInput = `.lmt__glossary_newEntry_inputSource`,
targetGlossaryInput = `.lmt__glossary_newEntry_inputTarget`,
glossaryAddButton = `.lmt__glossary_acceptButton`;
@ -108,8 +109,8 @@ module.exports = {
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
await page.waitForSelector(langSelect, { visible: true });
await page.click(langSelect);
await page.waitForSelector(glossaryLangSelect, { visible: true });
await page.click(glossaryLangSelect);
{
let isSet = false;
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
@ -170,6 +171,28 @@ module.exports = {
}
}
}
return res;
},
getSupportedGlossaryLanguages: async () => {
const page = await getNewPage();
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
await page.waitForSelector(glossaryLangSelect, { visible: true });
await page.click(glossaryLangSelect);
const res = [];
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
res.push({
sourceLanguage: (await (await (await button.$('.langCode')).getProperty('textContent')).jsonValue()).toLowerCase(),
targetLanguage: (await (await (await button.$('.langCode:nth-of-type(2)')).getProperty('textContent')).jsonValue()).toLowerCase()
});
}
await page.close();
return res;
}
};