Merged in isqua/chrome_point_plus/refactor_booru (pull request #39)

Refactor embedding from booru, tumblr etc.
This commit is contained in:
isqua 2015-02-03 19:04:40 +03:00
commit 81ad370af1
5 changed files with 180 additions and 89 deletions

View File

@ -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"

View File

@ -60,7 +60,7 @@
"message": "Включить встраивание ▼"
},
"option_images_load_booru": {
"message": "Загружать картинки с Booru, Tumblr и т.п."
"message": "Загружать картинки с Booru, Tumblr и т.п. через сервер Никиты"
},
"option_audios_parse_links": {
"message": "Аудио по прямой ссылке"

View File

@ -1,7 +1,7 @@
/**
* Объект для получения опций
* @constructor {OptionsManager}
* @param {Object} options Хеш настроек
* @constructor
*/
function OptionsManager(options) {
this._options = options || {};

View File

@ -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() {

View File

@ -0,0 +1,172 @@
/**
* Подгружает картинки через сервер Никиты
* @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';
/* jshint maxlen:false */
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
}
};
/* jshint maxlen:120 */
/**
* Обрабатывает все картинки
* @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] = matches[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 = $('<a>');
var $img = $('<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));
};