fix: rewrite improvements, including setting the base tag

This commit is contained in:
Derrick Hammer 2023-10-17 09:48:24 -04:00
parent 6738b2337f
commit ad71feea88
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 10 deletions

View File

@ -12,6 +12,14 @@ export default class URLRewriteFilter implements ContentFilter {
let html = await response.text();
const $ = cheerio.load(html);
// Insert or adjust the <base> tag
if ($("base").length) {
$("base").attr("href", "/browse/");
} else {
$("head").prepend('<base href="/browse/">');
}
["a", "link", "script", "img"].forEach((tag) => {
$.root()
.find(tag)
@ -20,16 +28,7 @@ export default class URLRewriteFilter implements ContentFilter {
let urlValue = $(element).attr(attrName);
if (urlValue) {
if (!isICANN(urlValue)) {
if (
urlValue.startsWith("/") ||
urlValue.startsWith("../") ||
urlValue.startsWith("http")
) {
console.log("before", urlValue);
if (!urlValue.startsWith("/")) {
urlValue = `/${urlValue}`;
}
console.log("after", urlValue);
if (urlValue.startsWith("http")) {
$(element).attr(attrName, `/browse${urlValue}`);
}
}