Custom JavaScript - triplecanopy/b-ber GitHub Wiki
New projects are initialized with a single JavaScript file that normalizes some e-reader behavior and provides helpers for other reading environments. The JavaScript can be modified and adapted.
Scripts that must fire once the DOM is ready should use the domReady
function provided in a new project's application.js
.
// Examples for using the domReady function:
domReady(fn);
domReady(function() {});
domReady(fn, context);
domReady(function(context) {}, ctx);
This will ensure that the scripts run in all environments, including the b-ber-reader and e-readers that support JavaScript.
function doStuff() {
//
}
domReady(doStuff);
To target a specific environment, a user can access the env
property of the global window.bber
object:
function readerOnly() {
if (window.bber.env !== 'reader') {
return;
}
// Do something reader-specific
}
function handleClick(e) {
// Images wrapped in links still behave like links
window.location.href = this.getAttribute('href');
return false;
}
function main() {
// Normalize link behaviour on iBooks, without interfering with footnotes, but don't run in reader builds since it conflicts with the React event handlers.
if (window.bber.env === 'reader') return;
var links = document.getElementsByTagName('a');
links = Array.prototype.slice.call(links, 0);
links = links.filter(function(l) {
return l.classList.contains('footnote-ref') === false;
});
for (var i = 0; i < links.length; i++) {
links[i].onclick = handleClick;
}
}
domReady(function() {
main();
});