首页 > 解决方案 > 如何在 CefPython 中启用外部文件链接?

问题描述

我正在开发一个 CEFPython 应用程序,该应用程序需要我包含一些外部文件,例如 JS 或 CSS 库。但是 HTML 文件中提到的任何外部路径(指同一文件夹中存在的外部库和在线资源 URL)似乎是不可接受,我确定缺少一个启用外部文件喜欢的标志,但无法弄清楚是哪个。下面是我的主要功能的代码:

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()

标签: pythonchromium-embeddedcefpython

解决方案


“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()

推荐阅读