I'm writing an extension that recognizes links. If it's an internal link, the page won't reload. Otherwise, if it is an external link, it will open in a new tab.
Here is my code, i know it's look very bad 😅 😅
extend(CommentPost.prototype, 'oncreate', () => {
const baseUrl = app.forum.attribute('baseUrl');
const links = document.querySelectorAll('a');
for (let i = 0; i < links.length; i++) {
const link = links[i];
const href = link.getAttribute('href');
const discussRegex = new RegExp(`^${baseUrl}/d/`);
if (discussRegex.test(href)) {
link.addEventListener('click', (e) => {
e.preventDefault();
const url = href.substr(baseUrl.length);
m.route.set(url);
});
} else {
const hrefRegex = new RegExp(`^${baseUrl}`);
if (!hrefRegex.test(href)) {
const pathRegex = new RegExp(`^/`);
if (!pathRegex.test(href)) {
link.setAttribute('target', '_blank');
} else {
link.addEventListener('click', (e) => {
e.preventDefault();
m.route.set(href);
});
}
} else {
const adminRegex = new RegExp(`^${baseUrl}/admin`);
if (!adminRegex.test(href)) {
link.addEventListener('click', (e) => {
e.preventDefault();
m.route.set(href);
});
} else {
link.setAttribute('target', '_blank');
}
}
}
}
});
Would be glad if someone developed a extension like this 😄