我正在尝试为 Google Hangouts 中的自动拨号编写一个 chrome 扩展程序,这样我就不必为每次 session 都输入我的 session 桥号码。也能保存它们就好了……无论如何,我的概念有点像工作,但我正在努力解决跨域问题。
当您在 Google 环聊中调用电话时,您会从联系人页面开始并输入您要调用的号码。当您按下回车键或单击“调用电话”时,拨号程序将加载到 iFrame 中。在我的 chrome 扩展程序中,我可以获得对 iFrame 的引用,但是该框架位于“plus.google.com”域下,而我的脚本是从“hangouts.google.com”访问的。
我知道它们都在 google.com 父域下,所以我试图允许我的 chrome 扩展访问框架,以便在框架的 contentWindow 中的拨号按钮上执行 .click()。
在 chrome 扩展程序的内容脚本中,我从父页面中选择 iframe 元素并设置为名为 iframe 的变量。
var iframe = $("div iframe")[0];
我可以设置
document.domain = "google.com";
没有问题,但是当我尝试做的时候
iframe.contentWindow.document.domain = "google.com";
我明白了
content.js:3 Uncaught SecurityError: Blocked a frame with origin
"https://hangouts.google.com" from accessing a frame with origin
"https://plus.google.com". The frame requesting access set "document.domain" to
"google.com", but the frame being accessed did not. Both must set
"document.domain" to the same value to allow access.
我试过放宽扩展中的内容安全策略,但也许我做的不对:
"content_security_policy": "script-src 'self' https://google.com; object-src 'self'",
"permissions": [
"http://*/",
"tabs"
]
有什么办法可以解决这个问题吗?
最佳答案
在这种情况下,添加权限或放宽 CSP 都无济于事。
您需要 iframe 中的内容脚本的第二个实例来跨域操作其文档。
确保使用 "all_frames": true
注入(inject)您的内容脚本。
您可以使用 iframe.contentWindow.postMessage
在内容脚本之间进行通信.
关于javascript - 用于 chrome 扩展的 iFrame 的跨域问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048613/