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.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);
}