You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into something similar to #671: relative URLs don't resolve to the expected absolute URL.
I use Readability from a WebExtension background script. The script fetch()es HTML resources and parses them into DOMs using the DOMParser browser API.
The resulting absolute URLs look something like moz-extension://<extension-base-url>/<relative-link-here>, since Readability gets the base URI from document.baseURI (which is window.location by default):
Remember to pass the page's URI as the `url` option in the `JSDOM` constructor (as shown in the example above), so that Readability can convert relative URLs for images, hyperlinks, etc. to their absolute counterparts.
However, DOMParser doesn't have a url option.
A workaround I've found is to inject a <base> element into the document's <head> before handing it over to Readability. This works because document.baseURI resolves to whatever is in <base>, only falling back to window.location if no <base> is found.
This works but feels a little bit brittle (it could break if Readability changes how it deals with relative URLs internally). Have we considered adding a baseURI option that, if explicitly set, overrides document.baseURI?
The text was updated successfully, but these errors were encountered:
robinmetral
changed the title
Add a baseURI option to resolve to the expected absolute URLs
Add a baseURI option to resolve to arbitrary absolute URLs
Mar 7, 2025
This works but feels a little bit brittle (it could break if Readability changes how it deals with relative URLs internally). Have we considered adding a baseURI option that, if explicitly set, overrides document.baseURI?
I've not come across this usecase but the DOMParser scenario makes sense - it's a bit unfortunate the consumer cannot set a base URI. I'd take a patch? :-)
I'm running into something similar to #671: relative URLs don't resolve to the expected absolute URL.
I use Readability from a WebExtension background script. The script
fetch()
es HTML resources and parses them into DOMs using theDOMParser
browser API.The resulting absolute URLs look something like
moz-extension://<extension-base-url>/<relative-link-here>
, since Readability gets the base URI fromdocument.baseURI
(which iswindow.location
by default):readability/Readability.js
Line 457 in 04fd32f
In #671, getting the expected absolute URLs was achieved by passing
JSDOM
aurl
option. This is now well-documented on theREADME
:readability/README.md
Line 100 in 04fd32f
However,
DOMParser
doesn't have aurl
option.A workaround I've found is to inject a
<base>
element into the document's<head>
before handing it over to Readability. This works becausedocument.baseURI
resolves to whatever is in<base>
, only falling back towindow.location
if no<base>
is found.This works but feels a little bit brittle (it could break if Readability changes how it deals with relative URLs internally). Have we considered adding a
baseURI
option that, if explicitly set, overridesdocument.baseURI
?The text was updated successfully, but these errors were encountered: