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&*
- // @grant GM.xmlHttpRequest
- // ==/UserScript==
- function addCiteLinksBooks() {
- var results = document.getElementsByClassName("Yr5TG");
- for (var i = 0; i < results.length; i++) {
- var line = results[i].getElementsByClassName("N96wpd")[0];
- var button = document.createElement('button');
- var buttonText = document.createTextNode("[cite]");
- button.appendChild(buttonText);
- button.onclick = displayCitationBooks;
- line.appendChild(button);
- }
- }
- function displayCitationBooks() {
- var result = this.closest(".Yr5TG");
- var target = result.getElementsByTagName("a")[0].href.split("&hl=")[0];
- var id = /\?id=(.*?)&/.exec(target)[1];
- var snippet = result.getElementsByClassName("ETWPw");
- var page = null;
- if (snippet.length) {
- snippet = snippet[0].getElementsByTagName("span")[0].textContent;
- page = result.getElementsByClassName("VNSPub")[0].textContent;
- page = /Page (\d+)/.exec(page);
- if (page) page = page[1];
- } else snippet = false;
- var request = GM.xmlHttpRequest({
- method: "GET",
- url: "https://www.googleapis.com/books/v1/volumes/" + id,
- onload: function(response) {
- volumeInfo = JSON.parse(response.responseText)["volumeInfo"];
- 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 isbn = volumeInfo["industryIdentifiers"];
- if (isbn) {
- isbn = isbn.find(function(x) {
- return x.type == "ISBN_13"
- }) || isbn.find(function(x) {
- return x.type == "ISBN_10"
- });
- isbn = isbn["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) || "") +
- (isbn && ("\n|isbn=" + isbn) || "") +
- (date && ("\n|" + dateType + "=" + date) || "") +
- (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', 80);
- textarea.value = citation;
- var line = result.getElementsByClassName("N96wpd")[0];
- line.insertBefore(button, this.nextSibling);
- line.insertBefore(textarea, button);
- }
- });
- }
- function minify() {
- var textarea = this.parentNode.getElementsByTagName('textarea')[0];
- textarea.value = textarea.value.replace(/\n/g, "");
- }
- addCiteLinksBooks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement