如何从 Accounts.txt 获得超过 1 组 User:Pass

How to get more than 1 set of User:Pass from Accounts.txt

我有一个 username:password 的列表,我想将它们分开,我该怎么做:

let username = usernameValue;
let password = passwordValue;

所以我可以有一个大列表,比如 10+ username/password,我的脚本可以自动从 txt 文件中提取,然后登录到每个个人帐户。

const fs = require('fs')
const path = require('path')
const accounts = fs.readFileSync(path.resolve('./../../settings/accounts.txt'), 'utf8')
const [usernameValue, passwordValue] = accounts.split(':');

console.log(usernameValue);
// 'username'

console.log(passwordValue);
// 'password'

以上部分代码摘自我原来的question on here

accounts.txt

test:test
test2:test2
test3:test3

输出

test - username
test - password
test2 - username

它不会输出每个值,我也不知道为什么。

如果是逐行你可以使用包 readline。例如:

const readline = require("readline");
const fs = require("fs");

const rl = readline.createInterface({
    input: fs.createReadStream(__dirname + "/accounts.txt"),
});

rl.on("line", (line) => {
    const [usernameValue, passwordValue] = line.split(':');
    console.log(`${usernameValue} - ${passwordValue}`);
});

如果你想要线路输出之间的延迟,你可以设置一个间隔并处理存储在缓冲区中的信息:

const readline = require("readline");
const fs = require("fs");

const rl = readline.createInterface({
    input: fs.createReadStream(__dirname + "/accounts.txt"),
});

const buffer = [];
let isDonde = false;

rl.on("line", (line) => {
    buffer.push(line.split(':'));
});

rl.on("close", () => {
    isDonde = true;
})

let interval = setInterval(() => {
    if (!buffer.length) {
        if (isDonde) {
            clearInterval(interval);
        }
        return;
    }

    const [usernameValue, passwordValue] = buffer.shift();
    console.log(`${usernameValue} - ${passwordValue}`);
}, 1000); // higher is more time between logs