// Hey there! Don't worry, nothing bad here, feel free to read the code, not obfuscated.
// We fetch the keywords from SeoJuice
// We replace the keywords in the content part of the website with the keywords.
function init() {
let currentPageUrl = window.location.href.replace(/(?<=&|\?)(utm_.*?|gclid=.*?|fbclid=.*?|msclkid=.*?)(&|$)/igm, "");
if (window.loadedSeojuice) {
return;
}
window.loadedSeojuice = true;
function showError(text) {
console.error('[SeoJuice] Error:', text)
}
setTimeout(function() {
fetch('https://seojuice.io/suggestions?url=' + encodeURIComponent(currentPageUrl))
.then(response => response.json())
.then(data => {
var errors = data["errors"];
var base = data["base"];
var isAsian = data["isAsian"] || false;
var alreadyReplaced = [];
if (errors.length > 0) {
errors.forEach((error) => {
showError(error);
});
return;
}
if ((window.location.origin + window.location.pathname) !== base) {
showError("Link Base doesn't match");
}
let links = data["suggestions"];
function replaceText(node, keyword, link) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.tagName && node.tagName.toUpperCase() === 'SCRIPT' ||
(node.parentNode && node.parentNode.tagName.toUpperCase() === 'SCRIPT')) {
return;
}
if (["H1", "H2", "H3", "H4", "H5", "A", "CANVAS"].indexOf(node.parentNode.tagName.toUpperCase()) !== -1) {
return;
}
if (node.parentNode.parentNode && ["H1", "H2", "H3", "H4", "H5", "A", "CANVAS"].indexOf(node.parentNode.parentNode.tagName.toUpperCase()) !== -1) {
return;
}
var parser = new DOMParser();
var doc = parser.parseFromString(keyword, 'text/html');
var plainTextPhrase = doc.body.textContent || "";
var escapedPlainTextPhrase = plainTextPhrase.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
var match;
if (isAsian) {
match = node.textContent.match(new RegExp(escapedPlainTextPhrase, 'i'));
} else {
match = node.textContent.match(new RegExp(" " + escapedPlainTextPhrase, 'i'));
}
if (!match || alreadyReplaced.includes(keyword.toLowerCase())) {
return;
}
if (isAsian) {
match = node.textContent.match(new RegExp("(?<=[\\p{IsHan}\\p{IsBopo}\\p{IsHira}\\p{IsKatakana}]?)" + escapedPlainTextPhrase + "[\\.{!\\?}(|\\]\\\\]?(?![a-zA-Z])(?=[\\)\\/]?)"), 'i')
} else {
match = node.textContent.match(new RegExp("(?<=\\s?\\(?\\\"?)" + escapedPlainTextPhrase + "[\\.\\s{!\\?}(|\\]\\\\]?(?![a-zA-Z])(?=[\\)\\/]?)"), 'i')
}
if (match) {
// Can't be the first word, so we look for space before and after.
var anchorIndex = match[0].toLowerCase().indexOf(keyword.toLowerCase());
if (anchorIndex !== -1) {
if (match[0].toLowerCase().trim() !== keyword.toLowerCase()
&& [".", ",", "!", ")", "?", "\""].indexOf(match[0].toLowerCase().trim().slice(-1)) === -1) {
return
}
var matchIndex = node.textContent.toLowerCase().indexOf(match[0].toLowerCase());
var newLink = link.cloneNode(true);
var afterLink = node.splitText(matchIndex + anchorIndex);
afterLink.textContent = afterLink.textContent.substring(keyword.length);
node.parentNode.insertBefore(newLink, afterLink);
alreadyReplaced.push(keyword.toLowerCase());
}
}
} else {
Array.from(node.childNodes).forEach((node) => replaceText(node, keyword, link));
}
}
if (Array.isArray(links)) {
links.forEach(function (link) {
let el = document.createElement('a');
el.href = link.url;
el.innerText = link.keyword;
replaceText(document.body, link.keyword, el);
});
}
})
.catch(error => showError(error));
}, 10)
}
document.addEventListener('DOMContentLoaded', init);
init();