forked from KaKi87/deepl-scraper
Update glossary support #1
2 changed files with 99 additions and 41 deletions
60
README.md
60
README.md
|
|
@ -30,7 +30,14 @@ sudo sysctl -w kernel.unprivileged_userns_clone=1
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { translate, getSupportedLanguages, setGlossary, quit } = require('deepl-scraper');
|
const {
|
||||||
|
translate,
|
||||||
|
getSupportedLanguages,
|
||||||
|
setGlossary,
|
||||||
|
clearGlossary,
|
||||||
|
getSupportedGlossaryLanguages,
|
||||||
|
quit
|
||||||
|
} = require('deepl-scraper');
|
||||||
|
|
||||||
translate(sentence, source, target).then(console.log).catch(console.error);
|
translate(sentence, source, target).then(console.log).catch(console.error);
|
||||||
```
|
```
|
||||||
|
|
@ -93,19 +100,56 @@ DeepL allows you to define a glossary, so you can add your own words in the tran
|
||||||
|
|
||||||
You can add new words in the glossary using:
|
You can add new words in the glossary using:
|
||||||
```js
|
```js
|
||||||
setGlossary({
|
await setGlossary({
|
||||||
channel: 'salon'
|
'channel': 'salon'
|
||||||
}, 'EN', 'FR');
|
}, 'en', 'fr');
|
||||||
```
|
```
|
||||||
|
|
||||||
Once a word is added, you can update it by calling the function again:
|
Once a word is added, you can update it by calling the function again:
|
||||||
```js
|
```js
|
||||||
setGlossary({
|
await setGlossary({
|
||||||
channel: 'test'
|
'channel': 'test'
|
||||||
}, 'EN', 'FR');
|
}, 'en', 'fr');
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: at this time, the only way to reset the glossary is to quit the browser.
|
Glossary is cleared when you quit the browser, although it can be cleared earlier using the following method :
|
||||||
|
```js
|
||||||
|
await clearGlossary();
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
#### Quitting
|
||||||
|
|
||||||
|
|
|
||||||
72
index.js
72
index.js
|
|
@ -11,7 +11,9 @@ const getBrowser = async () => {
|
||||||
|
|
||||||
const getNewPage = async () => await (await getBrowser()).newPage();
|
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 = {
|
module.exports = {
|
||||||
translate: async (sentence, sourceLanguage = 'auto', targetLanguage) => {
|
translate: async (sentence, sourceLanguage = 'auto', targetLanguage) => {
|
||||||
|
|
@ -82,7 +84,7 @@ module.exports = {
|
||||||
await page.waitForSelector('.lmt--active_translation_request');
|
await page.waitForSelector('.lmt--active_translation_request');
|
||||||
await page.waitForSelector('.lmt--active_translation_request', { hidden: true });
|
await page.waitForSelector('.lmt--active_translation_request', { hidden: true });
|
||||||
_res.target.translation = await page.$eval(targetSentenceField, el => el.value);
|
_res.target.translation = await page.$eval(targetSentenceField, el => el.value);
|
||||||
page.close().catch(() => {});
|
await page.close();
|
||||||
return _res;
|
return _res;
|
||||||
},
|
},
|
||||||
clearGlossary: async () => {
|
clearGlossary: async () => {
|
||||||
|
|
@ -90,18 +92,14 @@ module.exports = {
|
||||||
await page.goto(homepage);
|
await page.goto(homepage);
|
||||||
await page.waitForSelector(glossaryButton, { visible: true });
|
await page.waitForSelector(glossaryButton, { visible: true });
|
||||||
await page.click(glossaryButton);
|
await page.click(glossaryButton);
|
||||||
await page.evaluate(() => {
|
for(const button of await page.$$('button[dl-test=glossary-entry-delete-button]'))
|
||||||
Array.from(document.querySelectorAll('button[dl-test=glossary-entry-delete-button]')).forEach((element) => {
|
await (await page.$('button[dl-test=glossary-entry-delete-button]')).click();
|
||||||
element.click();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
setGlossary: async (glossary, sourceLanguage, targetLanguage) => {
|
setGlossary: async (glossary, sourceLanguage, targetLanguage) => {
|
||||||
sourceLanguage = sourceLanguage.toUpperCase();
|
sourceLanguage = sourceLanguage.toUpperCase();
|
||||||
targetLanguage = targetLanguage.toUpperCase();
|
targetLanguage = targetLanguage.toUpperCase();
|
||||||
|
|
||||||
const
|
const
|
||||||
langSelect = `.lmt__glossary_newEntry_langButton`,
|
|
||||||
sourceGlossaryInput = `.lmt__glossary_newEntry_inputSource`,
|
sourceGlossaryInput = `.lmt__glossary_newEntry_inputSource`,
|
||||||
targetGlossaryInput = `.lmt__glossary_newEntry_inputTarget`,
|
targetGlossaryInput = `.lmt__glossary_newEntry_inputTarget`,
|
||||||
glossaryAddButton = `.lmt__glossary_acceptButton`;
|
glossaryAddButton = `.lmt__glossary_acceptButton`;
|
||||||
|
|
@ -111,27 +109,21 @@ module.exports = {
|
||||||
await page.goto(homepage);
|
await page.goto(homepage);
|
||||||
await page.waitForSelector(glossaryButton, { visible: true });
|
await page.waitForSelector(glossaryButton, { visible: true });
|
||||||
await page.click(glossaryButton);
|
await page.click(glossaryButton);
|
||||||
await page.waitForSelector(langSelect, { visible: true });
|
await page.waitForSelector(glossaryLangSelect, { visible: true });
|
||||||
await page.click(langSelect);
|
await page.click(glossaryLangSelect);
|
||||||
try {
|
{
|
||||||
await page.evaluate(({
|
let isSet = false;
|
||||||
sourceLanguage,
|
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
|
||||||
targetLanguage
|
const
|
||||||
}) => {
|
_sourceLanguage = await (await (await button.$('.langCode')).getProperty('textContent')).jsonValue(),
|
||||||
const langButtons = Array.from(document.querySelectorAll('button[dl-test=glossary-newentry-lang-dropdown-option]'));
|
_targetLanguage = await (await (await button.$('.langCode:nth-of-type(2)')).getProperty('textContent')).jsonValue();
|
||||||
const button = langButtons.find((btn) => {
|
if(sourceLanguage === _sourceLanguage && targetLanguage === _targetLanguage){
|
||||||
const childrens = Array.from(Array.from(btn.children)[1].children);
|
await button.click();
|
||||||
const buttonSourceLanguage = childrens[0].textContent;
|
isSet = true;
|
||||||
const buttonTargetLanguage = childrens[2].textContent;
|
break;
|
||||||
return buttonSourceLanguage === sourceLanguage && buttonTargetLanguage === targetLanguage;
|
|
||||||
});
|
|
||||||
button.click();
|
|
||||||
}, {
|
|
||||||
sourceLanguage,
|
|
||||||
targetLanguage
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
catch(_){
|
}
|
||||||
|
if(!isSet)
|
||||||
throw new Error('UNSUPPORTED_GLOSSARY_LANGUAGE');
|
throw new Error('UNSUPPORTED_GLOSSARY_LANGUAGE');
|
||||||
}
|
}
|
||||||
await page.waitForSelector(sourceGlossaryInput, { visible: true });
|
await page.waitForSelector(sourceGlossaryInput, { visible: true });
|
||||||
|
|
@ -142,7 +134,7 @@ module.exports = {
|
||||||
await page.type(targetGlossaryInput, glossary[word]);
|
await page.type(targetGlossaryInput, glossary[word]);
|
||||||
await page.click(glossaryAddButton);
|
await page.click(glossaryAddButton);
|
||||||
}
|
}
|
||||||
page.close().catch(() => {});
|
await page.close();
|
||||||
},
|
},
|
||||||
quit: async () => {
|
quit: async () => {
|
||||||
if(browser)
|
if(browser)
|
||||||
|
|
@ -179,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;
|
return res;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue