1

I use Selenium Webdriver in a python script to download sites from my webpage and convert them into PNGs:

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

This worked fine until last month when I still used Ubuntu 20.04. It created the images with the right font like this.

I guess exactly since the Ubuntu upgrade Firefox cannot load the custom fonts, I have included via css in my site, anymore and it loads the default fonts instead now.

What could have changed that behaviour? Was there a recent update in Ubuntu 20.10 that changed the behaviour of Firefox?

I get this error in the console which is strange:

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

it is strange, because the font is in the same domain (localhost) so there shouldn't be a CORS error.

The same site works fine in Chrome and loads the fonts. Also in Waterfox-classic it loads the fonts just fine.

I would like to add something like:

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

but I serve the site locally with ZeroNet, which is html-, CSS- and JS-only, so I think, I cannot change the headers sent.

I tried in firefox about:config:

  • disable content.cors.disable (empty string)
  • security.fileuri.strict_origin_policy False
  • privacy.file_unique_origin False

but none of those had any effect.

If I install the access-control-allow-origin addon and enable Access-Control-Allow-Origin: * it also works fine, (I added that as a workaround below.)

How do I set Firefox to ignore the Cross-Origin Request?

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • If you want to set headers. Does this help: https://stackoverflow.com/a/51919307/7906655 – mangupt Apr 15 '21 at 15:15
  • @mangupt, can you provide an answer how to send a header, so that firefox will ignore any CORS errors? – rubo77 Apr 15 '21 at 17:56
  • But I think there must be another problem with my Firefox, I have the same CORS errors on youtube now: The page loads, but when it tries to start the video, I get a CORS error in the console there too – rubo77 Apr 15 '21 at 17:57
  • Maybe try refreshing or reinstalling. – mangupt Apr 16 '21 at 03:46
  • I don't want to repost the answer from above stack post. Can you please check that? You might need to use Selenium Wire instead to be able to set. Also check https://stackoverflow.com/a/15647143/7906655 why Selenium won't help you set headers. – mangupt Apr 16 '21 at 03:50

2 Answers2

1

might be your container/zeronet updated or firefox updated after that you are facing issue

For Temporary Workaround in chrome for localhost you can add — disable-web-security while starting chrome

in firefox you can do the following :

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

Sahil Aggarwal
  • 1,311
  • 1
  • 12
  • 29
  • I need to use Firefox. And I tried `security.fileuri.strict_origin_policy` already without effect – rubo77 Apr 14 '21 at 16:41
0

Workaround

  1. go to about:preferences and create a new empty profile
  2. change to that profile in a new window
  3. install the Allow CORS: Access-Control-Allow-Origin there

now change your python code to use that new profile with

profile = webdriver.FirefoxProfile("/home/your_user/.mozilla/firefox/your_new_profile")

This will start the profile, wich already has installed the addon that fixes the CORS problem.

rubo77
  • 19,527
  • 31
  • 134
  • 226