diff options
author | Yuchen Pei <hi@ypei.me> | 2022-09-23 11:55:04 +1000 |
---|---|---|
committer | Yuchen Pei <hi@ypei.me> | 2022-09-23 11:56:46 +1000 |
commit | 5fa19f66d2c39bf14e653765a2f5f86820cd3d3d (patch) | |
tree | 4ca2ae6a017965fd59c3742bffdef39532607bf4 /content | |
parent | e56023afced93d5620a07fcbdfc0075d1016ba14 (diff) |
fixing a bug introduced in 136cb7354a7.
- the base url of the weblabel page should be the its own url unless
set. otherwise the content script may use the url of the current
page that references the weblabels page.
Diffstat (limited to 'content')
-rw-r--r-- | content/externalLicenseChecker.js | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/content/externalLicenseChecker.js b/content/externalLicenseChecker.js index cfcc897..f59eed8 100644 --- a/content/externalLicenseChecker.js +++ b/content/externalLicenseChecker.js @@ -31,14 +31,20 @@ const fetchWebLabels = async args => { const { map, cache } = args; const link = document.querySelector(`link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]`); - const baseURL = link ? link.href : cache.webLabels && new URL(cache.webLabels.href, document.baseURI); - if (baseURL) try { - const response = await fetch(baseURL); + const webLabelsUrl = link ? link.href : cache.webLabels && new URL(cache.webLabels.href, document.baseURI); + if (webLabelsUrl) try { + const response = await fetch(webLabelsUrl); if (!response.ok) throw `${response.status} ${response.statusText}`; const doc = new DOMParser().parseFromString( await response.text(), 'text/html' ); + // Sets the base url to be the url of the weblabel page if it + // does not exist. Otherwise relative links on the weblabels + // page will use the base of the current page. + if (!doc.querySelector("base")) { + doc.head.appendChild(doc.createElement("base")).href = webLabelsUrl; + } const link = a => ({ url: a.href, label: a.textContent }); const firstLink = parent => link(parent.querySelector("a")); const allLinks = parent => Array.prototype.map.call(parent.querySelectorAll("a"), link); @@ -50,7 +56,7 @@ const sources = cols[2] ? allLinks(cols[2]) : []; map.set(script.url, { script, licenseLinks, sources }); } catch (e) { - console.error("LibreJS: error parsing Web Labels at %s, row %s", baseURL, row.innerHTML, e); + console.error("LibreJS: error parsing Web Labels at %s, row %s", webLabelsUrl, row.innerHTML, e); } } } catch (e) { |