1

Im making a really simple Chrome extension, what redirects pages what are on http protocol ,to https protocol, if exist. Im on debugging, and i found facebook, what has both http, and https.

The code is here:

function redirect() {    
    chrome.tabs.query({active: true}, function(tabArray) {  
        var currentURL = tabArray[0].url;               //http://facebook.com
        var httpsURL = generateSSL(currentURL);         //https://facebook.com
        if(httpsURL == currentURL){
            console.log(currentURL+" is already on HTTPS");
            chrome.browserAction.setIcon({path:"../images/padlock_green.png"});
        } else if(checkSSL(httpsURL)){                      
            chrome.tabs.update(tabArray[0].id, {url: httpsURL});
            chrome.browserAction.setIcon({path:"../images/padlock_green.png"});
            chrome.browserAction.setBadgeText({text:"SSL"});
            console.log("SSL found,"+currentURL+" redirected to"+httpsURL);
        } else {
            //donothing
            console.log(currentURL+" has no SSL");
            chrome.browserAction.setIcon({path:"../images/padlock_red.png"});
        }
    });
}

ajax call:

function checkSSL(url){
    $.support.ajax = true;
    $.ajax({
        url: url,
        type:'HEAD',
        error: function()
        {
            return false;
        },
        success: function()
        {
            return true;
        }
    });
}

The problem is, that i get in console the following error msg:

XMLHttpRequest cannot load https://www.facebook.com/. Origin chrome-extension://pgidanbjmliilmmohlphbagcapafjjpg is not allowed by Access-Control-Allow-Origin.

I dont have any ideas what could be the problem :(

4

2 に答える 2

1

あなたのコードにはいくつかの小さな問題があります:

  • あなたのマニフェスト ファイルはhttp://*/*ではなく https://*/*に対する権限のみを要求しているため、HTTPS サイトへの要求は失敗しています。HTTP だけでなく、すべてのプロトコル*://*/*ですべてのドメインのすべてのページを取得できるようにするには、アクセス許可が必要です。

  • 2 つ目の問題は、呼び出しでブール値が返されることを期待して$.ajaxいるが、そうではないことです。$.ajax呼び出しには 2 つのコールバックがあり、それぞれブール値を返しますcheckSSLが、Ajax 呼び出しが完了する前に終了します。つまり、checkSSL常に が返されますundefined

代わりにやりたいことはcheckSSL、引数としてコールバック関数を提供することです:

function checkSSL(url, callback){
    $.ajax({
        url: url,
        type:'HEAD',
        error: function() { callback(false); },
        success: function() { callback(true); }
    });
}

次に、そのコールバックを使用して、への呼び出しの後にコードの呼び出しを実行しますcheckSSL

function redirect() {    
    chrome.tabs.query({active: true}, function(tabArray) {  
        var currentURL = tabArray[0].url;               //http://facebook.com
        var httpsURL = generateSSL(currentURL);         //https://facebook.com
        if(httpsURL == currentURL){
            console.log(currentURL+" is already on HTTPS");
            chrome.browserAction.setIcon({path:"../images/padlock_green.png"});
        } else {
            // call checkSSL and take action in an anonymous callback function!
            checkSSL(httpsURL, function(urlDoesExist) { 
                if(urlDoesExist) {                 
                    chrome.tabs.update(tabArray[0].id, {url: httpsURL});
                    chrome.browserAction.setIcon({path:"../images/padlock_green.png"});
                    chrome.browserAction.setBadgeText({text:"SSL"});
                    console.log("SSL found,"+currentURL+" redirected to"+httpsURL);
                } else {
                    //donothing
                    console.log(currentURL+" has no SSL");
                    chrome.browserAction.setIcon({path:"../images/padlock_red.png"});
                }
            });
        }
    });
}

最初の の下のコードの変更に注目してくださいelse。Ajax 呼び出しが解決されるまで何をすべきかを決定できないため、Ajaxsuccesserror関数がブール値の引数でコールバックを起動するようにします。そのコールバック関数は、ブール値に基づいてアクションを実行します。

于 2012-04-17T14:17:24.547 に答える
0

拡張機能をパッケージ化する場合、クロスドメインリクエストを実行できますが、ホスト型アプリケーション/拡張機能として作成する場合は許可されません。以下を参照してください。

Chrome拡張機能のクロスドメインリクエスト

于 2012-04-16T12:24:33.093 に答える