简体   繁体   中英

How do i call an Electron Browser Window multiple times?

I have an electron browser program that will allow the user to open multiple browser windows using a different proxy on each one. Im wondering how to call the newWindow() Function multiple times. In my current code it opens all the windows but only one loads up the page as shown in this picture here

Here is my code:

const url = require('url');
const async = require('async')

let win = null


function boot() {
  console.log(process.type)
  win = new BrowserWindow({
    width: 500,
    height: 500,
    resizable: false,
    frame: false
  })
  win.loadURL(`file://${__dirname}/index.html`)


}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function newWindow() {
  var proxyList = '196.19.218.117:9044:dailyf:proxies'
  var siteUrl = 'https://twitter.com/home'

  win2 = new BrowserWindow({
    width: 750,
    height: 500,
    resizable: false,
    parent: win,
  })
  win2.webContents.session.setProxy({proxyRules:`http=foopy,direct://${proxyList}`}, function () {
  win2.loadURL(`${siteUrl}`);
  },

  win2.on('closed', () => {
    win = null
  }),
)}

app.on('ready', boot)

function spawnChildren() {
  app.on('ready', _ => {
    console.log('yessirski')
    newWindow();
    sleep(4000)
    newWindow();
    sleep(4000)
    newWindow();
  }) 
}

spawnChildren();```

Make a local variable declaration for win2 :

var win2 = new BrowserWindow(){

Otherwise, win2 will be overwritten with the next call to newWindow() and only the last one will be usable. (With no var local declaration, your variable will be global)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM