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
|
||||
|
||||
```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);
|
||||
```
|
||||
|
|
@ -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:
|
||||
```js
|
||||
setGlossary({
|
||||
channel: 'salon'
|
||||
}, 'EN', 'FR');
|
||||
await setGlossary({
|
||||
'channel': 'salon'
|
||||
}, 'en', 'fr');
|
||||
```
|
||||
|
||||
Once a word is added, you can update it by calling the function again:
|
||||
```js
|
||||
setGlossary({
|
||||
channel: 'test'
|
||||
}, 'EN', 'FR');
|
||||
await setGlossary({
|
||||
'channel': 'test'
|
||||
}, '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
|
||||
|
||||
|
|
|
|||
76
index.js
76
index.js
|
|
@ -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) => {
|
||||
|
|
@ -82,7 +84,7 @@ module.exports = {
|
|||
await page.waitForSelector('.lmt--active_translation_request');
|
||||
await page.waitForSelector('.lmt--active_translation_request', { hidden: true });
|
||||
_res.target.translation = await page.$eval(targetSentenceField, el => el.value);
|
||||
page.close().catch(() => {});
|
||||
await page.close();
|
||||
return _res;
|
||||
},
|
||||
clearGlossary: async () => {
|
||||
|
|
@ -90,18 +92,14 @@ module.exports = {
|
|||
await page.goto(homepage);
|
||||
await page.waitForSelector(glossaryButton, { visible: true });
|
||||
await page.click(glossaryButton);
|
||||
await page.evaluate(() => {
|
||||
Array.from(document.querySelectorAll('button[dl-test=glossary-entry-delete-button]')).forEach((element) => {
|
||||
element.click();
|
||||
});
|
||||
});
|
||||
for(const button of await page.$$('button[dl-test=glossary-entry-delete-button]'))
|
||||
await (await page.$('button[dl-test=glossary-entry-delete-button]')).click();
|
||||
},
|
||||
setGlossary: async (glossary, sourceLanguage, targetLanguage) => {
|
||||
sourceLanguage = sourceLanguage.toUpperCase();
|
||||
targetLanguage = targetLanguage.toUpperCase();
|
||||
|
||||
const
|
||||
langSelect = `.lmt__glossary_newEntry_langButton`,
|
||||
sourceGlossaryInput = `.lmt__glossary_newEntry_inputSource`,
|
||||
targetGlossaryInput = `.lmt__glossary_newEntry_inputTarget`,
|
||||
glossaryAddButton = `.lmt__glossary_acceptButton`;
|
||||
|
|
@ -111,28 +109,22 @@ 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);
|
||||
try {
|
||||
await page.evaluate(({
|
||||
sourceLanguage,
|
||||
targetLanguage
|
||||
}) => {
|
||||
const langButtons = Array.from(document.querySelectorAll('button[dl-test=glossary-newentry-lang-dropdown-option]'));
|
||||
const button = langButtons.find((btn) => {
|
||||
const childrens = Array.from(Array.from(btn.children)[1].children);
|
||||
const buttonSourceLanguage = childrens[0].textContent;
|
||||
const buttonTargetLanguage = childrens[2].textContent;
|
||||
return buttonSourceLanguage === sourceLanguage && buttonTargetLanguage === targetLanguage;
|
||||
});
|
||||
button.click();
|
||||
}, {
|
||||
sourceLanguage,
|
||||
targetLanguage
|
||||
});
|
||||
}
|
||||
catch(_){
|
||||
throw new Error('UNSUPPORTED_GLOSSARY_LANGUAGE');
|
||||
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]')){
|
||||
const
|
||||
_sourceLanguage = await (await (await button.$('.langCode')).getProperty('textContent')).jsonValue(),
|
||||
_targetLanguage = await (await (await button.$('.langCode:nth-of-type(2)')).getProperty('textContent')).jsonValue();
|
||||
if(sourceLanguage === _sourceLanguage && targetLanguage === _targetLanguage){
|
||||
await button.click();
|
||||
isSet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!isSet)
|
||||
throw new Error('UNSUPPORTED_GLOSSARY_LANGUAGE');
|
||||
}
|
||||
await page.waitForSelector(sourceGlossaryInput, { visible: true });
|
||||
|
||||
|
|
@ -142,7 +134,7 @@ module.exports = {
|
|||
await page.type(targetGlossaryInput, glossary[word]);
|
||||
await page.click(glossaryAddButton);
|
||||
}
|
||||
page.close().catch(() => {});
|
||||
await page.close();
|
||||
},
|
||||
quit: async () => {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue