跨原产地问题与iFrame chrome扩展

Cross-Origin issue with iFrame for chrome extension

本文关键字:chrome 扩展 iFrame 原产地 问题      更新时间:2023-09-26

我正试图在Google Hangouts中编写一个自动拨号的chrome扩展,这样我就不必为每次会议输入我的会议桥号码。如果他们也能得救就好了……无论如何,我有一个概念,但我正试图解决一个跨源问题。

当您在Google Hangouts中拨打电话时,您可以从联系人页面开始输入您要拨打的号码。当你按回车键或点击"拨打电话"时,拨号器就会被加载到iFrame中。在我的chrome扩展,我能够得到一个参考的iFrame,但框架是在域"plus.google.com",我的脚本是从"hangouts.google.com"访问。

我知道它们都在google.com父域下,所以我试图让我的chrome扩展访问框架,以便在框架的contentWindow内的dialer按钮上执行。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

您可以使用iframe.contentWindow.postMessage在内容脚本之间进行通信