Skip to content

Instantly share code, notes, and snippets.

@black23
Created December 21, 2015 08:37
Show Gist options
  • Save black23/2e48a946c5b247b8f076 to your computer and use it in GitHub Desktop.
Save black23/2e48a946c5b247b8f076 to your computer and use it in GitHub Desktop.
Obálky z Obálek knih v OPACu Koha
/* Obálky */
var obalky = obalky || {};
obalky.protocol =
(document.location.protocol == 'https:') ? 'https://':'http://';
obalky.url = obalky.protocol+"www.obalkyknih.cz";
obalky.books = obalky.books || [];
obalky.version = "0.2.0";
obalky.id = 0;
obalky.msie = navigator.userAgent.indexOf("MSIE") != -1 ? 1 : 0;
obalky.callback = obalky.callback || function (books) {
for(var i=0;i<books.length;i++) {
for(var j=0;j<books[i]["callbacks"].length;j++) {
callback = books[i]["callbacks"][j];
var element = document.getElementById(callback["id"]);
var function_pointer = eval(callback["name"]);
if(function_pointer) function_pointer(element,books[i]);
}
}
};
obalky.download = obalky.download || function (books) {
var obalky_json = "obalky_json_"+obalky.id++;
var jsonScript = document.getElementById(obalky_json);
if (jsonScript) jsonScript.parentNode.removeChild(jsonScript);
var scriptElement = document.createElement("script");
scriptElement.setAttribute("id", obalky_json);
scriptElement.setAttribute("type", "text/javascript");
scriptElement.setAttribute("src", obalky.url+"/api/books?books="+
encodeURIComponent(JSON.stringify(books)));
document.documentElement.firstChild.appendChild(scriptElement);
};
obalky.findFirstNodeByClass = function (startNode, className) {
if(!(startNode.nodeType === 1)) return null; // skip non-elements
if(startNode.className == className) return startNode;
var childs = startNode.childNodes;
for(var i=0;i<childs.length;i++) {
var found = obalky.findFirstNodeByClass(childs[i], className);
if(found) return found;
}
return null;
};
obalky.findNodesByClass = function (startNode, className) {
var nodes = (startNode.className == className) ? [ startNode ] : [];
var childs = startNode.childNodes;
for(var i=0;i<childs.length;i++)
nodes = nodes.concat(obalky.findNodesByClass(childs[i], className));
return nodes;
};
obalky.getValue = function (startNode, className) {
var el = obalky.findFirstNodeByClass(startNode, className);
return el ? (el.textContent ? el.textContent : el.innerHTML ) : undefined;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment