From 1aa4d4c6426de3d86a6e8586eb7c0b6bdc1de9ea Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Sun, 3 Jul 2011 08:18:44 +0000 Subject: [PATCH] Make Nature Chrome/Safari-compatible and fix search results --- translators/Nature.js | 246 +++++++++++++++++++++++++++++++++--------- 1 file changed, 196 insertions(+), 50 deletions(-) diff --git a/translators/Nature.js b/translators/Nature.js index 1cea82fe0..bc20f2b55 100644 --- a/translators/Nature.js +++ b/translators/Nature.js @@ -1,35 +1,30 @@ { - "translatorID":"6614a99-479a-4524-8e30-686e4d66663e", - "translatorType":4, - "label":"Nature", - "creator":"Simon Kornblith", - "target":"https?://www\\.nature\\.com[^/]*/(?:[^/]+/journal/v[^/]+/n[^/]+/(?:(?:full|abs)/.+\\.html|index.html)|search/executeSearch)", - "minVersion":"1.0.0b3.r1", - "maxVersion":"", - "priority":100, - "inRepository":true, - "lastUpdated":"2011-01-11 04:31:00" + "translatorID": "6614a99-479a-4524-8e30-686e4d66663e", + "label": "Nature", + "creator": "Simon Kornblith", + "target": "https?://www\\.nature\\.com[^/]*/(?:[^/]+/journal/v[^/]+/n[^/]+/(?:(?:full|abs)/.+\\.html|index.html)|search/executeSearch)", + "minVersion": "1.0.0b3.r1", + "maxVersion": "", + "priority": 100, + "inRepository": true, + "translatorType": 4, + "browserSupport": "gcs", + "lastUpdated": "2011-07-03 04:19:18" } +var articleRe = /(https?:\/\/[^\/]+\/[^\/]+\/journal\/v[^\/]+\/n[^\/]+\/)(full|abs)(\/.+)\.html/; + function detectWeb(doc, url) { - var articleRe = /(https?:\/\/[^\/]+\/[^\/]+\/journal\/v[^\/]+\/n[^\/]+\/)(full|abs)(\/.+\.)html/; - if (articleRe.test(url)) { if (doc.evaluate('//a[contains(@href, ".ris")]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) { return "journalArticle"; - } else { return false; } + } else { + return false; + } } else { - var namespace = doc.documentElement.namespaceURI; - var nsResolver = namespace ? function(prefix) { - if (prefix == 'x') return namespace; else return null; - } : null; + var links = doc.evaluate('//ol[@class="results-list"]//h2[@class="atl"]/a', doc, null, XPathResult.ANY_TYPE, null); - var tableRows = doc.evaluate('//*[@class="atl"] | //*[@class="articletitle"] | //h4[@class="norm"]', - doc, nsResolver, XPathResult.ANY_TYPE, null); - var fulltextLinks = doc.evaluate('//a[text() = "Full Text"] | //a[text() = "Full text"] | //a[text() = "Full Text "]', - doc, nsResolver, XPathResult.ANY_TYPE, null); - - if(tableRows.iterateNext() && fulltextLinks.iterateNext()) { + if(links.iterateNext()) { return "multiple"; } } @@ -38,40 +33,37 @@ function detectWeb(doc, url) { } function doWeb(doc, url) { - var namespace = doc.documentElement.namespaceURI; - var nsResolver = namespace ? function(prefix) { - if (prefix == 'x') return namespace; else return null; - } : null; - - var articleRe = /(https?:\/\/[^\/]+\/[^\/]+\/journal\/v[^\/]+\/n[^\/]+\/)(full|abs)(\/.+)\.html/; var m = articleRe.exec(url); if(!m) { // search page var items = new Array(); - var tableRows = doc.evaluate('//*[@class="atl"] | //*[@class="articletitle"] | //h4[@class="norm"]', - doc, nsResolver, XPathResult.ANY_TYPE, null); - var fulltextLinks = doc.evaluate('//a[text() = "Full Text"] | //a[text() = "Full text"] | //a[text() = "Full Text "]', - doc, nsResolver, XPathResult.ANY_TYPE, null); - var tableRow, fulltextLink; - while((tableRow = tableRows.iterateNext()) && (fulltextLink = fulltextLinks.iterateNext())) { - items[fulltextLink.href] = Zotero.Utilities.trimInternal(tableRow.textContent); + var links = doc.evaluate('//ol[@class="results-list"]//h2[@class="atl"]/a', doc, null, XPathResult.ANY_TYPE, null); + var link; + while((link = links.iterateNext())) { + items[link.href] = Zotero.Utilities.trimInternal(link.textContent); } - items = Zotero.selectItems(items); - if(!items) return true; - - var urls = new Array(); - for(var url in items) { - urls.push(url); - } + Zotero.selectItems(items, function(items) { + if(!items) return true; + + var urls = new Array(); + for(var url in items) { + urls.push(url); + } + processArticles(urls); + }); } else { - var urls = [url]; + processArticles([url]); } - - var RIS = new Array(); - var regexps = new Array(); + + Zotero.wait(); +} + +function processArticles(urls) { + var RIS = new Array(); + var regexps = new Array(); for each(var item in urls) { var m = articleRe.exec(item); @@ -102,6 +94,160 @@ function doWeb(doc, url) { }); translator.translate(); }, function() { Zotero.done(); }); - - Zotero.wait(); -} \ No newline at end of file +} + +/** BEGIN TEST CASES **/ +var testCases = [ + { + "type": "web", + "url": "http://www.nature.com/emboj/journal/vaop/ncurrent/full/emboj2011212a.html", + "items": [ + { + "itemType": "journalArticle", + "creators": [ + { + "lastName": "Huang", + "firstName": "Jian", + "creatorType": "author" + }, + { + "lastName": "Yao", + "firstName": "Ling", + "creatorType": "author" + }, + { + "lastName": "Xu", + "firstName": "Rongting", + "creatorType": "author" + }, + { + "lastName": "Wu", + "firstName": "Huacheng", + "creatorType": "author" + }, + { + "lastName": "Wang", + "firstName": "Min", + "creatorType": "author" + }, + { + "lastName": "White", + "firstName": "Brian S", + "creatorType": "author" + }, + { + "lastName": "Shalloway", + "firstName": "David", + "creatorType": "author" + }, + { + "lastName": "Zheng", + "firstName": "Xinmin", + "creatorType": "author" + } + ], + "notes": [], + "tags": [], + "seeAlso": [], + "attachments": [ + { + "url": false, + "title": "Nature Snapshot", + "mimeType": "text/html" + }, + { + "url": false, + "title": "Nature Full Text PDF", + "mimeType": "application/pdf" + } + ], + "title": "Activation of Src and transformation by an RPTP[alpha] splice mutant found in human tumours", + "journalAbbreviation": "EMBO J", + "date": "online July 01, 2011", + "volume": "advance online publication", + "publisher": "European Molecular Biology Organization", + "ISBN": "1460-2075", + "ISSN": "1460-2075", + "url": "http://dx.doi.org/10.1038/emboj.2011.212", + "DOI": "10.1038/emboj.2011.212", + "publicationTitle": "EMBO J", + "libraryCatalog": "Nature" + } + ] + }, + { + "type": "web", + "url": "http://www.nature.com/nrn/journal/v12/n7/abs/nrn3042.html", + "items": [ + { + "itemType": "journalArticle", + "creators": [ + { + "lastName": "Heinz", + "firstName": "Adrienne J.", + "creatorType": "author" + }, + { + "lastName": "Beck", + "firstName": "Anne", + "creatorType": "author" + }, + { + "lastName": "Meyer-Lindenberg", + "firstName": "Andreas", + "creatorType": "author" + }, + { + "lastName": "Sterzer", + "firstName": "Philipp", + "creatorType": "author" + }, + { + "lastName": "Heinz", + "firstName": "Andreas", + "creatorType": "author" + } + ], + "notes": [], + "tags": [], + "seeAlso": [], + "attachments": [ + { + "url": false, + "title": "Nature Snapshot", + "mimeType": "text/html" + }, + { + "url": false, + "title": "Nature Full Text PDF", + "mimeType": "application/pdf" + } + ], + "title": "Cognitive and neurobiological mechanisms of alcohol-related aggression", + "journalAbbreviation": "Nat Rev Neurosci", + "date": "July 2011", + "volume": "12", + "issue": "7", + "pages": "400-413", + "publisher": "Nature Publishing Group, a division of Macmillan Publishers Limited. All Rights Reserved.", + "ISBN": "1471-003X", + "ISSN": "1471-003X", + "url": "http://dx.doi.org/10.1038/nrn3042", + "DOI": "10.1038/nrn3042", + "publicationTitle": "Nat Rev Neurosci", + "libraryCatalog": "Nature" + } + ] + }, + { + "type": "web", + "url": "http://www.nature.com/search/executeSearch?sp-q-1=&sp-q=nature&sp-p=all&sp-c=25&sp-m=0&sp-s=date_descending&include-collections=journals_nature%2Ccrawled_content&exclude-collections=journals_palgrave%2Clab_animal&sp-a=sp1001702d&sp-sfvl-field=subject%7Cujournal&sp-x-1=ujournal&sp-p-1=phrase&submit=go", + "items": "multiple" + }, + { + "type": "web", + "url": "http://www.nature.com/search/executeSearch?sp-q-1=NRN&sp-q=visual+search&sp-c=25&sp-m=0&sp-s=date_descending&include-collections=journals_nature%2Ccrawled_content&exclude-collections=journals_palgrave%2Clab_animal&sp-a=sp1001702d&sp-sfvl-field=subject%7Cujournal&sp-x-1=ujournal&sp-p-1=phrase&sp-p=all&submit=go", + "items": "multiple" + } +] +/** END TEST CASES **/ \ No newline at end of file