Skip to content

Instantly share code, notes, and snippets.

@westc
Last active August 6, 2018 02:26
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 westc/0c9866fc52f5e6dab2a098a7bb51573c to your computer and use it in GitHub Desktop.
Save westc/0c9866fc52f5e6dab2a098a7bb51573c to your computer and use it in GitHub Desktop.
Gets Bible stats from the Online Watchtower Library. Can be used to find out how many new Bibles, study Bibles, sign Bibles and other Bibles there are.
(function () {
var myVue;
var urlPrefix = location.hostname === 'wol.jw.org' ? '' : 'https://cors-anywhere.herokuapp.com/';
var bibles = window.__bibles__ = [];
bibles.logSummary = logSummary;
function loadScriptsIn(doc, urls, callback) {
var scriptsLeft = urls.length;
urls.forEach(function(url) {
var script = doc.createElement('script');
script.onerror = script.onload = function() {
if (!--scriptsLeft) {
callback();
}
};
script.src = url;
doc.head.appendChild(script);
});
}
function createFrame(callback) {
var div = document.createElement('div');
div.style.cssText = 'position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 9999999; padding: 10px;';
var iframe = document.createElement('iframe');
iframe.src = location.href;
iframe.frameborder = 0;
iframe.style.cssText = 'width: 100%; height: 100%; background: rgba(255,255,255,0.9); box-shadow: 0 0 5px; border-radius: 5px;';
document.body.appendChild(div);
div.appendChild(iframe);
function pollFrame() {
if (iframe.contentWindow && iframe.contentWindow.document && iframe.contentWindow.document.readyState === 'complete') {
callback(iframe);
}
else {
setTimeout(pollFrame, 50);
}
}
pollFrame();
}
function parsePage(url, callback) {
jQuery.get(urlPrefix + url, function (response) {
callback(jQuery(response));
});
}
function logGroup(groupName, filter) {
var filteredBibles = bibles.filter(filter);
var bibleGroup = { name: groupName, bibles: [] };
myVue.bibleGroups.push(bibleGroup);
console.groupCollapsed(groupName + ' (' + filteredBibles.length + ')');
filteredBibles.forEach(function (bible, i) {
bibleGroup.bibles.push(bible);
console.log((i + 1) + '. ' + bible.url + ' (' + bible.eLang + ' - ' + bible.name + ')');
});
console.groupEnd();
}
function logSummary() {
console.log("All bible data can now be found in the __bibles__ variable:", bibles);
logGroup('New Study Bibles', function (bible) { return bible.kind === 'study'; });
logGroup('New Bibles', function (bible) { return bible.kind === 'new'; });
logGroup('Sign Bibles', function (bible) { return bible.kind === 'sign'; });
logGroup('Other Bibles', function (bible) { return !bible.kind; });
}
function fixWOLURL(url) {
return 'https://wol.jw.org' + url.replace(urlPrefix, '').replace(/^\w+:\/\/[^\/]+/, '');
}
function startParsingPage() {
parsePage('https://wol.jw.org/en/wol/li/r1/lp-e', function (jElem) {
console.clear();
var jLinks = jElem.find('a[data-rsconf]');
var langCount = jLinks.length;
var langsLeft = langCount;
jLinks.each(function (i, a) {
var data = $(a).data();
var eLang = $('.meps-lang-E', a).text().trim();
var isSign = !!$('.signLanguageLibrary', a)[0];
var url = 'https://wol.jw.org/' + data.locale + '/wol/bibles/' + data.rsconf + '/' + data.lib;
parsePage(url, function (jElem) {
jElem.find('.directory a').each(function (i, a) {
bibles.push({
eLang: eLang,
isSign: isSign,
url: fixWOLURL(a.href),
biblesURL: fixWOLURL(url),
data: data,
name: $('.cardTitle .title', a).text().trim(),
year: +$('.cardTitle .details', a).text(),
kind: isSign
? 'sign'
: /\/nwt\/thumbnail/.test($('img', a).prop('src'))
? 'new'
: /\/nwtsty\/thumbnail/.test($('img', a).prop('src'))
? 'study'
: null
});
});
if (myVue.langsLeft = --langsLeft) {
console.log('There ' + (langsLeft - 1 ? 'are' : 'is') + ' ' + langsLeft + ' language' + (langsLeft - 1 ? 's' : '') + ' left to process...');
}
else {
console.clear();
logSummary();
}
});
});
});
}
createFrame(function(iframe) {
var win = iframe.contentWindow;
var doc = win.document;
doc.write(''
+ '<div id="myVue">'
+ '<button class="btn btn-danger" v-on:click="close" style="position: fixed; top: 5px; right: 5px; z-index: 1;">&times;</button>'
+ '<template v-if="langsLeft !== 0">{{ waitingMessage }}</template>'
+ '<template v-for="bibleGroup in bibleGroups">'
+ '<h1>{{ bibleGroup.name }} ({{ bibleGroup.bibles.length }})</h1>'
+ '<table class="table table-striped table-hover">'
+ '<thead>'
+ '<tr>'
+ '<th>Language</th>'
+ '<th>Native Name</th>'
+ '<th>Year</th>'
+ '</tr>'
+ '</thead>'
+ '<tbody>'
+ '<tr v-for="bible in bibleGroup.bibles">'
+ '<td><a v-bind:href="bible.url" target="_blank">{{ bible.eLang }}</a></td>'
+ '<td><a v-bind:href="bible.url" target="_blank">{{ bible.name }}</a></td>'
+ '<td>{{ bible.year }}</td>'
+ '</tr>'
+ '</tbody>'
+ '</table>'
+ '</template>'
+ '</div>');
loadScriptsIn(
doc,
[
'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js',
'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js'
],
function() {
doc.body.style.cssText = 'padding: 10px; background-color: rgba(0,0,0,0);';
var link = doc.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css';
doc.head.appendChild(link);
myVue = new win.Vue({
el: '#myVue',
data: {
langsLeft: null,
bibleGroups: []
},
methods: {
close: function() {
iframe.parentNode.removeChild(iframe);
}
},
computed: {
waitingMessage: function() {
if (this.langsLeft === null) {
return 'Loading languages data from the languages page...';
}
return 'There ' + (this.langsLeft !== 1 ? 'are ' + this.langsLeft + ' languages' : 'is only one language') + ' to parse...';
}
}
});
startParsingPage();
}
);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment