首页 > 解决方案 > 设置 chrome 代理设置表单扩展

问题描述

我为代理服务器开发了一个 chrome 扩展。在将 IP 和 PORT 设置为 chrome 设置后,我通过 API 获取服务器(及其授权数据)列表:

connect: function(proxyData, callback) {

    this.setListener(proxyData);
    chrome.proxy.settings.set(
        {
            value: {
                mode: "fixed_servers",
                rules: {
                    singleProxy: {
                        scheme: "http",
                        host: proxyData.ip,
                        port: parseInt(proxyData.port)
                    },
                }
            },
            scope: 'regular'
        },
        function() {
            if (typeof callback === 'function') {
                callback(proxyData);
            }
        }
    );
}

之后我添加了自动化监听器:

setListener: function(proxyData) {
    let username = proxyData.username,
        password = proxyData.password;
    this.proxyAuthFunction = function(details, callbackFn) {
        console.log(username, password, 'Init proxy auth function');
        callbackFn({authCredentials: { username: username, password: password}});
    };
    if (chrome.webRequest.onAuthRequired.hasListener(this.proxyAuthFunction)) {
        chrome.webRequest.onAuthRequired.removeListener(this.proxyAuthFunction);
    }
    chrome.webRequest.onAuthRequired.addListener(
        this.proxyAuthFunction,
        {urls: ["<all_urls>"]},
        ["asyncBlocking"]
    );
}

它不起作用,浏览器无论如何都会要求基本授权。

但是硬编码效果很好,浏览器不要求基本授权:

setListener: function(proxyData) {
    let username = 'qwerty',
        password = 'qwerty';
    this.proxyAuthFunction = function(details, callbackFn) {
        console.log(username, password, 'Init proxy auth function');
        callbackFn({authCredentials: { username: username, password: password}});
    };
    if (chrome.webRequest.onAuthRequired.hasListener(this.proxyAuthFunction)) {
        chrome.webRequest.onAuthRequired.removeListener(this.proxyAuthFunction);
    }
    chrome.webRequest.onAuthRequired.addListener(
        this.proxyAuthFunction,
        {urls: ["<all_urls>"]},
        ["asyncBlocking"]
    );
}

有没有人遇到过类似的问题?

标签: javascriptgoogle-chrome-extension

解决方案


推荐阅读