Advertisement
Guest User

Untitled

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