Skip to content

Instantly share code, notes, and snippets.

@zuphilip
Created March 27, 2018 19:19
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 zuphilip/58701fbce385de5d6f665d1b01e1058e to your computer and use it in GitHub Desktop.
Save zuphilip/58701fbce385de5d6f665d1b01e1058e to your computer and use it in GitHub Desktop.
First try for a translator for the Vorarlberger Landesbibliothek (Aqua Browser)
{
"translatorID": "d84805ad-46b2-4739-a0be-0a54aa7aa780",
"label": "Vorarlberger Landesbibliothek",
"creator": "Philipp Zumstein",
"target": "^https?://vlb-browser\\.vorarlberg\\.at/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2018-03-27 19:18:00"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2018 Philipp Zumstein
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
// attr()/text() v2
function attr(docOrElem,selector,attr,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.getAttribute(attr):null;}function text(docOrElem,selector,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.textContent:null;}
// INFO for testing in Scaffold you need to choose the ablfram as test frame
function detectWeb(doc, url) {
// TODO detection does not work outside Scaffold
if (getContentDocument(doc)) {
return "book";
} else if (getSearchResults(doc, true)) {
return "multiple";
}
}
function getContentDocument(doc) {
if (doc.getElementById('ifraResult')
&& doc.getElementById('ifraResult').contentDocument.getElementById('drif')
&& doc.getElementById('ifraResult').contentDocument.getElementById('drif').contentDocument.getElementById('fullrecord')) {
return doc.getElementById('ifraResult').contentDocument.getElementById('drif').contentDocument;
}
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
if (!doc.getElementById('ifraResult')) return false;
doc = doc.getElementById('ifraResult').contentDocument;
var rows = doc.querySelectorAll('#resulttable .titlenew a');
for (let i=0; i<rows.length; i++) {
let href = rows[i].href;
let title = ZU.trimInternal(rows[i].textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) {
return true;
}
var articles = [];
for (var i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
} else {
scrape(doc, url);
}
}
function scrape(doc, url) {
doc = getContentDocument(doc);
var risURL = ZU.xpathText(doc, '//*[@id="sideinfo"]//a[contains(@href, "export.ashx?type=export-ris")]/@href');
ZU.doGet(risURL, function(text) {
// Generic can be anything, but maybe most likely a book
text = text.replace(/TY - (Boek|GEN)/, "TY - BOOK");
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.setHandler("itemDone", function(obj, item) {
// sometimes the creators contain also their role as part of the first name
for (let i=0; i<item.creators.length; i++) {
if (item.creators[i].firstName) {
item.creators[i].firstName = item.creators[i].firstName.replace(/ VerfasserIn$/, '');
}
}
delete item.tags;
delete item.abstractNote;
item.complete();
});
translator.translate();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment