Guest User

Untitled

a guest
Apr 18th, 2012
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. function detectWeb(doc, url) {
  2. var re = new RegExp('^http://(books|www)\\.google\\.[a-z]+(\.[a-z]+)?/books\\?(.*&)?(id|vid)=([^&]+)', 'i');
  3. if(re.test(doc.location.href)) {
  4. return "book";
  5. } else {
  6. return "multiple";
  7. }
  8. }
  9. function doWeb(doc, url) {
  10. var namespace = doc.documentElement.namespaceURI;
  11. var nsResolver = namespace ? function(prefix) {
  12. if (prefix == 'x') return namespace; else return null;
  13. } : null;
  14.  
  15. // get local domain suffix
  16. var psRe = new RegExp("https?://(books|www)\.google\.([^/]+)/");
  17. var psMatch = psRe.exec(url);
  18. var suffix = psMatch[2];
  19. var prefix = psMatch[1];
  20. var uri = doc.location.href;
  21. var newUris = new Array();
  22.  
  23. var re = new RegExp('^http://(?:books|www)\\.google\\.[a-z]+(?:\.[a-z]+)?/books\\?(?:.*&)?(id|vid)=([^&]+)', 'i');
  24. var m = re.exec(uri);
  25. if(m && m[1] == "id") {
  26. newUris.push("http://books.google.com/books/feeds/volumes/"+m[2]);
  27. } else if (m && m[1] == "vid") {
  28. var itemLinkWithID = doc.evaluate("/html/head/link", doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().href;
  29. var m = re.exec(itemLinkWithID);
  30. newUris.push("http://books.google.com/books/feeds/volumes/"+m[2]);
  31. } else {
  32. var items = getItemArrayGB(doc, doc, 'google\\.' + suffix + '/books\\?id=([^&]+)', '^(?:All matching pages|About this Book|Table of Contents|Index)');
  33. // Drop " - Page" thing
  34. //Zotero.debug(items);
  35. for(var i in items) {
  36. items[i] = items[i].replace(/- Page [0-9]+\s*$/, "");
  37. }
  38. items = Zotero.selectItems(items);
  39.  
  40. if(!items) {
  41. return true;
  42. }
  43.  
  44. for(var i in items) {
  45. var m = re.exec(i);
  46. newUris.push("http://books.google.com/books/feeds/volumes/"+m[2]);
  47. }
  48. }
  49.  
  50. var itemUrlBase = "http://"+prefix+".google."+suffix+"/books?id=";
  51.  
  52. for (var i in newUris) {
  53. var d = Zotero.Utilities.retrieveSource(newUris[i]);
  54. //Zotero.debug(d);
  55. parseXML(d, itemUrlBase);
  56. }
  57. }
  58.  
  59. function parseXML(text, itemUrlBase) {
  60. // Remove xml parse instruction and doctype
  61. text = text.replace(/<!DOCTYPE[^>]*>/, "").replace(/<\?xml[^>]*\?>/, "");
  62.  
  63. var xml = new XML(text);
  64.  
  65. default xml namespace = "http://purl.org/dc/terms"; with ({});
  66.  
  67. var newItem = new Zotero.Item("book");
  68.  
  69. var authors = xml.creator;
  70. for (var i in authors) {
  71. newItem.creators.push(Zotero.Utilities.cleanAuthor(authors[i].toString(), "author"));
  72. }
  73.  
  74. newItem.date = xml.date.toString();
  75.  
  76. var pages = xml.format.toString();
  77. var pagesRe = new RegExp(/(\d+)( pages)/);
  78. var pagesMatch = pagesRe.exec(pages);
  79. if (pagesMatch!=null) {
  80. newItem.numPages = pagesMatch[1];
  81. } else {
  82. newItem.numPages = pages;
  83. }
  84.  
  85. var ISBN;
  86. var ISBN10Re = new RegExp(/(ISBN:)(\w{10})$/);
  87. var ISBN13Re = new RegExp(/(ISBN:)(\w{13})$/);
  88. var identifiers = xml.identifier;
  89. for (var i in identifiers) {
  90. var ISBN10Match = ISBN10Re.exec(identifiers[i].toString());
  91. var ISBN13Match = ISBN13Re.exec(identifiers[i].toString());
  92. if (ISBN10Match != null) {
  93. ISBN = ISBN10Match[2];
  94. }
  95. if (ISBN13Match != null) {
  96. ISBN = ISBN13Match[2];
  97. }
  98. }
  99. newItem.ISBN = ISBN;
  100.  
  101. if (xml.publisher[0]) {
  102. newItem.publisher = xml.publisher[0].toString();
  103. }
  104.  
  105. newItem.title = xml.title[0].toString();
  106.  
  107. var url = itemUrlBase + xml.identifier[0];
  108.  
  109. newItem.attachments = [{title:"Google Books Link", snapshot:false, mimeType:"text/html", url:url}];
  110.  
  111. newItem.complete();
  112. }
  113.  
  114. /**
  115. * Grabs items based on URLs, modified for Google Books
  116. *
  117. * @param {Document} doc DOM document object
  118. * @param {Element|Element[]} inHere DOM element(s) to process
  119. * @param {RegExp} [urlRe] Regexp of URLs to add to list
  120. * @param {RegExp} [urlRe] Regexp of URLs to reject
  121. * @return {Object} Associative array of link => textContent pairs, suitable for passing to
  122. * Zotero.selectItems from within a translator
  123. */
  124. function getItemArrayGB (doc, inHere, urlRe, rejectRe) {
  125. var namespace = doc.documentElement.namespaceURI;
  126. var nsResolver = namespace ? function(prefix) {
  127. if (prefix == 'x') return namespace; else return null;
  128. } : null;
  129.  
  130. var availableItems = new Object(); // Technically, associative arrays are objects
  131.  
  132. // Require link to match this
  133. if(urlRe) {
  134. if(urlRe.exec) {
  135. var urlRegexp = urlRe;
  136. } else {
  137. var urlRegexp = new RegExp();
  138. urlRegexp.compile(urlRe, "i");
  139. }
  140. }
  141. // Do not allow text to match this
  142. if(rejectRe) {
  143. if(rejectRe.exec) {
  144. var rejectRegexp = rejectRe;
  145. } else {
  146. var rejectRegexp = new RegExp();
  147. rejectRegexp.compile(rejectRe, "i");
  148. }
  149. }
  150.  
  151. if(!inHere.length) {
  152. inHere = new Array(inHere);
  153. }
  154.  
  155. for(var j=0; j<inHere.length; j++) {
  156. var coverView = doc.evaluate('//div[@class="thumbotron"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();//Detect Cover view
  157. if(coverView){
  158. var links = inHere[j].getElementsByTagName("a");
  159. for(var i=0; i<links.length; i++) {
  160. if(!urlRe || urlRegexp.test(links[i].href)) {
  161. var text = links[i].textContent;
  162. if(!text) {
  163. var text = links[i].firstChild.alt;
  164. }
  165. if(text) {
  166. text = Zotero.Utilities.trimInternal(text);
  167. if(!rejectRe || !rejectRegexp.test(text)) {
  168. if(availableItems[links[i].href]) {
  169. if(text != availableItems[links[i].href]) {
  170. availableItems[links[i].href] += " "+text;
  171. }
  172. } else {
  173. availableItems[links[i].href] = text;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. else {
  181. var links = inHere[j].getElementsByTagName("img");//search for <img>-elements, scrape title from alt-attribute, href-link from parent <a>-element
  182. for(var i=0; i<links.length; i++) {
  183. if(!urlRe || urlRegexp.test(links[i].parentNode.href)) {
  184. var text = links[i].alt;
  185. if(text) {
  186. text = Zotero.Utilities.trimInternal(text);
  187. if(!rejectRe || !rejectRegexp.test(text)) {
  188. if(availableItems[links[i].href]) {
  189. if(text != availableItems[links[i].href]) {
  190. availableItems[links[i].href] += " "+text;
  191. }
  192. } else {
  193. availableItems[links[i].parentNode.href] = text;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201.  
  202. return availableItems;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment