首页 > 解决方案 > 突然 Firefox 无法再从同一个站点加载自定义字体并出现 CORS 错误

问题描述

我在 python 脚本中使用 Selenium Webdriver 从我的网页下载网站并将它们转换为 PNG:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.headless = False
SITE = "http://localhost/something_I_want_to_convert_with_hi-resolution.html"
DPI = 2.5
profile = webdriver.FirefoxProfile()
profile.set_preference("layout.css.devPixelsPerPx", str(DPI))
driver = webdriver.Firefox(options=options, firefox_profile=profile)

driver.get(SITE)
...

直到上个月我还在使用 Ubuntu 20.04 时,这一切都很好。它用这样的正确字体创建了图像。

我猜正是由于 Ubuntu 升级 Firefox 无法加载自定义字体,我已经通过 css 在我的网站中包含了,现在它加载了默认字体。

什么可以改变这种行为?Ubuntu 20.10 中的最新更新是否改变了 Firefox 的行为?

我在控制台中收到此错误,这很奇怪:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/css/fonts/kramer__.TTF. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

很奇怪,因为字体在同一个域(localhost)中,所以不应该出现 CORS 错误。

同一个站点在 Chrome 中运行良好并加载字体。同样在 Waterfox-classic 中,它可以很好地加载字体。

我想添加如下内容:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");

但我在本地使用ZeroNet为站点提供服务,它是 html-、CSS- 和 JS-only,所以我认为,我无法更改发送的标头。

我在 Firefox 中试过about:config

但这些都没有任何效果。

如果我安装access-control-allow-origin 插件并启用Access-Control-Allow-Origin: *它也可以正常工作,(我在下面添加了它作为解决方法。)

如何设置 Firefox 忽略跨域请求?

标签: selenium-webdriverpython-3.8

解决方案


在您遇到问题之后,可能是您的容器/zeronet 更新或 firefox 更新

对于 localhost 的 chrome 中的临时解决方法,您可以添加 — disable-web-security while started chrome

在 Firefox 中,您可以执行以下操作:

about:config -> security.fileuri.strict_origin_policy -> false


推荐阅读