2013-02-12 181 views
1

我正在编写一个chrome扩展,它在open选项卡中注入iframe并在其中加载url。要加载的url与打开的页面不在同一个域中标签is.I正在使用下面的代码:来自Chrome扩展的注入脚本中的跨域请求

--menifest.json-- 

"background" : { 
    "scripts": ["background.js"] 
    }, 
    "permissions": [ 
    "tabs", "http://*/", "https://*/" 
    ] 

--background.js-- 

chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(null, 
          {file:"logic.js"}); 
}); 

--logic.js-- 

var newdiv = document.createElement('div'); 
var iframe = document.createElement('iframe'); 
iframe.setAttribute('src','http://google.co.in'); 
newdiv.appendChild(iframe); 
document.body.appendChild(newdiv); 

这只有当CURENT页http://google.co.in而不是其他pages.So我打问题。但据我所知跨域,扩展可以使跨域请求,那么怎么做呢?请帮忙。

回答

2

谷歌使用X-Frame-Options头,许多网站使用它们作为最佳实践

有对X框,选择三个可能的值:

  • DENY 该页面无法显示在一个框架中,不管网站试图这样做。
  • SAMEORIGIN 该页面只能以与页面本身相同的原点显示在框架中。
  • ALLOW-FROM uri 该页面只能显示在指定原点的框架中。

谷歌使用SAMEORIGIN值,所以这只适用于当页面为http://google.co.in

因此,您没有遇到跨域问题,并且是扩展可以提出跨域请求。

+0

谢谢,它与维基百科合作。永远不会怀疑google.co.in是罪魁祸首。 – vishesh 2013-02-12 10:23:11