如何连接到电子 webview 中的代理?

因为我可以通过免费代理服务器(或付费)连接,目前用作桌面应用程序的电子 JS 解决方案

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

代理列表服务器示例

http://proxylist.hidemyass.com/

推荐答案

你可以使用 .setProxy() 会话对象的方法.您可以直接指定代理,如下例所示:

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/');
});

或者你可以使用 PACscript:

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";
}

相关文章