【问题标题】:cross domain request in injected script from chrome extension来自 chrome 扩展的注入脚本中的跨域请求
【发布时间】:2013-02-12 18:05:09
【问题描述】:

我正在编写一个 chrome 扩展程序,它在打开的选项卡中注入一个 iframe 并在其中加载一个 url。要加载的 url 与选项卡中打开的页面不在同一个域中。我正在使用以下代码:

--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);

这仅适用于当前页面为http://google.co.in 而不在其他页面上。所以我遇到了跨域问题。但据我所知,扩展可以发出跨域请求,那么该怎么做呢?请帮助。

【问题讨论】:

    标签: google-chrome google-chrome-extension


    【解决方案1】:

    Google 使用X-Frame-Options 标头,许多网站都将它们用作最佳实践

    X-Frame-Options 有三个可能的值:

    • 拒绝 该页面无法在框架中显示,无论网站是否尝试这样做。
    • 同源 页面只能显示在与页面本身同源的框架中。
    • ALLOW-FROM uri 页面只能显示在指定原点的框架中。

    Google 使用 SAMEORIGIN 值,因此仅当当前页面为 http://google.co.in 时才有效。

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

    【讨论】:

    • 谢谢,它与维基百科合作。从不怀疑 google.co.in 是罪魁祸首。
    猜你喜欢
    • 2014-03-04
    • 2012-04-22
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多