function getUrlVar(name, target) { target = typeof target !== 'undefined' ? target : document.getElementById('related').src, name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); let regexS = "[\\?&]" + name + "=([^&#]*)"; let regex = new RegExp(regexS); let results = regex.exec(target); if (results == null) return ''; else return results[1]; } function buildUnit(data) { let o = document.createElement('div'); o.className = 'related'; let count = getUrlVar('count') ? parseInt(getUrlVar('count')) + 1 : 5; let showThumbnail = getUrlVar('thumbnail') ? parseInt(getUrlVar('thumbnail')) : 1; let perItemClass = getUrlVar('per_item_class') ? getUrlVar('per_item_class') : null; let titleClass = getUrlVar('title_class') ? getUrlVar('title_class') : null; var separators = ["-", "|"]; var separatorPattern = separators.map(function(separator) { return "\\" + separator; }).join("|"); data.value.slice(1, count).forEach(result => { var separatorRegex = new RegExp('(\\s[' + separatorPattern + ']+\\s|^[' + separatorPattern + ']+\\s)(.*)(?:\\s[' + separatorPattern + ']+\\s|$)', "i"); var modifiedTitle = result.title.replace(separatorRegex, ""); o.innerHTML += '' + (result.image.url && showThumbnail ? '' + modifiedTitle + '' : '') +'' + modifiedTitle + ''; }) document.getElementById('related').parentNode.insertBefore(o, document.getElementById('related').nextSibling); } function getJSON(url) { fetch(url) .then(response => response.json()) .then(data => buildUnit(data)) .catch(error => console.error(error)); } getJSON('https://related.pubforge.com/results.php?site='+getUrlVar('site')+'&q='+getUrlVar('title'));