Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Google Books Wiktionary Citation
- // @version 1
- // @include https://www.google.com/search*&tbm=bks&*
- // @include https://www.google.com/search?tbm=bks&*
- // @include https://www.google.com/books?*
- // @include https://www.google.com/books/*
- // @include https://books.google.com/books?*
- // @include https://books.google.com/books/*
- // @require https://cdn.jsdelivr.net/npm/citation-js@0.5
- // @grant GM.xmlHttpRequest
- // @grant GM_xmlhttpRequest
- // ==/UserScript==
- var Cite = require('citation-js');
- var mode = "TM";
- if (typeof GM_xmlhttpRequest == "undefined") {
- mode = "GM";
- GM_xmlhttpRequest = GM.xmlHttpRequest;
- }
- var viewType = "unknown";
- function getURLFromSubframe() {
- try {
- mouseEvent(document.getElementById(":a"), "mousedown");
- mouseEvent(document.getElementById(":b"), "mousedown");
- mouseEvent(document.getElementById(":b"), "click");
- mouseEvent(document.getElementById(":b"), "mouseup");
- return document.querySelector('.popup-menu[style*="visible"] .linkto-popup-content input').value;
- } catch (error) {
- var id = /[?&]id=(.*?)&|\/books\/edition\/.*?\/([^?]+)/.exec(document.location);
- id = id && (id[2] || id[1]);
- return id && ("https://www.google.com/books?id=" + id) || document.querySelector('link[rel=canonical]').href;
- }
- }
- function getSnippetFromSubframe() {
- mouseEvent(document.getElementById(":a"), "mousedown");
- mouseEvent(document.getElementById(":b"), "mousedown");
- mouseEvent(document.getElementById(":b"), "click");
- mouseEvent(document.getElementById(":b"), "mouseup");
- return document.querySelector('.popup-menu[style*="visible"] .linkto-popup-content input').value;
- }
- function getURLFromOldView() {
- var linkbar = document.getElementsByClassName("linkbar-panel-div");
- if (linkbar.length) return linkbar[0].getElementsByTagName("input")[0].value;
- var linkimg = document.querySelector("img[src*='insert_link.png']");
- mouseEvent(linkimg.parentNode, "mousedown");
- mouseEvent(linkimg.parentNode, "click");
- mouseEvent(linkimg.parentNode, "mouseup");
- linkbar = document.getElementsByClassName("linkbar-panel-div");
- return linkbar[0].getElementsByTagName("input")[0].value;
- }
- function getSnippet(page) {
- var snippets = document.querySelectorAll('body > div[style*="position: absolute; display: none; background-color"]');
- snippets = Array.from(snippets);
- if (!snippets.length) return;
- var snippet = page && snippets.find(function(x) {
- return x.children[0].textContent.match(RegExp("\\b" + page + "\\b"));
- });
- if (page && snippet) return snippet.children[1].textContent;
- snippet = snippets.map(function(x) {
- return x.children && x.children.length > 1 && ("[" + x.children[0].textContent + "] " + x.children[1].textContent) || "";
- }).join(" \n/// ");
- if (snippet.length < 2000) return snippet;
- }
- function addCiteLinksBooks() {
- var results = document.getElementsByClassName("Yr5TG");
- var button, buttonText;
- var tag = "button";
- for (var i = 0; i < results.length; i++) {
- viewType = "results";
- var line = results[i].getElementsByClassName("N96wpd")[0];
- if (!line.getElementsByClassName('wikt-cite-button').length) {
- button = document.createElement('button');
- button.classList.add('wikt-cite-button');
- buttonText = document.createTextNode("[cite]");
- button.appendChild(buttonText);
- button.onclick = displayCitationBooks;
- line.appendChild(button);
- }
- }
- var infobar = document.getElementsByClassName("gb-entity-page-toolbar-container");
- if (infobar.length) {
- viewType = "subframe";
- infobar = infobar[0];
- } else if (document.getElementById("volume-info-sidebar")) {
- viewType = "old";
- infobar = document.getElementById("volume-info-sidebar");
- } else {
- infobar = document.getElementById("bookinfo") || document.getElementById("bep-tab-selector");
- }
- if (infobar && !infobar.getElementsByClassName('wikt-cite-button').length) {
- var div = document.createElement('div');
- button = document.createElement(tag);
- button.classList.add('wikt-cite-button');
- buttonText = document.createTextNode("[cite]");
- button.appendChild(buttonText);
- button.onclick = displayCitationBooks;
- div.append(button);
- infobar.append(div);
- }
- var iframe = document.querySelector('iframe[src*="books.google.com/books"]');
- if (mode == "GM" && iframe) {
- var embed = document.createElement('embed');
- var attributes = iframe.attributes;
- for (i = 0; i < attributes.length; i++) {
- embed.setAttribute(attributes[i].nodeName, attributes[i].nodeValue);
- }
- iframe.replaceWith(embed);
- }
- }
- function displayCitationBooks() {
- var page = null;
- var result = this.closest(".Yr5TG");
- var that = this;
- var target = document.location;
- if (viewType == "results") target = result.getElementsByTagName("a")[0].href.split("&hl=")[0];
- else if (viewType == "subframe") target = getURLFromSubframe();
- else if (viewType == "old") target = getURLFromOldView();
- var id = /[?&]id=(.*?)(?:&|$)|\/books\/edition\/.*?\/([^?]+)/.exec(target);
- id = id && (id[2] || id[1]);
- page = /[?&]pg=PA(.*?)\b/.exec(target);
- page = page && page[1];
- var snippet;
- if (viewType == "results") {
- snippet = result.getElementsByClassName("ETWPw");
- snippet = snippet.length && snippet[0].getElementsByTagName("span")[0].textContent || null;
- page = result.getElementsByClassName("VNSPub");
- page = page.length && page[0].textContent || "";
- page = /Page (\d+)/.exec(page);
- if (page) page = page[1];
- } else snippet = getSnippet(page);
- var oclc = document.querySelector('a[href*="worldcat.org/oclc/"]');
- oclc = oclc && /worldcat\.org\/oclc\/([0-9]+)/.exec(oclc)[1];
- var volumeInfo = null,
- cite = null,
- lccn = null;
- var request = GM_xmlhttpRequest({
- method: "GET",
- url: "https://www.googleapis.com/books/v1/volumes/" + id,
- onload: function(response) {
- volumeInfo = JSON.parse(response.responseText).volumeInfo;
- request = GM_xmlhttpRequest({
- method: "GET",
- url: "https://books.google.com/books?id=" + id + "&output=ris",
- onload: function(response) {
- cite = new Cite(response.responseText).data[0];
- request = GM_xmlhttpRequest({
- method: "GET",
- url: "https://books.google.com/books?id=" + id + "&output=bibtex",
- onload: function(response) {
- lccn = /\blccn={(.*?)}/.exec(response.responseText);
- lccn = lccn && lccn[1];
- if (oclc || (viewType != "results" && viewType != "subframe")) displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc);
- else {
- request = GM_xmlhttpRequest({
- method: "GET",
- url: "https://www.google.com/books/?id=" + id,
- onload: function(response) {
- var el = document.createElement('html');
- el.innerHTML = response.responseText;
- oclc = el.querySelector('a[href*="worldcat.org/oclc/"]');
- oclc = oclc && /worldcat\.org\/oclc\/([0-9]+)/.exec(oclc)[1];
- displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc);
- }
- });
- }
- }
- });
- }
- });
- }
- });
- }
- function displayCitationsBooks2(that, result, page, target, id, snippet, volumeInfo, cite, lccn, oclc) {
- var title = volumeInfo.title && (volumeInfo.title + (volumeInfo.subtitle && (": " + volumeInfo.subtitle) || "")).replace("|", "|");
- var publisher = volumeInfo.publisher && volumeInfo.publisher.replace("|", "|");
- var date = volumeInfo.publishedDate;
- var dateType = date && date.includes("-") && "date" || "year";
- var lang = volumeInfo.language;
- var volume = /\bv\. (.*?)(?:,|$)/.exec(cite.issue || "");
- volume = volume && volume[1];
- var issue = /\bnos?\. (.*?)(?:,|$)/.exec(cite.issue || "");
- issue = issue && issue[1];
- var part = /\bpts?\. (.*?)(?:,|$)/.exec(cite.issue || "");
- part = part && part[1];
- var series = cite["collection-title"];
- var isbn, issn;
- var identifiers = volumeInfo.industryIdentifiers;
- if (identifiers) {
- isbn = identifiers.find(function(x) {
- return x.type == "ISBN_13"
- }) || identifiers.find(function(x) {
- return x.type == "ISBN_10"
- });
- issn = identifiers.find(function(x) {
- return x.type == "ISSN"
- });
- if (isbn) isbn = isbn.identifier;
- if (issn) issn = issn.identifier;
- }
- var author = volumeInfo.authors && volumeInfo.authors.join(", ").replace("|", "|");
- var citation = "{{quote-book|" +
- lang +
- (target && ("\n|url=" + target) || "") +
- (title && ("\n|title=" + title) || "") +
- (author && ("\n|author=" + author) || "") +
- (publisher && ("\n|publisher=" + publisher) || "") +
- (series && ("\n|series=" + series) || "") +
- (isbn && ("\n|isbn=" + isbn) || "") +
- (issn && ("\n|issn=" + issn) || "") +
- (lccn && ("\n|lccn=" + lccn) || "") +
- (oclc && ("\n|oclc=" + oclc) || "") +
- (date && ("\n|" + dateType + "=" + date) || "") +
- (volume && ("\n|volume=" + volume) || "") +
- (issue && ("\n|issue=" + issue) || "") +
- (part && ("\n|other=part " + part) || "") +
- (page && ("\n|page=" + page) || "") +
- (snippet && ("\n|passage=" + snippet) || "") +
- (snippet && (lang != "en") && ("\n|t=") || "") +
- "\n}}";
- var textarea = document.createElement("textarea");
- var button = document.createElement('button');
- var buttonText = document.createTextNode("[minify]");
- button.appendChild(buttonText);
- button.onclick = minify;
- textarea.setAttribute('rows', 10);
- textarea.setAttribute('cols', (viewType == "results") && 80 || (viewType == "old") && 25 || (viewType == "subframe") && 200);
- textarea.value = citation;
- if (viewType == "results") {
- var line = result.getElementsByClassName("N96wpd")[0];
- line.insertBefore(button, this.nextSibling);
- line.insertBefore(textarea, button);
- } else {
- that.parentNode.append(textarea);
- that.parentNode.append(button);
- }
- }
- function minify() {
- var textarea = this.parentNode.getElementsByTagName('textarea')[0];
- textarea.value = textarea.value.replace(/\n/g, "");
- }
- function mouseEvent(element, eventType) {
- var event = document.createEvent("MouseEvents");
- event.initEvent(eventType, true, true);
- element.dispatchEvent(event);
- }
- addCiteLinksBooks();
- var observer = new MutationObserver(addCiteLinksBooks);
- observer.observe(document.body, {
- childList: true,
- subtree: true,
- attributes: false,
- characterData: false
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement