Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Google Scholar Wiktionary Citation
- // @version 1
- // @grant GM_xmlhttpRequest
- // @require https://cdn.jsdelivr.net/npm/citation-js@0.5
- // @include https://scholar.google.com/scholar?*
- // ==/UserScript==
- var Cite = require('citation-js');
- function displayCitationScholar(id, data) {
- var target = document.getElementById(id).href;
- var popup = document.getElementById("gs_citd");
- var tool = document.getElementsByClassName('wikt-citation-tool');
- if (tool.length)
- tool[0].remove();
- var div = document.createElement('div');
- div.classList.add('wikt-citation-tool');
- popup.insertBefore(div, popup.firstChild);
- var subdiv1 = document.createElement('div');
- var textarea = document.createElement('textarea');
- textarea.setAttribute('rows', 10);
- textarea.setAttribute('cols', 50);
- var button = document.createElement('button');
- var buttonText = document.createTextNode("[minify]");
- button.appendChild(buttonText);
- button.onclick = minify;
- var crbutton = document.createElement('button');
- var crbuttonText = document.createTextNode("[check crossref; will overwrite]");
- crbutton.appendChild(crbuttonText);
- subdiv1.append(textarea);
- subdiv1.append(button);
- subdiv1.style.display = 'none';
- var subdiv2 = document.createElement('div');
- subdiv2.style.display = 'none';
- div.append(subdiv1);
- div.append(subdiv2);
- var title = data.title,
- containerTitle = data["container-title"];
- var volume = data.volume,
- issue = data.issue;
- var publisher = data.publisher;
- var isbn = data.ISBN,
- issn = data.ISSN,
- doi = data.DOI,
- pmid = data.PMID;
- var language = data.language;
- var page = data.page;
- var pageType = page && (page.includes("-") ? "pages" : "page");
- var result = document.getElementById(id).closest('.gs_or');
- var snippet = result && result.getElementsByClassName('gs_rs');
- snippet = snippet.length && snippet[0].textContent.replace(/\s*\n\s*/g, " ");
- var date = data.issued,
- dateType = "date";
- if (date && date["date-parts"]) {
- var parts = date["date-parts"][date["date-parts"].length - 1];
- var year = parts[0],
- month = parts[1],
- day = parts[2];
- if (month === undefined) date = "" + year, dateType = "year";
- else if (day === undefined) date = "" + year + "-" + ("0" + month).slice(-2);
- else date = "" + year + "-" + ("0" + month).slice(-2) + "-" + ("0" + day).slice(-2);
- }
- var journalTypes = ['article-journal', 'article-magazine', 'article-newspaper'];
- var template = "quote-book",
- container = "title",
- entry = "chapter";
- if (!containerTitle) entry = "title";
- var authorialFields = ["original-author", "author", "composer", "contributor", "director", "guest", "host", "illustrator", "interviewer", "narrator", "performer", "script-writer", "series-creator"];
- var editorialFields = ["container-author", "editor", "compiler", "chair", "collection-editor", "curator", "editorial-director", "editor-translator", "producer", "executive-producer", "organizer", "translator"];
- var f = function(x) {
- return x.literal || ((x.given || "") + " " + (x.family || "")).replace(/^\s+|\s+$/g, '');
- };
- var author = [];
- for (var i = 0; i < authorialFields.length; i++) {
- var field = authorialFields[i];
- if (field in data) {
- author = author.concat(data[field].map(f));
- }
- }
- var editor = [];
- for (var i = 0; i < editorialFields.length; i++) {
- var field = editorialFields[i];
- if (field in data) {
- editor = editor.concat(data[field].map(f));
- }
- }
- author = author.join(", "), editor = editor.join(", ");
- if (journalTypes.includes(data.type)) {
- template = "quote-journal", container = "journal", entry = "title";
- }
- var displayCitation = function(event) {
- subdiv1.style.display = 'block';
- subdiv2.style.display = 'none';
- var citation = "{{" + template + "|" + (language || "languagecode") +
- (target && ("\n|url=" + target) || "") +
- (title && ("\n|" + entry + "=" + title) || "") +
- (containerTitle && ("\n|" + container + "=" + containerTitle) || "") +
- (author && ("\n|author=" + author) || "") +
- (editor && ("\n|editor=" + editor) || "") +
- (publisher && ("\n|publisher=" + publisher) || "") +
- (isbn && ("\n|isbn=" + isbn) || "") +
- (issn && ("\n|issn=" + issn) || "") +
- (doi && ("\n|doi=" + doi) || "") +
- (pmid && ("\n|pmid=" + pmid) || "") +
- (date && ("\n|" + dateType + "=" + date) || "") +
- (volume && ("\n|volume=" + volume) || "") +
- (issue && ("\n|issue=" + issue) || "") +
- (page && ("\n|" + pageType + "=" + page) || "") +
- (snippet && ("\n|passage=" + snippet) || "") +
- (snippet && language && (language != "en") && "\n|t=" || "") +
- "\n}}";
- textarea.value = citation;
- };
- displayCitation();
- var crossrefCheck = function() {
- GM_xmlhttpRequest({
- method: "GET",
- url: "https://api.crossref.org/works?rows=2&query.bibliographic=" + encodeURIComponent(document.getElementsByClassName('gs_citr')[0].textContent),
- onload: function(response) {
- crbutton.style.display = 'none';
- var results = JSON.parse(response.responseText);
- var items = results.message.items;
- if (items.length) {
- subdiv1.style.display = 'none';
- subdiv2.style.display = 'block';
- var p = document.createElement('p');
- p.innerText = "Check carefully. If any of these is the *exact same* publication/work, select it (there may be duplicates; do *not* select reviews):";
- subdiv2.append(p);
- for (var i = 0; i < items.length; i++) {
- var c = new Cite(items[i]);
- var itemdiv = document.createElement('div');
- var input = document.createElement('input');
- input.setAttribute('name', 'crossref');
- input.setAttribute('type', 'radio');
- input.setAttribute('value', 'crossref' + i);
- input.setAttribute('id', 'crossref' + i);
- var label = document.createElement('label');
- label.setAttribute('for', 'crossref' + i);
- label.innerText = c.get({
- 'format': 'string',
- 'lang': 'en',
- 'style': 'citation-apa',
- 'type': 'string'
- });
- itemdiv.append(input);
- itemdiv.append(label);
- subdiv2.append(itemdiv);
- }
- var button1 = document.createElement('button');
- var button2 = document.createElement('button');
- button1.innerText = 'select';
- button2.innerText = 'none match';
- subdiv2.append(button1);
- subdiv2.append(button2);
- subdiv2.append(document.createElement('hr'));
- button1.onclick = function() {
- var selected = +document.querySelector('input[name="crossref"]:checked').value.slice(-1);
- pmid = items[selected].PMID;
- doi = items[selected].DOI;
- language = items[selected].language;
- displayCitation();
- }
- button2.onclick = displayCitation;
- } else displayCitation();
- }
- });
- };
- crbutton.onclick = crossrefCheck;
- subdiv1.append(crbutton);
- subdiv1.append(document.createElement('hr'));
- }
- var observer = new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (!mutation.addedNodes) return;
- for (var i = 0; i < mutation.addedNodes.length; i++) {
- var node = mutation.addedNodes[i];
- if (node.id == 'gs_citi') {
- for (var j = 0; j < node.childNodes.length; j++) {
- var subnode = node.childNodes[j];
- if (subnode.textContent == "RefMan") {
- var url = subnode.href;
- var id = /q=info:(.*?):/.exec(url)[1];
- GM_xmlhttpRequest({
- method: "GET",
- url: url,
- onload: function(response) {
- var cite = new Cite(response.responseText);
- displayCitationScholar(id, cite.data[0]);
- }
- });
- }
- }
- }
- }
- });
- })
- observer.observe(document.body, {
- childList: true,
- subtree: true,
- attributes: false,
- characterData: false
- });
- function minify() {
- var textarea = this.parentNode.getElementsByTagName('textarea')[0];
- textarea.value = textarea.value.replace(/\n/g, "");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement