Advertisement
Guest User

Untitled

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