Google Scholar Oneclick Copy Bib

One click to copy the bib information of each google scholar entry. The bib information will be automatically copied into the clipboard.

当前为 2021-12-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google Scholar Oneclick Copy Bib
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  One click to copy the bib information of each google scholar entry. The bib information will be automatically copied into the clipboard.
// @author       Maple
// @include      https://scholar.google.com*
// @include      https://scholar.google*
// @include      https://scholar.googleusercontent.com*
// @icon         https://icon-icons.com/downloadimage.php?id=130918&root=2108/ICO/64/&file=google_scholar_icon_130918.ico
// @grant        none
// @license MIT
// ==/UserScript==

//Sleep function
const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

async function openbib(event){
    var abib = (event.target || event.srcElement);
    abib.parentElement.querySelector("a.gs_or_cit.gs_nph").click();
    while(1){
        try{
            await sleep(100);
            document.querySelector("a.gs_citi").click();
            break;
        }catch(e){
        }
    }
}


var items = document.querySelectorAll("div.gs_ri div.gs_fl");
for (var item of items){
    var bib = document.createElement('a');
    bib.innerHTML = "Copy Bib"
    bib.setAttribute ('class', 'abib');
    bib.setAttribute ('style', 'display:inline; color: #180EA4 !important;');
    bib.setAttribute ('href', '#');
    //bib.setAttribute ('onclick', 'openbib(this);');
    bib.addEventListener (
        "click", openbib, false
    );
    item.appendChild (bib);
}


//============================================================

/*const copyToClipboard = str => {
  const el = document.createElement('textarea');
  el.value = str;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  el.focus();
  el.select();
  console.log(el);
  //var result = document.execCommand('copy');
  console.log(result);
  document.body.removeChild(el);
};*/

function fallbackCopyTextToClipboard(text) {
  var textArea = document.createElement("textarea");
  textArea.value = text;

  // Avoid scrolling to bottom
  textArea.style.top = "0";
  textArea.style.left = "0";
  textArea.style.position = "fixed";

  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Fallback: Copying text command was ' + msg);
  } catch (err) {
    console.error('Fallback: Oops, unable to copy', err);
  }

  document.body.removeChild(textArea);
}

function copyToClipboard(text) {
  if (!navigator.clipboard) {
    fallbackCopyTextToClipboard(text);
    return;
  }
  navigator.clipboard.writeText(text).then(function() {
    console.log('Async: Copying to clipboard was successful!');
  }, function(err) {
    console.error('Async: Could not copy text: ', err);
  });
}

if(window.location.href.includes("googleusercontent")){
    //document.body.innerText
    //console.log(document.body.innerText)
    copyToClipboard(document.body.innerText);
    window.history.go(-2);
}