From 5fa19f66d2c39bf14e653765a2f5f86820cd3d3d Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 23 Sep 2022 11:55:04 +1000 Subject: 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. --- content/externalLicenseChecker.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'content') 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) { -- cgit v1.2.3