2022-07-17

const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const xlsxrd = require("node-xlsx");
const fs = require("fs");

function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
/**

  • 输入 #baidu_translate_input
  • 输出 .output-wrap
  • 翻译按钮 #translate-button
    */

function translate(message) {
return new Promise(async (resolve) => {
const browser = await puppeteer.launch({
headless: true,
args: [
"--disable-extensions",
"--hide-scrollbars",
"--disable-bundled-ppapi-flash",
"--mute-audio",
"--disable-setuid-sandbox",
"--disable-gpu",
"--no-sandbox",
"--disable-web-security",
"--disable-infobars",
],
ignoreDefaultArgs: ["--enable-automation"],
devtools: false,
dumpio: false,
});
const page = await browser.newPage();
await page.evaluateOnNewDocument(() => {
Object.defineProperties(navigator, { webdriver: { get: () => false } });
});
await page.goto("https://fanyi.baidu.com/?aldtype=16047#auto/zh");
const closeBtn = await page.("#baidu_translate_input");
const search_btn = await page. = cheerio.load(html);
await browser.close();
const text = $(".output-wrap .target-output").text();
resolve(text);
});
}

// translate().then((res) => {
// console.log(res);
// });
const list = xlsxrd.parse(${process.cwd()}/template.xlsx);
const { data, name } = list[0];
const l = data.slice(1);
const s = l.map((v) => v[1] + "#").join("\n");
console.log(l.length)
translate(s).then((r) => {
console.log(
r
.split("#")
.map((v) => v.trim())
.filter((v) => v).length
);
});

你可能感兴趣的:(2022-07-17)