forked from KaKi87/deepl-scraper
Update glossary support #1
2 changed files with 68 additions and 5 deletions
42
README.md
42
README.md
|
|
@ -30,7 +30,13 @@ sudo sysctl -w kernel.unprivileged_userns_clone=1
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
```js
|
```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);
|
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.
|
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
|
#### 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 :
|
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 :
|
||||||
|
|
|
||||||
31
index.js
31
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) => {
|
||||||
|
|
@ -98,7 +100,6 @@ module.exports = {
|
||||||
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`;
|
||||||
|
|
@ -108,8 +109,8 @@ 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);
|
||||||
{
|
{
|
||||||
let isSet = false;
|
let isSet = false;
|
||||||
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue