How to read proxies from file 1 by 1 and then repeat them in same natural order in JS?

66 Views Asked by At

I have this worker.js script where proxies are read from index.js and using Math.floor to randomize the order of the proxies and then use puppeteer to visit a URL. in index.js:

const proxies = fs.readFileSync('./proxies.txt')

and in worker.js file:

const index = Math.floor(Math.random() * workerData.proxies.length);
const browser = await puppeteer.launch({
                    args: ['--proxy-server=' + workerData.proxies[index]],
                    headless: "new",
                    executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome'

I don't wish to randomize, I just wish it to execute proxy 1 by 1 and then after last one, repeat from first one in sequence.

1

There are 1 best solutions below

0
pjpscriv On

It looks like making a copy of workerData.proxies and then removing items 1 by 1 from that list using .slice() might do the job.

var proxiesCopy = workerData.proxies.slice();

while (proxiesCopy.length > 0) {
    const index = Math.floor(Math.random() * proxiesCopy.length);
    const browser = await puppeteer.launch({
        args: ['--proxy-server=' + workerData.proxies[index]],
        headless: "new",
        executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome'
    })
    // Possibly more puppeteer stuff etc...

    proxiesCopy.splice(index, 1)
}