How connect to proxy in electron webview?

别说谁变了你拦得住时间么 提交于 2019-12-03 06:23:19

问题


as I can connect through a to a free proxy server (or pay), currently in use as electron JS solution as desktop application

example proxy list servers

http://proxylist.hidemyass.com/


回答1:


You can use .setProxy() method of session object. You're able to specify proxy directly like in example below:

// in main.js
var electron      = require('electron');
var BrowserWindow = electron.BrowserWindow;
mainWindow = new BrowserWindow({
    "width": 970,
    "height": 500,
    "center": true,
    'title': 'Main window',
});
mainWindow.webContents.session.setProxy({proxyRules:"socks5://114.215.193.156:1080"}, function () {
    mainWindow.loadURL('https://whatismyipaddress.com/');
});

Or you can use PACscript:

// in main.js
mainWindow.webContents.session.setProxy({pacScript:"file://"+root+"/js/pacfile.js"}, function () {
    mainWindow.loadURL('https://whatismyipaddress.com/');
});


// pacfile.js example
var blocked      = ["site1.com", "site2.com", "site3.com"];
var proxyServer  = "SOCKS5 114.215.193.156:1080";
function FindProxyForURL(url, host) {
    var shost = host.split(".").reverse();
    shost = shost[1] + "." + shost[0];
    for(var i = 0; i < blocked.length; i++) {
        if( shost == blocked[i] ) return proxyServer;
    }
    return "DIRECT";
}


来源:https://stackoverflow.com/questions/37393248/how-connect-to-proxy-in-electron-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!