class LinkHandler implements HTMLRewriterElementContentHandlers {
// Current URL
context: EventContext<any, any, any>;
currentURL: string;
constructor(context: EventContext<any, any, any>) {
this.context = context;
this.currentURL = context.request.url;
}
// Go through all attributes in the config (src, href, etc) and rewrite them to the correct URI
element(element: Element) {
// Show the element type and id
console.log(`Link element, tag: ${element.tagName}`);
// // Check if the element has any rewriteable attributes
for (let attribute of config["reverse-proxy"]["proxied-attributes"]) {
// Check if the element has the attribute
if (!element.getAttribute(attribute)) {
// Rewrite the attribute
continue;
}
// Check the current locale, and prepend it to the URL in the attribute
let currentLocale = getCurrentLocale(new URL(this.currentURL));
// If no current locale, then continue
if (!currentLocale) {
continue;
}
// Replace the "target-url" with "target-url"/currentLocale/
let new_attribute = element.getAttribute(attribute).replace(config["reverse-proxy"]["target-url"], `${config["reverse-proxy"]["target-url"]}/${currentLocale}`);
// If it's different, then log it and change it
if (new_attribute == element.getAttribute(attribute)) {
continue;
}
console.log("Changed attribute from " + element.getAttribute(attribute) + " to " + new_attribute + " for attribute " + attribute + " on element " + element.tagName);
element.setAttribute(attribute, new_attribute);
}
// Kma
}
}
class LinkHandler implements HTMLRewriterElementContentHandlers {
// Current URL
context: EventContext<any, any, any>;
currentURL: string;
constructor(context: EventContext<any, any, any>) {
this.context = context;
this.currentURL = context.request.url;
}
// Go through all attributes in the config (src, href, etc) and rewrite them to the correct URI
element(element: Element) {
// Show the element type and id
console.log(`Link element, tag: ${element.tagName}`);
// // Check if the element has any rewriteable attributes
for (let attribute of config["reverse-proxy"]["proxied-attributes"]) {
// Check if the element has the attribute
if (!element.getAttribute(attribute)) {
// Rewrite the attribute
continue;
}
// Check the current locale, and prepend it to the URL in the attribute
let currentLocale = getCurrentLocale(new URL(this.currentURL));
// If no current locale, then continue
if (!currentLocale) {
continue;
}
// Replace the "target-url" with "target-url"/currentLocale/
let new_attribute = element.getAttribute(attribute).replace(config["reverse-proxy"]["target-url"], `${config["reverse-proxy"]["target-url"]}/${currentLocale}`);
// If it's different, then log it and change it
if (new_attribute == element.getAttribute(attribute)) {
continue;
}
console.log("Changed attribute from " + element.getAttribute(attribute) + " to " + new_attribute + " for attribute " + attribute + " on element " + element.tagName);
element.setAttribute(attribute, new_attribute);
}
// Kma
}
}