-
Notifications
You must be signed in to change notification settings - Fork 689
Description
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):
Line 457 in 04fd32f
var baseURI = this._doc.baseURI; |
In #671, getting the expected absolute URLs was achieved by passing JSDOM
a url
option. This is now well-documented on the README
:
Line 100 in 04fd32f
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
?