2012-04-16 210 views
1

我做了一个非常简单的Chrome扩展,什么重定向页面上的http协议,https协议,如果存在。我在调试,我发现Facebook,什么都有http和https。访问 - 控制 - 允许来源不允许访问 - >来源*

的代码是在这里:

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调用:

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

的问题是,我在控制台收到以下错误信息:

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

我没有任何想法可能是什么问题:(

+0

好像你的扩展正在发出Ajax请求... – 2012-04-16 12:02:06

+0

sry,我忘了复制ajax调用,我更新了我的帖子:/ – 2012-04-16 12:05:18

+0

但是,这个Ajax调用是问题...你不能这样做,这就是错误信息所说的。此外,您不能从Ajax回调中返回值,整个设置将无法工作。您必须找到其他方式来测试HTTPS版本是否可用。 – 2012-04-16 12:07:45

回答

1

代码有对小情侣问题:

  • 你的清单文件只请求权限http://*/*https://*/*,所以你的要求到HTTPS站点失败。您需要获得*://*/*的许可,因此您可以通过所有协议获取所有域上的所有页面,而不仅仅通过HTTP。

  • 第二个问题是,你期望你的$.ajax调用返回一个布尔值,但这不会发生。 $.ajax调用有两个回调,每个回调都返回一个布尔值,但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调用解决之前该做什么,因此我们让Ajax successerror函数使用布尔参数触发回调。该回调函数然后根据布尔值进行操作。

+0

伟大的答案,谢谢;)作品好^^ – 2012-04-17 20:24:35

0

如果您打包的扩展,你被允许做跨域请求,但如果你将其创建为托管应用/扩展程序,请参见:

Chrome extension Cross Domain Request

+0

谢谢,greate arcticle,我需要阅读它immadietely – 2012-04-17 20:25:10

相关问题