Advertisement
Guest User

Google Books Wiktionary Citation

a guest
Jan 7th, 2022
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Google Books Wiktionary Citation
  3. // @version  1
  4. // @include https://www.google.com/search*&tbm=bks&*
  5. // @grant GM.xmlHttpRequest
  6. // ==/UserScript==
  7.  
  8. function addCiteLinksBooks() {
  9.     var results = document.getElementsByClassName("Yr5TG");
  10.  
  11.     for (var i = 0; i < results.length; i++) {
  12.         var line = results[i].getElementsByClassName("N96wpd")[0];
  13.         var button = document.createElement('button');
  14.         var buttonText = document.createTextNode("[cite]");
  15.         button.appendChild(buttonText);
  16.         button.onclick = displayCitationBooks;
  17.         line.appendChild(button);
  18.     }
  19. }
  20.  
  21. function displayCitationBooks() {
  22.     var result = this.closest(".Yr5TG");
  23.  
  24.     var target = result.getElementsByTagName("a")[0].href.split("&hl=")[0];
  25.     var id = /\?id=(.*?)&/.exec(target)[1];
  26.  
  27.     var snippet = result.getElementsByClassName("ETWPw");
  28.     var page = null;
  29.     if (snippet.length) {
  30.         snippet = snippet[0].getElementsByTagName("span")[0].textContent;
  31.         page = result.getElementsByClassName("VNSPub")[0].textContent;
  32.         page = /Page (\d+)/.exec(page);
  33.         if (page) page = page[1];
  34.     } else snippet = false;
  35.  
  36.     var request = GM.xmlHttpRequest({
  37.         method: "GET",
  38.         url: "https://www.googleapis.com/books/v1/volumes/" + id,
  39.  
  40.         onload: function(response) {
  41.             volumeInfo = JSON.parse(response.responseText)["volumeInfo"];
  42.          
  43.             var title = volumeInfo["title"] && (volumeInfo["title"] + (volumeInfo["subtitle"] && (": " + volumeInfo["subtitle"]) || "")).replace("|", "&#124;");
  44.          
  45.             var publisher = volumeInfo["publisher"] && volumeInfo["publisher"].replace("|", "&#124;");
  46.          
  47.             var date = volumeInfo["publishedDate"];
  48.             var dateType = date && date.includes("-") && "date" || "year";
  49.  
  50.             var lang = volumeInfo["language"];
  51.  
  52.             var isbn = volumeInfo["industryIdentifiers"];
  53.             if (isbn) {
  54.                 isbn = isbn.find(function(x) {
  55.                     return x.type == "ISBN_13"
  56.                 }) || isbn.find(function(x) {
  57.                     return x.type == "ISBN_10"
  58.                 });
  59.                 isbn = isbn["identifier"];
  60.             }
  61.  
  62.             var author = volumeInfo["authors"] && volumeInfo["authors"].join(", ").replace("|", "&#124;");
  63.  
  64.             var citation = "{{quote-book|" +
  65.                 lang +
  66.                 (target && ("\n|url=" + target) || "") +
  67.                 (title && ("\n|title=" + title) || "") +
  68.                 (author && ("\n|author=" + author) || "") +
  69.                 (publisher && ("\n|publisher=" + publisher) || "") +
  70.                 (isbn && ("\n|isbn=" + isbn) || "") +
  71.                 (date && ("\n|" + dateType + "=" + date) || "") +
  72.                 (page && ("\n|page=" + page) || "") +
  73.                 (snippet && ("\n|passage=" + snippet) || "") +
  74.                 (snippet && (lang != "en") && ("\n|t=") || "") +
  75.                 "\n}}";
  76.  
  77.             var textarea = document.createElement("textarea");
  78.             var button = document.createElement('button');
  79.             var buttonText = document.createTextNode("[minify]");
  80.             button.appendChild(buttonText);
  81.             button.onclick = minify;
  82.             textarea.setAttribute('rows', 10);
  83.             textarea.setAttribute('cols', 80);
  84.             textarea.value = citation;
  85.  
  86.             var line = result.getElementsByClassName("N96wpd")[0];
  87.             line.insertBefore(button, this.nextSibling);
  88.             line.insertBefore(textarea, button);
  89.         }
  90.     });
  91. }
  92.  
  93. function minify() {
  94.     var textarea = this.parentNode.getElementsByTagName('textarea')[0];
  95.     textarea.value = textarea.value.replace(/\n/g, "");
  96. }
  97.  
  98. addCiteLinksBooks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement