Advertisement
Guest User

Untitled

a guest
Jan 9th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     Google Scholar Wiktionary Citation
  3. // @version  1
  4. // @grant    GM.xmlHttpRequest
  5. // @require https://cdn.jsdelivr.net/npm/citation-js@0.5
  6. // @include https://scholar.google.com/scholar?*
  7. // ==/UserScript==
  8. var Cite = require('citation-js');
  9.  
  10. function displayCitationScholar(id, data) {
  11.     var target = document.getElementById(id).href;
  12.  
  13.     var popup = document.getElementById("gs_citd");
  14.     var tool = document.getElementsByClassName('wikt-citation-tool');
  15.     if (tool.length)
  16.         tool[0].remove();
  17.  
  18.     var div = document.createElement('div');
  19.     div.classList.add('wikt-citation-tool');
  20.     popup.insertBefore(div, popup.firstChild);
  21.  
  22.     var subdiv1 = document.createElement('div');
  23.  
  24.     var textarea = document.createElement('textarea');
  25.     textarea.setAttribute('rows', 10);
  26.     textarea.setAttribute('cols', 50);
  27.  
  28.     var button = document.createElement('button');
  29.     var buttonText = document.createTextNode("[minify]");
  30.     button.appendChild(buttonText);
  31.     button.onclick = minify;
  32.     var crbutton = document.createElement('button');
  33.     var crbuttonText = document.createTextNode("[check crossref; will overwrite]");
  34.     crbutton.appendChild(crbuttonText);
  35.  
  36.     subdiv1.append(textarea);
  37.     subdiv1.append(button);
  38.     subdiv1.style.display = 'none';
  39.     var subdiv2 = document.createElement('div');
  40.     subdiv2.style.display = 'none';
  41.     div.append(subdiv1);
  42.     div.append(subdiv2);
  43.  
  44.     var title = data.title,
  45.         containerTitle = data["container-title"];
  46.     var volume = data.volume,
  47.         issue = data.issue;
  48.     var publisher = data.publisher;
  49.  
  50.     var isbn = data.ISBN,
  51.         issn = data.ISSN,
  52.         doi = data.DOI,
  53.         pmid = data.PMID;
  54.     var language = data.language;
  55.     var page = data.page;
  56.     var pageType = page && (page.includes("-") ? "pages" : "page");
  57.     var result = document.getElementById(id).closest('.gs_or');
  58.     var snippet = result && result.getElementsByClassName('gs_rs');
  59.     snippet = snippet.length && snippet[0].textContent.replace(/\s*\n\s*/g, " ");
  60.  
  61.     var date = data.issued,
  62.         dateType = "date";
  63.  
  64.     if (date && date["date-parts"]) {
  65.         var parts = date["date-parts"][date["date-parts"].length - 1];
  66.         var year = parts[0],
  67.             month = parts[1],
  68.             day = parts[2];
  69.  
  70.         if (month === undefined) date = "" + year, dateType = "year";
  71.         else if (day === undefined) date = "" + year + "-" + ("0" + month).slice(-2);
  72.         else date = "" + year + "-" + ("0" + month).slice(-2) + "-" + ("0" + day).slice(-2);
  73.     }
  74.  
  75.     var journalTypes = ['article-journal', 'article-magazine', 'article-newspaper'];
  76.     var template = "quote-book",
  77.         container = "title",
  78.         entry = "chapter";
  79.     if (!containerTitle) entry = "title";
  80.  
  81.     var authorialFields = ["original-author", "author", "composer", "contributor", "director", "guest", "host", "illustrator", "interviewer", "narrator", "performer", "script-writer", "series-creator"];
  82.     var editorialFields = ["container-author", "editor", "compiler", "chair", "collection-editor", "curator", "editorial-director", "editor-translator", "producer", "executive-producer", "organizer", "translator"];
  83.  
  84.     var f = function(x) {
  85.         return x.literal || ((x.given || "") + " " + (x.family || "")).replace(/^\s+|\s+$/g, '');
  86.     };
  87.  
  88.     var author = [];
  89.     for (var i = 0; i < authorialFields.length; i++) {
  90.         var field = authorialFields[i];
  91.         if (field in data) {
  92.             author = author.concat(data[field].map(f));
  93.         }
  94.     }
  95.  
  96.     var editor = [];
  97.     for (var i = 0; i < editorialFields.length; i++) {
  98.         var field = editorialFields[i];
  99.         if (field in data) {
  100.             editor = editor.concat(data[field].map(f));
  101.         }
  102.     }
  103.  
  104.     author = author.join(", "), editor = editor.join(", ");
  105.  
  106.     if (journalTypes.includes(data.type)) {
  107.         template = "quote-journal", container = "journal", entry = "title";
  108.     }
  109.  
  110.     var displayCitation = function(event) {
  111.         subdiv1.style.display = 'block';
  112.         subdiv2.style.display = 'none';
  113.         var citation = "{{" + template + "|" + (language || "languagecode") +
  114.             (target && ("\n|url=" + target) || "") +
  115.             (title && ("\n|" + entry + "=" + title) || "") +
  116.             (containerTitle && ("\n|" + container + "=" + containerTitle) || "") +
  117.             (author && ("\n|author=" + author) || "") +
  118.             (editor && ("\n|editor=" + editor) || "") +
  119.             (publisher && ("\n|publisher=" + publisher) || "") +
  120.             (isbn && ("\n|isbn=" + isbn) || "") +
  121.             (issn && ("\n|issn=" + issn) || "") +
  122.             (doi && ("\n|doi=" + doi) || "") +
  123.             (pmid && ("\n|pmid=" + pmid) || "") +
  124.             (date && ("\n|" + dateType + "=" + date) || "") +
  125.             (volume && ("\n|volume=" + volume) || "") +
  126.             (issue && ("\n|issue=" + issue) || "") +
  127.             (page && ("\n|" + pageType + "=" + page) || "") +
  128.             (snippet && ("\n|passage=" + snippet) || "") +
  129.             (snippet && language && (language != "en") && "\n|t=" || "") +
  130.             "\n}}";
  131.         textarea.value = citation;
  132.     };
  133.  
  134.     displayCitation();
  135.  
  136.     var crossrefCheck = function() {
  137.         GM.xmlHttpRequest({
  138.             method: "GET",
  139.             url: "https://api.crossref.org/works?rows=2&query.bibliographic=" + encodeURIComponent(document.getElementsByClassName('gs_citr')[0].textContent),
  140.             onload: function(response) {
  141.                 crbutton.style.display = 'none';
  142.                 var results = JSON.parse(response.responseText);
  143.                 var items = results.message.items;
  144.                 if (items.length) {
  145.                     subdiv1.style.display = 'none';
  146.                     subdiv2.style.display = 'block';
  147.                     var p = document.createElement('p');
  148.                     p.innerText = "Check carefully. If any of these is the *exact same* publication/work, select it (there may be duplicates; do *not* select reviews):";
  149.                     subdiv2.append(p);
  150.  
  151.                     for (var i = 0; i < items.length; i++) {
  152.                         var c = new Cite(items[i]);
  153.                         var itemdiv = document.createElement('div');
  154.                         var input = document.createElement('input');
  155.                         input.setAttribute('name', 'crossref');
  156.                         input.setAttribute('type', 'radio');
  157.                         input.setAttribute('value', 'crossref' + i);
  158.                         input.setAttribute('id', 'crossref' + i);
  159.                         var label = document.createElement('label');
  160.                         label.setAttribute('for', 'crossref' + i);
  161.                         label.innerText = c.get({
  162.                             'format': 'string',
  163.                             'lang': 'en',
  164.                             'style': 'citation-apa',
  165.                             'type': 'string'
  166.                         });
  167.                         itemdiv.append(input);
  168.                         itemdiv.append(label);
  169.                         subdiv2.append(itemdiv);
  170.                     }
  171.  
  172.                     var button1 = document.createElement('button');
  173.                     var button2 = document.createElement('button');
  174.                     button1.innerText = 'select';
  175.                     button2.innerText = 'none match';
  176.                     subdiv2.append(button1);
  177.                     subdiv2.append(button2);
  178.                     subdiv2.append(document.createElement('hr'));
  179.                     button1.onclick = function() {
  180.                         var selected = +document.querySelector('input[name="crossref"]:checked').value.slice(-1);
  181.                         pmid = items[selected].PMID;
  182.                         doi = items[selected].DOI;
  183.                         language = items[selected].language;
  184.                         displayCitation();
  185.                     }
  186.                     button2.onclick = displayCitation;
  187.                 } else displayCitation();
  188.             }
  189.         });
  190.     };
  191.     crbutton.onclick = crossrefCheck;
  192.     subdiv1.append(crbutton);
  193.     subdiv1.append(document.createElement('hr'));
  194. }
  195.  
  196. var observer = new MutationObserver(function(mutations) {
  197.     mutations.forEach(function(mutation) {
  198.         if (!mutation.addedNodes) return;
  199.  
  200.         for (var i = 0; i < mutation.addedNodes.length; i++) {
  201.             var node = mutation.addedNodes[i];
  202.  
  203.             if (node.id == 'gs_citi') {
  204.                 for (var j = 0; j < node.childNodes.length; j++) {
  205.                     var subnode = node.childNodes[j];
  206.  
  207.                     if (subnode.textContent == "RefMan") {
  208.                         var url = subnode.href;
  209.                         var id = /q=info:(.*?):/.exec(url)[1];
  210.  
  211.                         GM.xmlHttpRequest({
  212.                             method: "GET",
  213.                             url: url,
  214.                             onload: function(response) {
  215.                                 var cite = new Cite(response.responseText);
  216.                                 displayCitationScholar(id, cite.data[0]);
  217.                             }
  218.                         });
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.     });
  224. })
  225.  
  226. observer.observe(document.body, {
  227.     childList: true,
  228.     subtree: true,
  229.     attributes: false,
  230.     characterData: false
  231. });
  232.  
  233. function minify() {
  234.     var textarea = this.parentNode.getElementsByTagName('textarea')[0];
  235.     textarea.value = textarea.value.replace(/\n/g, "");
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement