Advertisement
Guest User

Untitled

a guest
Jan 9th, 2022
63
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. // @include https://www.google.com/books?*
  7. // @include https://www.google.com/books/*
  8. // @include https://books.google.com/books?*
  9. // @include https://books.google.com/books/*
  10. // @require https://cdn.jsdelivr.net/npm/citation-js@0.5
  11. // @grant GM.xmlHttpRequest
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14. var Cite = require('citation-js');
  15. var mode = "TM";
  16.  
  17. if (typeof GM_xmlhttpRequest == "undefined") {
  18.     mode = "GM";
  19.     GM_xmlhttpRequest = GM.xmlHttpRequest;
  20. }
  21.  
  22. var viewType = "unknown";
  23.  
  24. function getURLFromSubframe() {
  25.     try {
  26.         var moreMenu = document.querySelector('img[src*="more_vert_black_24dp.png"]');
  27.         moreMenu = moreMenu && moreMenu.parentNode.parentNode;
  28.         mouseEvent(moreMenu, "mousedown");
  29.         var shareButton = document.querySelector('.goog-menu.goog-menu-vertical > div.goog-control:nth-child(1)');
  30.         mouseEvent(shareButton, "mousedown");
  31.         mouseEvent(shareButton, "click");
  32.         mouseEvent(shareButton, "mouseup");
  33.         return document.querySelector('.popup-menu[style*="visible"] .linkto-popup-content input').value;
  34.     } catch (error) {
  35.         var id = /[?&]id=(.*?)(?:&|$)|\/books\/edition\/.*?\/([^?]+)/.exec(document.location);
  36.         id = id && (id[2] || id[1]);
  37.         return id && ("https://www.google.com/books?id=" + id) || document.querySelector('link[rel=canonical]').href;
  38.     }
  39. }
  40.  
  41. function getURLFromOldView() {
  42.     var linkbar = document.getElementsByClassName("linkbar-panel-div");
  43.     if (linkbar.length) return linkbar[0].getElementsByTagName("input")[0].value;
  44.     var linkimg = document.querySelector("img[src*='insert_link.png']");
  45.     mouseEvent(linkimg.parentNode, "mousedown");
  46.     mouseEvent(linkimg.parentNode, "click");
  47.     mouseEvent(linkimg.parentNode, "mouseup");
  48.     linkbar = document.getElementsByClassName("linkbar-panel-div");
  49.     return linkbar[0].getElementsByTagName("input")[0].value;
  50. }
  51.  
  52. function getSnippet(page) {
  53.     var snippets = document.querySelectorAll('body > div[style*="position: absolute; display: none; background-color"]');
  54.     snippets = Array.from(snippets);
  55.     if (!snippets.length) return;
  56.     var snippet = page && snippets.find(function(x) {
  57.         return x.children[0].textContent.match(RegExp("\\b" + page + "\\b"));
  58.     });
  59.     if (page && snippet) return snippet.children[1].textContent;
  60.     snippet = snippets.map(function(x) {
  61.         return x.children && x.children.length > 1 && ("[" + x.children[0].textContent + "] " + x.children[1].textContent) || "";
  62.     }).join(" \n/// ");
  63.     if (snippet.length < 2000) return snippet;
  64. }
  65.  
  66. function addCiteLinksBooks() {
  67.     var results = document.getElementsByClassName("Yr5TG");
  68.     var button, buttonText;
  69.     var tag = "button";
  70.     for (var i = 0; i < results.length; i++) {
  71.         viewType = "results";
  72.         var line = results[i].getElementsByClassName("N96wpd")[0];
  73.         if (!line.getElementsByClassName('wikt-cite-button').length) {
  74.             button = document.createElement('button');
  75.             button.classList.add('wikt-cite-button');
  76.             buttonText = document.createTextNode("[cite]");
  77.             button.appendChild(buttonText);
  78.             button.onclick = displayCitationBooks;
  79.             line.appendChild(button);
  80.         }
  81.     }
  82.     var infobar = document.getElementsByClassName("gb-entity-page-toolbar-container");
  83.     if (infobar.length) {
  84.         viewType = "subframe";
  85.         infobar = infobar[0];
  86.     } else if (document.getElementById("volume-info-sidebar")) {
  87.         viewType = "old";
  88.         infobar = document.getElementById("volume-info-sidebar");
  89.     } else {
  90.         infobar = document.getElementById("bookinfo") || document.getElementById("bep-tab-selector");
  91.     }
  92.     if (infobar && !infobar.getElementsByClassName('wikt-cite-button').length) {
  93.         var div = document.createElement('div');
  94.         button = document.createElement(tag);
  95.         button.classList.add('wikt-cite-button');
  96.         buttonText = document.createTextNode("[cite]");
  97.         button.appendChild(buttonText);
  98.         button.onclick = displayCitationBooks;
  99.         div.append(button);
  100.         infobar.append(div);
  101.     }
  102.  
  103.     var iframe = document.querySelector('iframe[src*="books.google.com/books"]');
  104.     if (mode == "GM" && iframe) {
  105.         var embed = document.createElement('embed');
  106.         var attributes = iframe.attributes;
  107.         for (i = 0; i < attributes.length; i++) {
  108.             embed.setAttribute(attributes[i].nodeName, attributes[i].nodeValue);
  109.         }
  110.         iframe.replaceWith(embed);
  111.     }
  112. }
  113.  
  114. function displayCitationBooks() {
  115.     var page = null;
  116.  
  117.     var result = this.closest(".Yr5TG");
  118.     var that = this;
  119.  
  120.     var target = document.location;
  121.     if (viewType == "results") target = result.getElementsByTagName("a")[0].href.split("&hl=")[0];
  122.     else if (viewType == "subframe") target = getURLFromSubframe();
  123.     else if (viewType == "old") target = getURLFromOldView();
  124.  
  125.     var id = /[?&]id=(.*?)(?:&|$)|\/books\/edition\/.*?\/([^?]+)/.exec(target);
  126.     id = id && (id[2] || id[1]);
  127.  
  128.     page = /[?&]pg=PA(.*?)\b/.exec(target);
  129.     page = page && page[1];
  130.  
  131.     var snippet;
  132.     if (viewType == "results") {
  133.         snippet = result.getElementsByClassName("ETWPw");
  134.         snippet = snippet.length && snippet[0].getElementsByTagName("span")[0].textContent || null;
  135.         page = result.getElementsByClassName("VNSPub");
  136.         page = page.length && page[0].textContent || "";
  137.         page = /(\d+)/.exec(page);
  138.         if (page) page = page[1];
  139.     } else snippet = getSnippet(page);
  140.  
  141.     var oclc = document.querySelector('a[href*="worldcat.org/oclc/"]');
  142.     oclc = oclc && /worldcat\.org\/oclc\/([0-9]+)/.exec(oclc)[1];
  143.  
  144.     var volumeInfo = null,
  145.         cite = null,
  146.         lccn = null;
  147.  
  148.     var request = GM_xmlhttpRequest({
  149.         method: "GET",
  150.         url: "https://www.googleapis.com/books/v1/volumes/" + id,
  151.         onload: function(response) {
  152.             volumeInfo = JSON.parse(response.responseText).volumeInfo;
  153.             request = GM_xmlhttpRequest({
  154.                 method: "GET",
  155.                 url: "https://books.google.com/books?id=" + id + "&output=ris",
  156.                 onload: function(response) {
  157.                     cite = new Cite(response.responseText).data[0];
  158.                     request = GM_xmlhttpRequest({
  159.                         method: "GET",
  160.                         url: "https://books.google.com/books?id=" + id + "&output=bibtex",
  161.                         onload: function(response) {
  162.                             lccn = /\blccn={(.*?)}/.exec(response.responseText);
  163.                             lccn = lccn && lccn[1];
  164.                             if (oclc || (viewType != "results" && viewType != "subframe")) displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc);
  165.                             else {
  166.                                 request = GM_xmlhttpRequest({
  167.                                     method: "GET",
  168.                                     url: "https://www.google.com/books/?id=" + id,
  169.                                     onload: function(response) {
  170.                                         var el = document.createElement('html');
  171.                                         el.innerHTML = response.responseText;
  172.                                         oclc = el.querySelector('a[href*="worldcat.org/oclc/"]');
  173.                                         oclc = oclc && /worldcat\.org\/oclc\/([0-9]+)/.exec(oclc)[1];
  174.                                         displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc);
  175.                                     }
  176.                                 });
  177.                             }
  178.                         }
  179.                     });
  180.                 }
  181.             });
  182.         }
  183.     });
  184. }
  185.  
  186. function displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc) {
  187.     var title = volumeInfo.title && (volumeInfo.title + (volumeInfo.subtitle && (": " + volumeInfo.subtitle) || "")).replace("|", "&#124;");
  188.  
  189.     var publisher = volumeInfo.publisher && volumeInfo.publisher.replace("|", "&#124;");
  190.  
  191.     var date = volumeInfo.publishedDate;
  192.     var dateType = date && date.includes("-") && "date" || "year";
  193.  
  194.     var lang = volumeInfo.language;
  195.  
  196.     var volume = /\bv\. (.*?)(?:,|$)/.exec(cite.issue || "");
  197.     volume = volume && volume[1];
  198.     var issue = /\bnos?\. (.*?)(?:,|$)/.exec(cite.issue || "");
  199.     issue = issue && issue[1];
  200.     var part = /\bpts?\. (.*?)(?:,|$)/.exec(cite.issue || "");
  201.     part = part && part[1];
  202.     var series = cite["collection-title"];
  203.     if (series == title) series = null;
  204.  
  205.     var isbn, issn;
  206.     var identifiers = volumeInfo.industryIdentifiers;
  207.     if (identifiers) {
  208.         isbn = identifiers.find(function(x) {
  209.             return x.type == "ISBN_13"
  210.         }) || identifiers.find(function(x) {
  211.             return x.type == "ISBN_10"
  212.         });
  213.         issn = identifiers.find(function(x) {
  214.             return x.type == "ISSN"
  215.         });
  216.         if (isbn) isbn = isbn.identifier;
  217.         if (issn) issn = issn.identifier;
  218.     }
  219.  
  220.     var author = volumeInfo.authors && volumeInfo.authors.join(", ").replace("|", "&#124;");
  221.  
  222.     var citation = "{{quote-book|" +
  223.         lang +
  224.         (target && ("\n|url=" + target) || "") +
  225.         (title && ("\n|title=" + title) || "") +
  226.         (author && ("\n|author=" + author) || "") +
  227.         (publisher && ("\n|publisher=" + publisher) || "") +
  228.         (series && ("\n|series=" + series) || "") +
  229.         (isbn && ("\n|isbn=" + isbn) || "") +
  230.         (issn && ("\n|issn=" + issn) || "") +
  231.         (lccn && ("\n|lccn=" + lccn) || "") +
  232.         (oclc && ("\n|oclc=" + oclc) || "") +
  233.         (date && ("\n|" + dateType + "=" + date) || "") +
  234.         (volume && ("\n|volume=" + volume) || "") +
  235.         (issue && ("\n|issue=" + issue) || "") +
  236.         (part && ("\n|other=part " + part) || "") +
  237.         (page && ("\n|page=" + page) || "") +
  238.         (snippet && ("\n|passage=" + snippet) || "") +
  239.         (snippet && (lang != "en") && ("\n|t=") || "") +
  240.         "\n}}";
  241.  
  242.     var textarea = document.createElement("textarea");
  243.     var button = document.createElement('button');
  244.     var buttonText = document.createTextNode("[minify]");
  245.     button.appendChild(buttonText);
  246.     button.onclick = minify;
  247.     textarea.setAttribute('rows', 10);
  248.     textarea.setAttribute('cols', (viewType == "results") && 80 || (viewType == "old") && 25 || (viewType == "subframe") && 200);
  249.     textarea.value = citation;
  250.  
  251.     if (viewType == "results") {
  252.         var line = result.getElementsByClassName("N96wpd")[0];
  253.         line.insertBefore(button, this.nextSibling);
  254.         line.insertBefore(textarea, button);
  255.     } else {
  256.         that.parentNode.append(textarea);
  257.         that.parentNode.append(button);
  258.     }
  259. }
  260.  
  261. function minify() {
  262.     var textarea = this.parentNode.getElementsByTagName('textarea')[0];
  263.     textarea.value = textarea.value.replace(/\n/g, "");
  264. }
  265.  
  266. function mouseEvent(element, eventType) {
  267.     var event = document.createEvent("MouseEvents");
  268.     event.initEvent(eventType, true, true);
  269.     element.dispatchEvent(event);
  270. }
  271.  
  272. addCiteLinksBooks();
  273.  
  274. var observer = new MutationObserver(addCiteLinksBooks);
  275.  
  276. observer.observe(document.body, {
  277.     childList: true,
  278.     subtree: true,
  279.     attributes: false,
  280.     characterData: false
  281. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement