【问题标题】:Chrome Extension: Unsafe JavaScript attempt to access frame with URL Domains, protocols and ports must matchChrome 扩展:不安全的 JavaScript 尝试使用 URL 访问框架 域、协议和端口必须匹配
【发布时间】:2012-07-19 03:53:15
【问题描述】:

此答案指定解释如何访问 gmail.com https://stackoverflow.com/a/9439525/222236 上所有 iframe 的内容

但是在 mail.google.com 上它会抛出这个错误:

Unsafe JavaScript attempt to access frame with URL https://plus.google.com/u/0/_/... from frame with URL https://mail.google.com/mail/u/0/#inbox. Domains, protocols and ports must match.

我尝试将*://plus.google.com/* 添加到扩展程序清单的匹配项中,但没有帮助。

更新:在访问内容之前检查 url 有效,但目前我的逻辑非常粗略,因为它只检查 google plus:

        if(-1==iframes[i].src.indexOf('plus.google.com')) {
            contentDocument = iframes[i].contentDocument;
            if (contentDocument && !contentDocument.rweventsadded73212312) {
                // add poller to the new iframe
                checkForNewIframe(iframes[i].contentDocument);
            }
        }

【问题讨论】:

  • 我相信,在许多情况下,可能需要遍历所有子帧,并且从控制流中排除外部域会破坏应用程序的主要功能。因此,在 chrome 扩展的情况下,我建议用清单中的 content_scripts "all_frames": true 声明替换通过帧的循环,以及合并结果的可选逻辑(如有必要)。

标签: javascript google-chrome-extension gmail


【解决方案1】:

由于same origin policy,访问被阻止。
避免错误的正确方法是排除来自不同来源的帧。你的逻辑确实很粗糙。它不专门查看主机名,也不考虑其他域。
反转逻辑以获得稳健的解决方案:

if (iframes[i].src.indexOf(location.protocol + '//' + location.host) == 0 ||
    iframes[i].src.indexOf('about:blank') == 0 || iframes[i].src == '') {

这个白名单的解释:

  • protocol://host/ = https://mail.google.com.
    显然,必须允许当前主机
  • about:blank 和一个空字符串
    这些框架由 GMail 动态创建和编写脚本。

【讨论】:

    【解决方案2】:

    mail.google.complus.google.com 不是同一个域。现代网络浏览器中的 JavaScript 实现不允许跨域脚本。

    在不诉诸各种骇客的情况下,解决此问题的正确方法是通过 CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing),在这种情况下您无法使用它。

    【讨论】:

      猜你喜欢
      • 2012-05-21
      • 2013-01-28
      • 1970-01-01
      • 2012-09-28
      • 2013-05-28
      • 2021-06-09
      • 2018-09-10
      • 2016-11-15
      • 2020-07-21
      相关资源
      最近更新 更多