• 允许CEF跨域访问iframe


    默认情况下,如果嵌入本地Web页面,并在页面内部使用iframe来显示一个在线页面,加载的过程中会触发一个未捕获异常,虚函数CefV8ContextHandler::OnUncaughtException被调用,如果搞个控制台窗口来输出异常消息,或者将消息打印到日志中,会是这样:

    Unsafe JavaScript attempt to access frame with URL file:///F:/Workspace/Project/WebUI_V1.0.0/WebUI/main.html from frame with URL http://112.4.xx.xx/js_app_choice/index/fac/1/uid/307839. The frame requesting access has a protocol of 'http', the frame being accessed has a protocol of 'file'. Protocols must match.

    虽然此时,在线页面嵌入成功并显示出来了,但是,无法happy的实现父子页面的跨域访问,父页面想要调子页面的JavaScript方法,没门,子页面调父页面,没门,限制重重。根据异常消息,子页面访问的父页面的URL必须为http协议,而非file协议。正常情况下,只要把父页面传到服务器上就可以解决协议问题。但是,我们是用CEF来做桌面程序界面,哪来的服务器,别猴急,看看Chrome浏览器上是怎么玩的:

    给chrome启动加个参数 --disable-web-security

    方法1

    • 在命令行运行 "C:Program FilesGoogleChromeApplicationchrome.exe" --disable-web-security

    方法2

    • 在桌面复制一个chrome的快捷方式,重命名为“Chrome跨域”,右键“属性”,在“目标”后面加上 --disable-web-security,即目标改为上面的"C:Program FilesGoogleChromeApplicationchrome.exe" --disable-web-security,以后点击上面的“Chrome跨域”启动浏览器,就可以跨域访问了

    注意:启动chrome的时候一定要关闭开着的所有chrome程序,然后再启动才能生效

    上文来源:http://blog.bccn.net/%E9%9D%99%E5%A4%9C%E6%80%9D/13411

    既然我们用CEF,那么跟Chrome也是一般无二了,只要找到类似的设置就可以。找呀找,在CefBrowserSettings也就是_cef_browser_settings_t结构体中,有个成员变量web_security_disabled,正是此货,来看下它的注释:

    ///
    // Set to true (1) to allow risky security behavior such as cross-site
    // scripting (XSS). Use with extreme care.
    ///

    确实,将其赋值为true,即可运行跨域访问。

    重新启动程序,显示iframe中的在线页面,为输出原来的异常消息。

    再在代码里跨域访问看看:

    var $iframe = $("#iframe-apps");
    $iframe.get(0).contentWindow.showAppList(param);

    嘿嘿,果然可用。

    下面是一些帖子,供各位参考下载JavaScript中实现iframe跨域访问的方法:

    《获取iframe窗口的 contentWindow 不兼容Chrome ?》

    http://bbs.csdn.net/topics/360064743

    《iframe子父页面调用》

    http://blog.csdn.net/w_basketboy24/article/details/8889085

    《jquery 父窗口 子窗口 相关操作》

    http://www.cnblogs.com/zhouchaoyi/archive/2010/09/16/1827973.html

  • 相关阅读:
    RocketMQ之二:分布式开放消息系统RocketMQ的原理与实践(消息的顺序问题、重复问题、可靠消息/事务消息)
    Redis 发布/订阅机制原理分析
    Guava 12-数学运算
    Guava] 11
    Guava 10-散列
    Guava 9-I/O
    Guava 8-区间
    mat(Eclipse Memory Analyzer tool)之二--heap dump分析
    Python流程控制语句
    监控和管理Cassandra
  • 原文地址:https://www.cnblogs.com/wxxweb/p/3738696.html
Copyright © 2020-2023  润新知