如何在CefPython中启用外部文件链接?

时间:2019-06-01 14:28:39

标签: python chromium-embedded cefpython

我正在开发一个CEFPython应用程序,该应用程序要求我包括一些外部文件,例如JS或CSS库。然而,HTML文件中提到的任何外部路径似乎都是不可接受的,但我肯定缺少启用外部文件喜欢的标志但无法弄清楚哪个。下面是我主要功能的代码:

def main():
sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
# To change user agent use either "product_version"
# or "user_agent" options. Explained in Tutorial in
# "Change user agent string" section.
settings = {
    # "web_security_disabled": True,
    # "user_agent": "MyAgent/20.00 MyProduct/10.00",

}
cef.Initialize(settings=settings)
browser = cef.CreateBrowserSync(url=html_to_data_uri(HTML_code),
                                window_title="Demo Program")
set_javascript_bindings(browser)
cef.MessageLoop()
cef.Shutdown()

2 个答案:

答案 0 :(得分:1)

“web_security_disabled”设置必须在浏览器创建中传递,而不是在 cef 初始化中传递。

示例:

settings = {
    "web_security_disabled": True,
}


def launcher(url):
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    cef.Initialize()
    cef.CreateBrowserSync(url=url, window_title="my title", settings=settings)
    cef.MessageLoop()
    cef.Shutdown()

答案 1 :(得分:0)

您不能混合使用不同来源的脚本,请参阅: https://en.wikipedia.org/wiki/Same-origin_policy

您可以尝试使用--disable-web-security开关。

如果这不起作用,则使用“ http” URL代替数据uri。您可以使用Python运行内部Web服务器。您也可以实现ResourceHandler并提供http内容,而无需运行Web服务器,但是正确实现ResourceHandler是一项复杂的任务。