-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtranslateScript.js
50 lines (43 loc) · 2.01 KB
/
translateScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const json = require('./filesToTranslate.json');
fs = require('fs');
global.fetch = require("node-fetch");
const puppeteer = require('puppeteer');
async function translate(incomingText) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.deepl.com/translator', {waitUntil: 'domcontentloaded'});
await page.evaluate(() => { localStorage.setItem('LMT_preferredLangs', 'EN,NL'); });
await page.evaluate(() => { localStorage.setItem('LMT_preferredLangVariants', 'en-US'); });
await page.evaluate(() => { localStorage.setItem('LMT_selectedTargetLang', 'NL'); });
await page.waitForSelector('.dl_cookieBanner--buttonAll');
await page.click('.dl_cookieBanner--buttonAll');
await page.evaluate(val => document.querySelector('.lmt__source_textarea').value = val, incomingText);
await page.click('.lmt__language_select--target');
await page.evaluate(() => {
[...document.querySelectorAll('.lmt__language_select__menu button')].find(element => element.textContent === 'Dutch').click();
});
await page.waitFor(5000);
let translatedText = await page.$eval('.lmt__target_textarea', el => el.value);
await browser.close();
return [incomingText, translatedText];
}
function startTranslating(url, file){
fetch(url)
.then(response => response.text())
.then(response => translate(response))
.then(response => {
fs.writeFile(file, `${response[0]}\n====================\n${response[1]}`, function (err) {
if (err) return console.log("schrijven mislukt! " + err);
console.log('schrijven gelukt');
});
})
.catch(err => console.log(err));
}
function fetchJSON(){
let files = json.files;
files.forEach(el => {
//console.log();
startTranslating("https://raw.githubusercontent.com/javascript-tutorial/nl.javascript.info/master/"+el.slice(21, el.length), "." + el.slice(20, el.length))
});
}
fetchJSON();