Last active
July 31, 2020 05:12
-
-
Save anhdiepmmk/57b02246684460932b12b8cd6f60389f to your computer and use it in GitHub Desktop.
Đoạn script auto translate nho nhỏ viết bằng nodejs và puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
msgid "人間" | |
msgstr "" | |
msgid "人" | |
msgstr "" | |
msgid "男" | |
msgstr "" | |
msgid "男の人" | |
msgstr "" | |
msgid "男の子" | |
msgstr "" | |
msgid "女" | |
msgstr "" | |
msgid "女の人" | |
msgstr "" | |
msgid "女の子" | |
msgstr "" | |
msgid "赤ちゃん" | |
msgstr "" | |
msgid "若者" | |
msgstr "" | |
msgid "私" | |
msgstr "" | |
msgid "少女" | |
msgstr "" | |
msgid "少年" | |
msgstr "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
msgid "人間" | |
msgstr "" | |
msgid "人" | |
msgstr "" | |
msgid "男" | |
msgstr "" | |
msgid "男の人" | |
msgstr "" | |
msgid "男の子" | |
msgstr "" | |
msgid "女" | |
msgstr "" | |
msgid "女の人" | |
msgstr "" | |
msgid "女の子" | |
msgstr "" | |
msgid "赤ちゃん" | |
msgstr "" | |
msgid "若者" | |
msgstr "" | |
msgid "私" | |
msgstr "" | |
msgid "少女" | |
msgstr "" | |
msgid "少年" | |
msgstr "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const fs = require('fs'); | |
const content = fs.readFileSync('./a.txt', 'utf8'); | |
var lines = content.split('\n') | |
lines = lines.filter(function(item) { | |
return item.startsWith('msgid'); | |
}); | |
lines = lines.map(function(item) { | |
item = item.trim(); | |
item = item.substring(0, item.length - 1); | |
return item.replace('msgid "', ''); | |
}); | |
(async () => { | |
const browser = await puppeteer.launch({headless: false}); | |
const page = await browser.newPage(); | |
await page.goto('https://translate.google.com/#view=home&op=translate&sl=ja&tl=en'); | |
for(var i = 0; i < lines.length; i++){ | |
const ele = lines[i] | |
// const textArea = await page.$('#source'); | |
// const text = await page.evaluate(textArea => textArea.value, textArea); | |
// console.log(text) | |
//await page.click('body > div.container > div.frame > div.page.tlid-homepage.homepage.translate-text > div.homepage-content-wrap > div.tlid-source-target.main-header > div.source-target-row > div.tlid-input.input.has-transliteration > div.source-wrap > div > div > div.source-header > div > div > div'); | |
const textArea = await page.$('#source'); | |
await page.evaluate(textArea => textArea.value = '', textArea); | |
await page.focus('#source') | |
await page.keyboard.type(ele) | |
await page.waitFor(500); | |
const rightSelector = 'body > div.container > div.frame > div.page.tlid-homepage.homepage.translate-text > div.homepage-content-wrap > div.tlid-source-target.main-header > div.source-target-row > div.tlid-results-container.results-container > div.tlid-result.result-dict-wrapper > div.result.tlid-copy-target > div.text-wrap.tlid-copy-target > div > span.tlid-translation.translation > span' | |
await page.waitForSelector(rightSelector) | |
const rightArea = await page.$(rightSelector); | |
const rtext = await page.evaluate(rightArea => rightArea.textContent, rightArea); | |
fs.writeFileSync('./b.txt', 'msgid "' + ele + '"\n', {'flag':'a'}) | |
fs.writeFileSync('./b.txt', 'msgstr "' + rtext + '"\n\n', {'flag':'a'}) | |
console.log(ele) | |
console.log(rtext) | |
} | |
await browser.close(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "abc", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"puppeteer": "^5.2.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment