From 795298c94ff84b8ccfea7e97ad443fe9199a3a0c Mon Sep 17 00:00:00 2001 From: isqua Date: Sat, 31 Jan 2015 16:01:15 +0300 Subject: [PATCH 1/2] Refactor embedding from booru, tumblr etc. --- chrome_point_plus/js/options-manager.js | 2 +- chrome_point_plus/js/point-plus.js | 91 +------------ chrome_point_plus/modules/booru.js | 171 ++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 87 deletions(-) create mode 100644 chrome_point_plus/modules/booru.js diff --git a/chrome_point_plus/js/options-manager.js b/chrome_point_plus/js/options-manager.js index 0a5072b..d75117b 100644 --- a/chrome_point_plus/js/options-manager.js +++ b/chrome_point_plus/js/options-manager.js @@ -1,7 +1,7 @@ /** * Объект для получения опций + * @constructor {OptionsManager} * @param {Object} options Хеш настроек - * @constructor */ function OptionsManager(options) { this._options = options || {}; diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index de2b23f..424581c 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -47,7 +47,11 @@ function PointPlus(ppVersion) { if (options.is('option_embedding')) { // Load pictures from Booru, Tumblr and some other sites if (options.is('option_images_load_booru')) { - load_all_booru_images(); + messenger.js({ + file: 'modules/booru.js' + }, function() { + var booru = new Booru($('.post-content .text a:not(.booru_pic)'), options); + }); } // Instagram @@ -726,91 +730,6 @@ function create_comment_elements(commentData, onCommentCreated) { }); } -// Картинки с бурятников -var booru_picture_count = 0; -function load_all_booru_images() { - $('.post-content a:not(.booru_pic)').each(function(num, obj) { - - var href = obj.href; - var n = null; - - if (n = href.match(new RegExp('^https?://danbooru\\.donmai\\.us/posts/([0-9]+)', 'i'))) { - var image = create_image('danbooru', n[1]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?gelbooru\\.com\\/index\\.php\\?page\\=post&s\\=view&id=([0-9]+)', 'i'))) { - var image = create_image('gelbooru', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?safebooru\\.org\\/index\\.php\\?page\\=post&s\\=view&id=([0-9]+)', 'i'))) { - var image = create_image('safebooru', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?([a-z0-9-]+\\.)?deviantart\\.com\\/art/[0-9a-z-]+?\\-([0-9]+)(\\?.+)?$', 'i'))) { - var image = create_image('deviantart', n[3]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?e621\\.net\\/post\\/show\\/([0-9]+)\\/', 'i'))) { - var image = create_image('e621', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://derpiboo\\.ru\\/([0-9]+)', 'i'))) { - var image = create_image('derpibooru', n[1]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://([0-9a-z-]+)\\.tumblr\\.com\\/post\\/([0-9]+)', 'i'))) { - var image = create_image('tumblr', n[2], {'username': n[1]}); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - /* - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?konachan\\.net\\/post\\/show\\/([0-9]+)\\/', 'i'))) { - var image = create_image('konachannet', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^https?\\://(www\\.)?konachan\\.com\\/post\\/show\\/([0-9]+)\\/', 'i'))) { - var image=create_image('konachancom', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - */ - } else if (n = href.match(new RegExp('^https?://(www\\.)?pixiv\\.net\\/member_illust\\.php\\?mode\\=medium\\&illust_id\\=([0-9]+)', 'i'))) { - var image = create_image('pixiv', n[2]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (n = href.match(new RegExp('^http\\:\\/\\/anime\\-pictures\\.net\\/pictures\\/view_post\\/([0-9]+)', 'i'))) { - var image = create_image('animepicturesnet', n[1]); - obj.parentElement.insertBefore(image, obj); - booru_picture_count++; - } else if (false) { - - - } - - }); - -} - -function create_image(domain, id, additional) { - var a = document.createElement('a'); - a.href = 'https://api.kanaria.ru/point/get_booru_picture.php?domain=' + domain + '&id=' + id; - if (typeof(additional) != 'undefined') { - for (var index in additional) { - a.href += '&add_' + encodeURIComponent(index) + '=' + encodeURIComponent(additional[index]); - } - } - $(a).addClass('booru_pic').addClass('booru-' + domain + '-' + id).addClass('postimg').attr({ - 'id': 'booru_pic_' + booru_picture_count, - 'title': domain + ' image #' + id, - 'target': '_blank' - }); - - var image = document.createElement('img'); - image.alt = a.title; - image.src = a.href; - a.appendChild(image); - - return a; -} - // Помечаем непрочитанные посты более видимо чем каким-то баджем // Эта часть написана @RainbowSpike function mark_unread_post() { diff --git a/chrome_point_plus/modules/booru.js b/chrome_point_plus/modules/booru.js new file mode 100644 index 0000000..dd748ac --- /dev/null +++ b/chrome_point_plus/modules/booru.js @@ -0,0 +1,171 @@ +/** + * Подгружает картинки через сервер Никиты + * @constructor + * @param {jQuery} $links Коллекция ссылок + * @param {OptionsManager} options Опции + */ +function Booru($links, options) { + this.count = 0; + + this.loadAllImages($links, options.is('option_embedding_remove_original_link')); +} + +/** + * Откуда тянуть картинки + * @type {String} + */ +Booru.baseUrl = 'https://api.kanaria.ru/point/get_booru_picture.php'; + +Booru.services = { + danbooru: { + mask: new RegExp('^https?://danbooru\\.donmai\\.us/posts/([0-9]+)', 'i'), + matchNumber: 1 + }, + gelbooru: { + mask: new RegExp('^https?\\://(www\\.)?gelbooru\\.com\\/index\\.php\\?page\\=post&s\\=view&id=([0-9]+)', 'i'), + matchNumber: 2 + }, + safebooru: { + mask: new RegExp('^https?\\://(www\\.)?safebooru\\.org\\/index\\.php\\?page\\=post&s\\=view&id=([0-9]+)', 'i'), + matchNumber: 2 + }, + deviantart: { + mask: new RegExp('^https?\\://(www\\.)?([a-z0-9-]+\\.)?deviantart\\.com\\/art/[0-9a-z-]+?\\-([0-9]+)(\\?.+)?$', 'i'), + matchNumber: 3 + }, + e621: { + mask: new RegExp('^https?\\://(www\\.)?e621\\.net\\/post\\/show\\/([0-9]+)\\/', 'i'), + matchNumber: 2 + }, + derpibooru: { + mask: new RegExp('^https?\\://derpiboo\\.ru\\/([0-9]+)', 'i'), + matchNumber: 1 + }, + tumblr: { + mask: new RegExp('^https?\\://([0-9a-z-]+)\\.tumblr\\.com\\/post\\/([0-9]+)', 'i'), + matchNumber: 2, + params: { + add_username: 1 + } + }, + pixiv: { + mask: new RegExp('^https?://(www\\.)?pixiv\\.net\\/member_illust\\.php\\?mode\\=medium\\&illust_id\\=([0-9]+)', 'i'), + matchNumber: 2, + }, + animepicturesnet: { + mask: new RegExp('^http\\:\\/\\/anime\\-pictures\\.net\\/pictures\\/view_post\\/([0-9]+)', 'i'), + matchNumber: 1 + } +}; + +/** + * Обрабатывает все картинки + * @param {jQuery} $links Коллекция ссылок + * @param {Boolean} removeOriginal Удалять ли оригинальную ссылку + */ +Booru.prototype.loadAllImages = function($links, removeOriginal) { + var booru = this; + + $links.each(function(index, link) { + var $link = $(link); + var href = link.href; + var $image; + + if ($link.hasClass('booru_pic')) { + return; + } + + Object.keys(booru.constructor.services).some(function(service) { + $image = booru.createImageFromService(service, href); + + return $image; + }); + + if ($image) { + $link.before($image); + this.count++; + + if (removeOriginal) { + $link.remove(); + } + } + }); +}; + +/** + * Создаёт картинку исходя из сервиса, если адрес картинки матчится + * @param {String} service Идентификатор сервиса + * @param {String} href URL картинки (который вставлен в пост) + */ +Booru.prototype.createImageFromService = function(service, href) { + var serviceInfo = this.constructor.services[service]; + var matches = href.match(serviceInfo.mask); + var imageArgs; + var params = {}; + var key; + + if (matches) { + imageArgs = [ service, matches[serviceInfo.matchNumber] ]; + + if (serviceInfo.params) { + for (key in serviceInfo.params) { + if (serviceInfo.params.hasOwnProperty(key)) { + params[key] = mathes[serviceInfo.params[key]]; + } + } + + imageArgs.push(params); + } + + return this.createImage.apply(this, imageArgs); + } +}; + +/** + * Создаёт ссылку с картикной + * @param {String} service Ключевое имя сервиса для Никиты + * @param {String} id Идентификатор картинки + * @param {Object} [params] Дополнительные параметры, которые надо добавить в url + * @returns {jQuery} Элемент ссылки + */ +Booru.prototype.createImage = function(service, id, params) { + var $link = $(''); + var $img = $(''); + var title = service + ' image #' + id; + var imageSource = this.getImageLink(service, id, params); + + $link + .addClass('booru_pic') + .addClass('booru-' + service + '-' + id) + .addClass('postimg') + .attr({ + href: imageSource, + id: 'booru_pic_' + this.count, + title: title, + target: '_blank' + }); + + $img.attr({ + alt: title, + src: imageSource + }); + + $link.append($img); + + return $link; +}; + +/** + * Генерирует ссылку на картинку + * @param {String} service Ключевое имя сервиса для Никиты + * @param {String} id Идентификатор картинки + * @param {Object} [params] Дополнительные параметры, которые надо добавить в url + * @returns {String} Ссылка на картинку + */ +Booru.prototype.getImageLink = function(service, id, params) { + return this.constructor.baseUrl + '?' + $.param($.extend({ + domain: service, + id: id, + }, params)); +}; + From 358d44b3bdfb3fb86c7e59ed8895c7499b7d6447 Mon Sep 17 00:00:00 2001 From: isqua Date: Sat, 31 Jan 2015 20:45:00 +0300 Subject: [PATCH 2/2] Add info about Nokita server to options --- chrome_point_plus/_locales/en/messages.json | 2 +- chrome_point_plus/_locales/ru/messages.json | 2 +- chrome_point_plus/modules/booru.js | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/chrome_point_plus/_locales/en/messages.json b/chrome_point_plus/_locales/en/messages.json index 9aa5015..548d89a 100644 --- a/chrome_point_plus/_locales/en/messages.json +++ b/chrome_point_plus/_locales/en/messages.json @@ -60,7 +60,7 @@ "message": "Enable embedding ▼" }, "option_images_load_booru": { - "message": "Load pictures from Booru, Tumblr, etc" + "message": "Load pictures from Booru, Tumblr, etc via NokitaKaze server" }, "option_audios_parse_links": { "message": "Audio from direct links" diff --git a/chrome_point_plus/_locales/ru/messages.json b/chrome_point_plus/_locales/ru/messages.json index 3cfa8e8..82c3561 100644 --- a/chrome_point_plus/_locales/ru/messages.json +++ b/chrome_point_plus/_locales/ru/messages.json @@ -60,7 +60,7 @@ "message": "Включить встраивание ▼" }, "option_images_load_booru": { - "message": "Загружать картинки с Booru, Tumblr и т.п." + "message": "Загружать картинки с Booru, Tumblr и т.п. через сервер Никиты" }, "option_audios_parse_links": { "message": "Аудио по прямой ссылке" diff --git a/chrome_point_plus/modules/booru.js b/chrome_point_plus/modules/booru.js index dd748ac..3dbb66c 100644 --- a/chrome_point_plus/modules/booru.js +++ b/chrome_point_plus/modules/booru.js @@ -16,6 +16,7 @@ function Booru($links, options) { */ Booru.baseUrl = 'https://api.kanaria.ru/point/get_booru_picture.php'; +/* jshint maxlen:false */ Booru.services = { danbooru: { mask: new RegExp('^https?://danbooru\\.donmai\\.us/posts/([0-9]+)', 'i'), @@ -50,13 +51,14 @@ Booru.services = { }, pixiv: { mask: new RegExp('^https?://(www\\.)?pixiv\\.net\\/member_illust\\.php\\?mode\\=medium\\&illust_id\\=([0-9]+)', 'i'), - matchNumber: 2, + matchNumber: 2 }, animepicturesnet: { mask: new RegExp('^http\\:\\/\\/anime\\-pictures\\.net\\/pictures\\/view_post\\/([0-9]+)', 'i'), matchNumber: 1 } }; +/* jshint maxlen:120 */ /** * Обрабатывает все картинки @@ -86,7 +88,7 @@ Booru.prototype.loadAllImages = function($links, removeOriginal) { this.count++; if (removeOriginal) { - $link.remove(); + $link.remove(); } } }); @@ -110,7 +112,7 @@ Booru.prototype.createImageFromService = function(service, href) { if (serviceInfo.params) { for (key in serviceInfo.params) { if (serviceInfo.params.hasOwnProperty(key)) { - params[key] = mathes[serviceInfo.params[key]]; + params[key] = matches[serviceInfo.params[key]]; } } @@ -165,7 +167,6 @@ Booru.prototype.createImage = function(service, id, params) { Booru.prototype.getImageLink = function(service, id, params) { return this.constructor.baseUrl + '?' + $.param($.extend({ domain: service, - id: id, + id: id }, params)); }; -