2017-04-12 78 views
0

我正在開發一個Chrome擴展,它將授權標頭添加到請求中。Chrome onBeforeSendHeaders未捕獲FTP請求

爲此,我使用onBeforeSendHeaders,但遺憾的是它不工作FTP請求。我已經給許可清單中,並在後臺

清單權限說:

"permissions": [ 
    "webRequest", 
    "webRequestBlocking", 
    "webNavigation", 
    "tabs", 
    "cookies", 
    "ftp://*/*", 
    "*://*/*" 
    ] 

,我用onBeforeSendHeaders這樣的:

chrome.webRequest.onBeforeSendHeaders.addListener(
     function(details) { 
     if(details.url == "my_url"){  
       details.requestHeaders.push({'name':'Authorization','value':'my_value'}); 
        return { requestHeaders: details.requestHeaders }; 
     }   
     }, 
    {urls: ['<all_urls>','ftp://*/*']}, 
    [ 'blocking', 'requestHeaders'] 
    ); 
callback(true); 
} 

並且如果我無法打開FTP在Chrome中;有沒有其他的方法,我可以使用單個命令(例如ssh [email protected] -pw pass)在終端或膩子中打開FTP?

+1

在FTP中有沒有標題。所以'onBeforeSendHeaders'沒有被觸發是有道理的。出於同樣的原因,嘗試向「FTP請求」添加「授權標題」確實很有意義。 –

+0

ohh。非常感謝@Martin ...你可以建議我一個解決方案來打開FTP服務器..應該怎麼做呢..有什麼稱爲證書..因爲當我們點擊打開FTP服務器..它要求憑據 – user5821368

+0

請閱讀[在什麼情況下,我可以添加「緊急」或其他類似的短語,以獲得更快的答案?](https://meta.stackoverflow.com/q/326569) - 總結是,這不是這是解決志願者問題的理想方式,可能會對獲得答案產生反作用。請不要將這添加到您的問題。 – halfer

回答

1

onAuthRequired是解決它,爲我工作:

var target = "ftp://ftpurl/"; 

var myCredentials = { 
    username: "username", 
    password: "pass" 
}; 

var pendingRequests = []; 

// A request has completed. 
// We can stop worrying about it. 
function completed(requestDetails) { 
    console.log("completed: " + requestDetails.requestId); 
    var index = pendingRequests.indexOf(requestDetails.requestId); 
    if (index > -1) { 
    pendingRequests.splice(index, 1); 
    } 
} 

function provideCredentialsSync(requestDetails) { 

    // If we have seen this request before, then 
    // assume our credentials were bad, and give up. 
    if (pendingRequests.indexOf(requestDetails.requestId) != -1) { 
    console.log("bad credentials for: " + requestDetails.requestId); 
    return {cancel:true}; 
    } 
    pendingRequests.push(requestDetails.requestId); 
    console.log("providing credentials for: " + requestDetails.requestId); 
    return {authCredentials: myCredentials}; 
} 

chrome.webRequest.onAuthRequired.addListener(
    provideCredentialsSync, 
    {urls: [target]}, 
    ["blocking"] 
); 

chrome.webRequest.onCompleted.addListener(
    completed, 
    {urls: [target]} 
); 

chrome.webRequest.onErrorOccurred.addListener(
    completed, 
    {urls: [target]} 
);    

}