最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing proxy in FindProxyForURL on request - Stack Overflow

programmeradmin3浏览0评论

We have a web scraper project that uses different proxies for each of the websites. Currently we're using this code to return same proxy for each of the websites, but now we need to specify a separate proxy URL for each one:

var config = {
    mode: "pac_script",
    pacScript: {
        data: "function FindProxyForURL(url, host) {\n" +
            "    let domainList = ['site1', 'site2',  'site3'];\n" +
            "      if (host === 'www.google' || host === 'analytics.google')" +
            "           return 'PROXY pr.oxylabs.io:7777';" +
            "    for (let i = 0; i < domainList.length; i++) {\n" +
            "\n" +
            "        if (dnsDomainIs(host, domainList[i])) {\n" +
            "            return 'PROXY pr.oxylabs.io:7777';\n" +
            "        }\n" +
            "    }\n" +
            "    if (dnsDomainIs(host, 'analytics.google') || dnsDomainIs(host, 'doubleclick')) {" +
            "       return 'PROXY 127.0.0.1:65500';\n" +
            "}\n" +
            "   return 'DIRECT';\n" +
            "}"
    }
};

This config is being loaded into browser' settings via:

chrome.proxy.settings.set({value: config, scope: "regular"}, function () {});

and credentials were provided via:

function callbackFn(details) {
    let username = "custom-username-" + makeid(6);
    return {
        authCredentials: {
            username: username,
            password: "our_password"
        }
    };
}

and then we kept track of how many requests were made to each domain via:

chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
        if (details.method === "GET" && details.type === "main_frame") {
        }
    },
    {urls: ["<all_urls>"]},
);

The question is - how can we assign a different proxy to each domain when, for example, we pass in a magic string in the URL (for example, ___CHANGE___PROXY___FOR___DOMAIN___ZZZ), or something similar to indicate which domain needs a proxy change?

My idea was to create a function that returns contents for FindProxyForURL that generates javascript for it, then put it into config.pacScript.data and call chrome.proxy.settings.set({value: config, scope: "regular"}, function () { }) ...

发布评论

评论列表(0)

  1. 暂无评论