RequestUpload

Facilitates uploading requests by adding an upload link to request page sidebar and populating the upload form with info available on the page.

Versione datata 25/10/2014. Vedi la nuova versione l'ultima versione.

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

You will need to install an extension such as Tampermonkey to install this 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            RequestUpload
// @namespace       varb
// @version         0.2
// @description     Facilitates uploading requests by adding an upload link to request page sidebar and populating the upload form with info available on the page.
// @match           *://bibliotik.org/requests/*
// @match           *://bibliotik.org/upload/*
// @require         https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @grant           GM_setValue
// @grant           GM_getValue
// @grant           GM_deleteValue
// ==/UserScript==

var FORMATS = {'MP3':1,'PDF':2,'CBR':3,'DJVU':4,'CBZ':5,'CHM':6,'FLAC':10,'SPX':13,'TXT':14,'EPUB':15,'MOBI':16,'M4A':17,'M4B':18,'AZW3':21};
var LANGS = {'English':1,'German':2,'French':3,'Spanish':4,'Italian':5,'Latin':6,'Japanese':7,'Swedish':8,'Norwegian':9,'Dutch':12,'Russian':13,'Portuguese':14,'Danish':15,'Korean':16,'Chinese':17,'Polish':18,'Arabic':19,'Irish':20,'Greek':21,'Turkish':22,'Hungarian':23,'Thai':24,'Indonesian':25,'Bulgarian':26};

$(document).ready(function () {

    console.log('requp: initialized');

    if (location.pathname.indexOf('requests') !== -1) {
        onReqPage();
    } else {
        onUploadPage();
    }

});

function onReqPage() {
    var category, lang, overdrive = false, req = {};
    var reqid = Math.random().toString(16).substr(2);
    var reqdetails = $('#requestDetails').text();

    if ($('#filled').length)
        return;

    // add upload link to req page sidebar
    var _match = reqdetails.match(/(?:(\w+),\s)?(\w+)\scategory/);
    if (_match !== null) {
        lang = _match[1];
        category = _match[2];
        var uppageurl = location.origin + '/upload/' + category.toLowerCase()
                      + '?reqid=' + reqid;
        $('#sidebar ul:first').append(
            '<li><a id="upreq" href="' + uppageurl + '">Upload Request</a></li>'
        );
    }

    // collect reusable info
    var $description = $('#description');
    req.title = $('h1').text().match(/\/\s*(.+?)(?:\s\[Retail\]$|$)/)[1];
    req.authors = $('#creatorlist a').map(function () { return this.text }).toArray();
    req.publishers = $('#published a').map(function () { return this.text }).toArray();
    req.tags = $('#details_tags a').map(function () {
        if (this.text.indexOf('overdrive') !== -1) {
            overdrive = true;
            return;
        }
        return this.text;
    }).toArray();
    req.overdrive = overdrive;
    req.retail = reqdetails.indexOf('Retail') !== -1;
    req.lang = lang;
    req.format = $('#requestDetails strong').text().split('/');
    req.desc = $description.html();
    if (category === 'Comics') {
        var $artists = $('#detailsbox > p').eq(1);
        if ($artists.attr('id') === 'creatorlist') { // same id is used for authors
            req.artists = $artists.find('a').map(function () { return this.text }).toArray();
        }
    }
    var _isbnmatch = $description.text().match(/Overdrive Listing \((\d+)\)/);
    req.isbn = _isbnmatch !== null ? _isbnmatch[1] : '';

    GM_setValue('req' + reqid, JSON.stringify(req));
}

function onUploadPage() {
    var _match = location.search.match(/reqid=(\w+)/);
    if (_match === null)
        return;
    var reqid = _match[1];
    var req = JSON.parse(GM_getValue('req' + reqid));
    GM_deleteValue('req' + reqid);

    // populate upload form fields
    $('#TitleField').val(req.title);
    $('#TagsField').val(req.tags.join(', '));
    $('#AuthorsField').val(req.authors.join(', '));
    if (req.format.length === 1)
        $('#FormatField').val(FORMATS[req.format[0]]);
    $('#PublishersField').val(req.publishers.join(', '));
    $('#LanguageField').val(LANGS[req.lang]);
    $('#RetailField').prop('checked', req.retail);
    $('#IsbnField').val(req.isbn);
    $('#DescriptionField').val(html2bb(
        req.overdrive ? $(req.desc).find('blockquote').html()
                      : $(req.desc).children('p:first').html()
    ));
}

function html2bb(text) {
    return text.replace(/\n/g, '')
        .replace(/<(\/)?em>/ig, '[$1i]')
        .replace(/<(\/)?strong>/ig, '[$1b]')
        .replace(/<(\/)?u>/ig, '[$1u]')
        .replace(/<(\/)?(o|u)l>/ig, '[$1$2l]')
        .replace(/(?:•|<li>)\s*(.+?)(?:<\/li>|<br>)/ig, function (m, c, p, s) {
            var firstli = s.search(/•/);
            if (firstli === p)
                return '[ul][*]' + c + '\n';
            if (firstli !== -1 && s.substr(p).match(/•/g).length === 1)
                return '[*]' + c + '[/ul]';
            return '[*]' + c + '\n';
        })
        .replace(/<a\s+href="(\S+?)">(.+?)<\/a>/ig, '[url=$1]$2[/url]')
        .replace(/<\/?(br|p)>/ig, '\n')
        .replace(/\s*--\s*/g, '\u2014');
}