–> p”); window.eleCount = document.querySelectorAll(“div.article-content > p”).length; window.promos = document.querySelectorAll(“div.article-content > div.nlp-ignore-block > aside.promo-link”); window.skipP = 5; // the number of paragraphs to skip. window.mobileOrDesktop = window.screen.width window.skipP) { // there needs to be more paragraphs than window.skipP // set the number of possible speedbumps window.maxLinks = Math.floor(window.childEles.length / window.skipP); } else { // the paragraph count is too low to inject a speedbump window.maxLinks = 0; } if (window.maxLinks > 0 ) { window.topStoriesJSON = “https://media.psg.nexstardigital.net/kxan/news/speedbump/most_page_views.json?v=” + cacheBreak() + 1; var request = new XMLHttpRequest(); request.open(‘GET’, window.topStoriesJSON, true); request.onload = function() { if (this.status >= 200 && this.status 0 ) { console.log(‘At least 1 promo link already exists’); let promoParent = promoEle[promoEle.length – 1].parentElement; let promoPos = whereIsPromo(promoParent); nthChild = nthChild + promoPos + 1; } // end check for existing promo link console.log(‘——–2————————————————-‘); console.log(`window.eleCount: ${window.eleCount}`); console.log(`nthChild: ${nthChild}`); console.log(`window.eleCount – nthChild: ${window.eleCount – nthChild}`); console.log(`window.skipP: ${window.skipP}`); // check for enough room at the bottom of the story // if ( window.eleCount – nthChild >= window.skipP ) { if ( window.eleCount – nthChild >= 2 ) { console.log(‘——–3————————————————-‘); for (var i = 0; i < window.topStoriesData.length; i++) { let link = window.topStoriesData[i].path; let feedPath = link.split("kxan.com")[1]; // check to see if link is the same as the user's current story; skip if the same if (thisPath.search(feedPath) < 0 && thisPath.search("/author/") p:nth-child(${nthChild})`); let bottomBread = document.querySelector(`div.article-content > p:nth-child(${nthChild + 1})`); // let plate = document.querySelector(`div.article-content > p:nth-child(${nthChild + 2})`); // console.log(`window.skipP + 1: ${window.skipP + 1}`); // console.log(`——————-`); // console.log(topBread); // console.log(bottomBread); // console.log(`———————`); // if (topBread !== null && topBread.tagName === ‘P’ && bottomBread.tagName === ‘P’ && plate.tagName === ‘P’) { if (topBread !== null && bottomBread !== null && topBread.tagName === ‘P’ && bottomBread.tagName === ‘P’) { bottomBread.before(promoDiv); nthChild += (window.skipP + 1); } else { nthChild++; } // console.log(`i: ${i}`); // console.log(`maxLinks: ${window.maxLinks}`); if (i + 1 > window.maxLinks) { // adding +1 at end of the loop stops orphan promo links at the bottom of the story, // and adding at the end of the loop allows at least one promo link in the story // console.log(“breaking promo link loop”); break; } // console.log(‘————————————————————————-‘); } } } } else { // We reached our target server, but it returned an error console.log(`error retrieving top stories JSON. Status: ${this.status}`); } }; request.onerror = function() { // There was a connection error of some sort console.log(‘unknown error retrieving top stories JSON’); }; request.send(); } function cacheBreak() { let now = new Date().getTime(); now = Math.floor(now / 1000 / 300); return “” + now; } function whereIsPromo(ele) { let parent = ele.parentNode; // console.log(`parent: ${parent}`); let children = parent.children; // console.log(`children: ${children}`); // console.log(`children.length: ${children.length}`); var i = children.length – 1; for (; i >= 0; i–) { if (ele == children[i]){ break; } } console.log(`whereIsPromo: ${i}`); return i; }; } // –> ]]>