refactor: updateTags to modern js

This commit is contained in:
Barış Soner Uşaklı
2026-02-13 11:29:16 -05:00
parent a8c68ddc65
commit 3756a8fe6c

View File

@@ -298,70 +298,46 @@ ajaxify.widgets = { render: render };
ajaxify.updateTitle = updateTitle; ajaxify.updateTitle = updateTitle;
function updateTags() { function updateTags() {
const metaWhitelist = ['title', 'description', /og:.+/, /article:.+/, 'robots'].map(function (val) { const metaWhitelist = ['title', 'description', /og:.+/, /article:.+/, 'robots'].map(val => new RegExp(val));
return new RegExp(val);
});
const linkWhitelist = ['canonical', 'alternate', 'up']; const linkWhitelist = ['canonical', 'alternate', 'up'];
// Delete the old meta tags // Delete the old meta tags
Array.prototype.slice document.querySelectorAll('head meta').forEach(el => {
.call(document.querySelectorAll('head meta')) const name = el.getAttribute('property') || el.getAttribute('name') || '';
.filter(function (el) { if (metaWhitelist.some(exp => exp.test(name))) {
const name = el.getAttribute('property') || el.getAttribute('name'); el.remove();
return metaWhitelist.some(function (exp) { }
return !!exp.test(name); });
});
})
.forEach(function (el) {
document.head.removeChild(el);
});
// Add new meta tags // Add new meta tags
ajaxify.data._header.tags.meta ajaxify.data._header.tags.meta.forEach(async (tagObj) => {
.filter(function (tagObj) { const name = tagObj.name || tagObj.property;
const name = tagObj.name || tagObj.property; if (metaWhitelist.some(exp => exp.test(name))) {
return metaWhitelist.some(function (exp) {
return !!exp.test(name);
});
}).forEach(async function (tagObj) {
if (tagObj.content) { if (tagObj.content) {
tagObj.content = await translator.translate(tagObj.content); tagObj.content = await translator.translate(tagObj.content);
} }
const metaEl = document.createElement('meta'); const metaEl = document.createElement('meta');
Object.keys(tagObj).forEach(function (prop) { Object.keys(tagObj).forEach(prop => metaEl.setAttribute(prop, tagObj[prop]));
metaEl.setAttribute(prop, tagObj[prop]);
});
document.head.appendChild(metaEl); document.head.appendChild(metaEl);
}); }
});
// Delete the old link tags // Delete the old link tags
Array.prototype.slice document.querySelectorAll('head link').forEach(el => {
.call(document.querySelectorAll('head link')) const name = el.getAttribute('rel');
.filter(function (el) { if (linkWhitelist.some(item => item === name)) {
const name = el.getAttribute('rel'); el.remove();
return linkWhitelist.some(function (item) { }
return item === name; });
});
})
.forEach(function (el) {
document.head.removeChild(el);
});
// Add new link tags // Add new link tags
ajaxify.data._header.tags.link ajaxify.data._header.tags.link.forEach(async (tagObj) => {
.filter(function (tagObj) { if (linkWhitelist.some(item => item === tagObj.rel)) {
return linkWhitelist.some(function (item) {
return item === tagObj.rel;
});
})
.forEach(function (tagObj) {
const linkEl = document.createElement('link'); const linkEl = document.createElement('link');
Object.keys(tagObj).forEach(function (prop) { Object.keys(tagObj).forEach(prop => linkEl.setAttribute(prop, tagObj[prop]));
linkEl.setAttribute(prop, tagObj[prop]);
});
document.head.appendChild(linkEl); document.head.appendChild(linkEl);
}); }
});
} }
ajaxify.end = function (url, tpl_url) { ajaxify.end = function (url, tpl_url) {