← All examples

JavaScript Productivity arxiv.org

arXiv: one-click Copy BibTeX button

Adds a button next to the paper title that copies a BibTeX entry with arxiv ID, authors and year to the clipboard.

Code to copy

// Add a one-click "Copy BibTeX" button next to the paper title
(function () {
  function bibtex() {
    var id = (location.pathname.match(/\/abs\/([\w.\-\/]+)/) || [])[1];
    if (!id) { return null; }
    var titleEl = document.querySelector('h1.title');
    var title = titleEl ? titleEl.textContent.replace(/^\s*Title:\s*/i, '').trim() : id;
    var authorEls = document.querySelectorAll('.authors a');
    var authors = Array.from(authorEls).map(function (a) { return a.textContent.trim(); }).join(' and ');
    var year = ((document.querySelector('.dateline, .submission-history') || {}).textContent || '').match(/(19|20)\d{2}/);
    year = year ? year[0] : '';
    var key = id.replace(/[^a-zA-Z0-9]/g, '');
    return '@article{arxiv' + key + ',\n  title  = {' + title + '},\n  author = {' + authors + '},\n  journal= {arXiv preprint arXiv:' + id + '},\n  year   = {' + year + '}\n}';
  }
  function mount() {
    if (document.getElementById('jzArxBibBtn')) { return; }
    var host = document.querySelector('h1.title') || document.querySelector('#abs');
    if (!host) { return; }
    var btn = document.createElement('button');
    btn.id = 'jzArxBibBtn';
    btn.type = 'button';
    btn.textContent = 'Copy BibTeX';
    btn.style.cssText = 'margin-left:10px;padding:4px 10px;background:#b31b1b;color:#fff;border:0;border-radius:4px;font-size:13px;cursor:pointer;font-weight:600;';
    btn.addEventListener('click', function () {
      var t = bibtex();
      if (!t) { return; }
      navigator.clipboard.writeText(t).then(function () {
        btn.textContent = 'Copied!';
        setTimeout(function () { btn.textContent = 'Copy BibTeX'; }, 1500);
      });
    });
    host.appendChild(btn);
  }
  mount();
  new MutationObserver(mount).observe(document.body, { childList: true, subtree: true });
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases