selenium-webdriver - 突然 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
:
- 禁用
content.cors.disable
(空字符串) security.fileuri.strict_origin_policy
错误的privacy.file_unique_origin
错误的
但这些都没有任何效果。
如果我安装access-control-allow-origin 插件并启用Access-Control-Allow-Origin: *
它也可以正常工作,(我在下面添加了它作为解决方法。)
如何设置 Firefox 忽略跨域请求?
解决方案
在您遇到问题之后,可能是您的容器/zeronet 更新或 firefox 更新
对于 localhost 的 chrome 中的临时解决方法,您可以添加 — disable-web-security while started chrome
在 Firefox 中,您可以执行以下操作:
about:config -> security.fileuri.strict_origin_policy -> false
推荐阅读
- angular - 包装垫选择选项以适应控件的宽度
- android - 在包含 Google AdMob 依赖项后发现“在模块 classes.jar 中找到重复的类 com.google.android.gms.common.api.internal.zza”
- php - 无法使用 php 连接到 mssql 服务器
- c# - SqlClient.SqlException:从 Fluent API 获取数据时列名 ClassNameId 无效
- python - 如何按名称应用 python 函数?
- python - 如何正确循环和构建 pyspark 数据框
- postgresql - 如果 order by 应用于多个列,则 order by 不在计算列上工作
- elasticsearch - 在 Elasticsearch 的生产部署中将现场数据变为 true 是一种好习惯吗?
- javascript - 为什么 SQL 语句将“缺失”列的值更新为 NULL?
- haskell - 在 Haskell 中创建一个具有 Int 或它们的数组的代数数据类型