Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilevich authored Jun 3, 2023
1 parent 2e2bdc3 commit 3e9d2c5
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,49 @@
const {browser: {createBrowser}} = require('mega-scraper')
const args = require('yargs').argv

function encodeSpecialParameters(input) {
const specialParams = input.match(/&[a-zA-Z0-9#]+;/g);
if(specialParams) {
for(const param of specialParams) {
const encoded = Buffer.from(param).toString('base64');
const wrappedEncoded = ` ${encoded} `;
input = input.replace(new RegExp(param, 'g'), wrappedEncoded);
encodeMap.set(param, wrappedEncoded);
decodeMap.set(wrappedEncoded, param);
}
}
return input;
}

// Function to decode special parameters
function decodeSpecialParameters(input) {
const specialParams = input.match(/ [a-zA-Z0-9+/=]+ /g);
if(specialParams) {
for(const param of specialParams) {
const original = decodeMap.get(param);
if(original) {
input = input.replace(new RegExp(param, 'g'), original);
}
}
}
return input;
}

async function translate({text, from = 'auto', to = 'en', browser, page} = {}) {
if (!text) throw new Error('missing text')
if (!from) throw new Error('missing from')
if (!to) throw new Error('missing to')
if (!browser) browser = await createBrowser({headless: true, screenshot: true})

page = page || await browser.newPage()
await page.goto(`https://translate.google.com/?op=translate&sl=${from}&tl=${to}&text=${encodeURIComponent(text)}`)
await page.goto(`https://translate.google.com/?op=translate&sl=${from}&tl=${to}&text=${encodeURIComponent(encodeSpecialParameters(text))}`)
await page.waitForSelector('[jsaction^="copy:"] div span[lang="ko"] span[jsaction^="click:"]', {timeout: 10000})

const translation = await page.evaluate(() => [...document.querySelectorAll('[jsaction^="copy:"] div span[lang="ko"] span[jsaction^="click:"]')].map(e => e.textContent.trim()).filter(s => s !== 'Try again').join(' '))
await page.close()
await browser.instance.close()

return translation
return decodeSpecialParameters(translation)
}

module.exports = translate

0 comments on commit 3e9d2c5

Please sign in to comment.