BookCrossing BCID Export

Export BCIds, title, author, and category of books on the current page to a CSV file. This can than be used to print labels.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         BookCrossing BCID Export
// @namespace    http://tampermonkey.net/
// @version      2025-01-05
// @description  Export BCIds, title, author, and category of books on the current page to a CSV file. This can than be used to print labels.
// @author       Kiki
// @match        https://www.bookcrossing.com/mybookshelf/*/available
// @icon         https://www.bookcrossing.com/images/site/bookcrossing-icon-new-256.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    createButton();
})();

function createButton()
{
    var menu = document.querySelector('div#content > h2 + div');
    if(null == menu)
    {
        console.log('Menu not found, cannot create button.');
        return;
    }
    console.log('createButton 1');

    var btn = createElementFromHTML(`
    <a target="_blank" class="plain" href="" title="Export BCIDs" onclick="return false;">
      <i class="fas fa-save muted">
      </i>
    </a>
    `);
    btn.innerHTML += "&nbsp;&nbsp;";

    btn.onclick = exportAll;

    menu.insertBefore(btn, menu.firstChild);
}

function createElementFromHTML(htmlString)
{
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  // Change this to div.childNodes to support multiple top-level nodes.
  return div.firstChild;
}


function exportAll()
{
    var txt = "BCID\tTitle\tAuthor\tCategory";
    document.querySelectorAll('div.book-list-detail').forEach(
        (book) => {
            var title = book.querySelector('h5 > a').innerText;
            var author = book.querySelector('div > a').innerText;
            var cat = book.querySelector('div > a:nth-of-type(2)').innerText;
            var bcid = book.querySelector('div:nth-of-type(4)  > div:nth-of-type(2) > span').innerText;
            bcid = bcid.replace('BCID: ', '');
            txt += `\r\n${bcid}\t${title}\t${author}\t${cat}`;
        });

    var dlurl = 'data:text/csv;base64,' + btoa(txt);

    saveFile(dlurl);

    return false;
}


function exportBCIDs()
{
    var ids = "BCID\r\n";
    document.querySelectorAll('div.my-2 > span').forEach(
        (e) => {
            ids = ids + e.textContent.replace('BCID: ', '') + "\n";
        });

    var dlurl = 'data:text/csv;base64,' + btoa(ids);

    saveFile(dlurl);

    return false;
}


function saveFile(fileURL, fileName)
{
  var save = document.createElement('a');
  save.href = fileURL;
  save.target = '_blank';
  save.download = fileName || 'unknown';

  var evt = new MouseEvent('click',
  {
      //'view': window,
      'bubbles': true,
      'cancelable': false
  });
  save.dispatchEvent(evt);
}