Skip to content

Instantly share code, notes, and snippets.

@mscalora
Created May 26, 2018 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mscalora/de53c2eafa64d0ac573f4dd9c45d4ddf to your computer and use it in GitHub Desktop.
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
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