RequestUpload

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

目前為 2014-10-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            RequestUpload
// @namespace       varb
// @version         0.1
// @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();

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