使用Chrome 45.0.2454.101中的扩展程序访问跨域iframe的文档

时间:2015-09-28 03:20:21

标签: google-chrome-extension cross-domain

我使用Chrome扩展程序中的内容脚本来访问跨域iframe中的文档,使用以下代码:

document.querySelector('iframe').contentWindow.document

此操作正常,直到我升级到最新版本的Google Chrome(45.0.2454.101 64位),在访问iframe时报告以下安全性错误:

Uncaught SecurityError: Blocked a frame with origin
"http://www.miercn.com" from accessing a frame with origin
"http://pos.baidu.com". Protocols, domains, and ports must match.

此版本的Chrome有何变化以及如何解决此问题?

我的Chrome版本:

  

Google Chrome 45.0.2454.101(正式版本)(64位)   修订版本3b3c00f2d95c45cca18ab944acced413fb759311-refs / branch-heads / 2454 @ {#502}   操作系统Mac OS X Blink 537.36   (@ 3b3c00f2d95c45cca18ab944acced413fb759311)JavaScript V8 4.5.103.35   Flash 19.0.0.185用户代理Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_4)   AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 45.0.2454.101   Safari / 537.36命令行/ Applications / Google   Chrome.app/Contents/MacOS/Google Chrome --enable-avfoundation   --enable-avfoundation --flag-switches-begin --flag-switches-end

1 个答案:

答案 0 :(得分:2)

您必须通过cross-domain messaging使用postMessage

为了让主页上的内容脚本与注入iframe的内容脚本进行通信,应该在all frames中注入内容脚本:

"content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["content.js"],
    "all_frames": true
}],