Created
May 26, 2018 12:57
-
-
Save mscalora/de53c2eafa64d0ac573f4dd9c45d4ddf to your computer and use it in GitHub Desktop.
Hoist surveys up into the article body on TownNews sites using The World Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function twtInsertAfter(newNode, referenceNode) { | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); | |
} | |
setTimeout(function () { | |
var d = document, $q = d.querySelector.bind(d), $g = d.getElementById.bind(d); | |
if (!$q('#twt-engagement-tool-widget, .twt-engagement-tool-widget-container')) { | |
var i, e = document.createElement('div'); | |
e.id = 'twt-engagement-tool-widget'; | |
var paras = d.querySelectorAll('[itemprop="articleBody"] p'); | |
for (i = 0; i < paras.length; i++) { | |
var p = paras[i], t = p.innerText; | |
if (/📊|world table survey|resilience/i.test(t)) { | |
twtInsertAfter(e, p); | |
return; | |
} | |
} | |
var locs = [$g('tncms-region-article_instory_middle'), | |
$g('tncms-region-article_instory_top'), | |
$g('tncms-region-article_instory_bottom')]; | |
for (i = 0; i < locs.length; i++) { | |
if (locs[i] && locs[i].clientHeight === 0) { | |
locs[i].appendChild(e); | |
return; | |
} | |
} | |
var articleBody = $q('[itemprop="articleBody"]'); | |
if (articleBody) { | |
twtInsertAfter(e, articleBody); | |
} | |
} | |
},90); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment