node / whatcd gazelle export bookmarks

// ==UserScript==
// @name        whatcd gazelle export bookmarks
// @include     /https?://www\.empornium\.(me|sx)/bookmarks.php\?type=torrents.*/
// @include     /https?://www\.empornium\.(me|sx)/torrents.php\?type=uploaded.*/
// @include     /https?://femdomcult\.org/bookmarks.php\?type=torrents.*/
// @version     1
// @require     http://code.jquery.com/jquery-2.1.1.js
// @grant       GM_addStyle
// ==/UserScript==

"use strict";

this.$ = this.jQuery = jQuery.noConflict(true);

GM_addStyle('' +
    '#export-torrents {' +
    '  display: none;' +
    '  position: absolute;' +
    '  z-index: 100;' +
    '  top: 20%;' +
    '  left: 50%;' +
    '  margin-left: -250px;' +
    '  width: 500px;' +
    '  box-shadow: 0px 0px 9px 2px hsl(0, 0%, 42%);' +
    '  border-radius: 6px 6px 6px 6px;' +
    '}' +
    '#export-torrents > div {' +
    '  padding: 2px 5px;' +
    '  background: linear-gradient(to bottom, hsl(0, 0%, 70%), hsl(0, 0%, 56%));' +
    '  font-size: 10pt;' +
    '  color: white;' +
    '  border-radius: 5px 5px 0 0;' +
    '}' +
    '#export-torrents a {' +
    '  color: white;' +
    '  text-decoration: none;' +
    '  margin-right: 5px' +
    '}' +
    '#export-torrents .eb-button {' +
    '  border-radius: 5px;' +
    '  padding: 1px 3px;' +
    '  display: inline-block;' +
    '}' +
    '#export-torrents .on {' +
    '  background-color: #6F6F6F;' +
    '}' +
    '#export-torrents textarea {' +
    '  resize: vertical;' +
    '  width: 100%;' +
    '  min-height: 20em;' +
    '  border: none;' +
    '  box-sizing: border-box;' +
    '  padding: 3px 5px;' +
    '  color: #222;' +
    '  border-radius: 0 0 5px 5px;' +
    '}' +
    '');

function ExportTorrents() {
    var self = this;

    this.$window = null;
    this.$textarea = null;
    this.$sort_date = null;
    this.$sort_title = null;

    this.init = function () {
        var title = document.URL.indexOf('bookmarks') !== -1 ? 'Export Bookmarks' : 'Export Torrents';
        $('#userinfo_username').prepend('<li class="export-torrents"><a href="#" style="padding-left: 2px">☂ ' + title + '</a></li>');
        var $button = $('.export-torrents a');
        $button.click(self.open_window);

        $('body').append('' +
            '<div id="export-torrents"><div>' +
            '<a class="close" href="#">✕</a>' +
            '<span></span>' +
            ', sorting by ' +
            '<a class="sort-title eb-button" href="#">title</a>' +
            '<a class="sort-date eb-button" href="#">default (date)</a>' +
            '</div>' +
            '<textarea></textarea>' +
            '</div>' +
            '');

        self.$window = $('#export-torrents');
        self.$title = self.$window.find('span');
        self.$textarea = self.$window.find('textarea');
        self.$sort_title = self.$window.find('.sort-title');
        self.$sort_date = self.$window.find('.sort-date');

        self.$sort_title.click(function() {self.update_text_area(true)});
        self.$sort_date.click(function() {self.update_text_area(false)});

        var $close = self.$window.find('.close');
        $close.click(self.close_window);
    };

    this.get_torrent_list = function () {
        return $('a[href^="torrents.php?id="]:first-of-type').map(function () {
            return {
                title: this.text,
                url: this.href
            };
        }).toArray();
    };

    this.list_to_text = function (list) {
        return list.map(function (value) {
            return value.title.trim() + '\n' + '# ' + value.url;
        }).join('\n\n');
    };

    this.compare = function(a, b) {
        return a < b ? -1 : a > b ? 1 : 0;
    };

    this.compare_title = function (a, b) {
        return self.compare(a.title, b.title);
    };

    this.update_text_area = function (sort_by_title) {
        var list = self.get_torrent_list();
        self.$sort_title.toggleClass('on', sort_by_title);
        self.$sort_date.toggleClass('on', !sort_by_title);
        if (sort_by_title)
            list = list.sort(self.compare_title);
        self.$title.text(list.length + ' Bookmarks were found');
        var text = self.list_to_text(list);
        self.$textarea.val(text);
    };

    this.open_window = function () {
        self.$window.show();
        self.update_text_area(true);
    };

    this.close_window = function () {
        self.$window.hide();
    };

    this.init();
}

new ExportTorrents();