// ==UserScript==
// @name pixiv add-bookmark
// @namespace http://d.hatena.ne.jp/monjudoh/
// @include http://www.pixiv.net/member_illust.php?mode=medium&illust_id=*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.js
// @require http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.js
// ==/UserScript==
(function($){
jQuery.makeArray=function( array ) {
var ret = [];
if( array != null ){
var i = array.length;
//the window, strings and functions also have 'length'
if( i == null || array.split || array.setInterval || array.apply )
ret[0] = array;
else
while( i )
ret[--i] = array[i];
}
return ret;
};
function GroupSelector(){
this.title = $('title').text();
this.comment = $('#illust_comment').text();
this.tags = $('#tags a').map(function(i,n){
return $(n).text();
}).get();
this.guestComment = ( $('#one_comment_area tr').map(function(){
return $('td:eq(1)',this).text();
}).get() || []).join();
this._$selectedGroups = $('
');
this._$container = $('');
var self = this;
this.findGroupsFromGMValue(function(){
self.findSelectedGroupIndexes();
self.createDialog();
});
}
GroupSelector.prototype.findGroupsFromGMValue = function(callback){
GM_log('findGroupsFromGMValue');
var groups = eval(GM_getValue('groups',''));
//GM_log(groups);
if(!groups){
this.findGroupsFromBookmarkPage(callback);
return;
}
this.groups = groups;
callback();
};
GroupSelector.prototype.findGroupsFromBookmarkPage = function(callback){
GM_log('findGroupsFromBookmarkPage');
var self = this;
$.ajax({
type:'GET'
,url:'http://www.pixiv.net/bookmark_group_setting.php'
,success:function(html){
var frag = html.replace(/^([\n\r]|.)*?|<\/html([\r\n]|.)*$/ig,"");
var d = document;
var htmlDoc = d.implementation.createDocument(null,"html",null);
var r=d.createRange();
r.setStartAfter(d.body);
htmlDoc.documentElement.appendChild(r.createContextualFragment(frag));
var groups = $('#mypixiv_list>div[class^=bg_] a',htmlDoc).map(function(i,n){
return $(n).text();
}).get();
groups.pop();
GM_setValue('groups',groups.toSource());
self.groups = groups;
callback();
}
});
};
GroupSelector.prototype.findSelectedGroupIndexes = function(){
GM_log('findSelectedGroupIndexes');
var self = this;
var tagPattern= new RegExp(self.tags.join('|'),'g');
self.selectedGroupIndexes = $.map(self.groups,function(n,i){
if(tagPattern.test(n))return i;
var groupPattern = new RegExp(n,'g');
return groupPattern.test(self.tags) ||
groupPattern.test(self.title) ||
groupPattern.test(self.comment) ||
groupPattern.test(self.guestComment) ? i : undefined ;
});
};
GroupSelector.prototype.checkGroup = function(index){
GM_log('checkGroup index:' + index);
if(~$.inArray(index,this.selectedGroupIndexes))return;
this.selectedGroupIndexes.push(index);
};
GroupSelector.prototype.unCheckGroup = function(index){
GM_log('unCheckGroup index:' + index);
var i = $.inArray(index,this.selectedGroupIndexes);
if(i == -1)return;
this.selectedGroupIndexes.splice(i,1);
};
GroupSelector.prototype.toggleCheckGroup = function(index){
GM_log('toggleCheckGroup index:' + index);
var i = $.inArray(index,this.selectedGroupIndexes);
// check
if(i == -1){
this.selectedGroupIndexes.push(index);
// unCheck
}else{
this.selectedGroupIndexes.splice(i,1);
}
this.rewriteSelectedGroups();
};
GroupSelector.prototype.rewriteSelectedGroups = function(){
GM_log('rewriteSelectedGroups');
var self = this;
var $selectedGroups = this._$selectedGroups;
var groups = this.groups;
$selectedGroups.empty();
$(this.selectedGroupIndexes).each(function(i,n){
$selectedGroups
.append(
$('')
.text(groups[n])
.click(function(e){
self.unCheckGroup(n);
$(this).remove();
})
).append(' ');
});
};
GroupSelector.prototype.createDialog = function(){
var self = this;
var $mainTable = $('')
.css({width:'680px'});
$('tr:eq(0)>td:eq(0)',$mainTable)
.css({width:'180px'});
$('tr:eq(0)>td:eq(1)',$mainTable)
.css({width:'500px'});
$('tr:eq(1)>td:eq(1)',$mainTable)
.append($('')
.attr({
id:'res0',
name:'restrict',
value:'0',
checked:'true'
}))
.append($(''))
.append($('')
.attr({
id:'res1',
name:'restrict',
value:'1'
}))
.append($(''));
$('')
.attr({
name:'comment',
maxlength:'140'
})
.css({width:'400px'})
.appendTo($('tr:eq(2)>td:eq(1)',$mainTable));
var $selectedGroups = this._$selectedGroups;
var $container = this._$container
.append($selectedGroups)
.append($mainTable);
var $td = $('tr:eq(0)>td:eq(1)',$mainTable);
$(self.groups).each(function(i,n){
var $a = $('')
.text(n)
.attr('href','#')
.click(function(e){
e.preventDefault();
$a.toggleClass('selected-group');
self.toggleCheckGroup(i);
});
if(~$.inArray(i,self.selectedGroupIndexes)){
$a.addClass('selected-group');
}
$td.append($a).append(' ');
});
this.rewriteSelectedGroups();
$container.dialog({
title:'ブックマーク登録',
dialogClass:'flora',
resizable:true,
draggable:true,
width:700,
height:320,
buttons:{
'追加':function(){
var restrict = $('input[name="restrict"]',$mainTable).val();
var comment = $('input[name="comment"]',$mainTable).val();
var $dialog = $(this);
self.addBookmark({
restrict:restrict,
comment:comment,
callback:function(){$dialog.dialog('close');}
});
}
}
});
};
GroupSelector.prototype.showDialog = function(){
this._$container.dialog('open');
};
GroupSelector.prototype.addBookmark = function(options){
GM_log('addBookmark options:' + options);
var self = this;
var id;
$.each(location.search.slice(1).split('&'),function(i,n){
var keyValue = n.split('=');
if(keyValue[0] == 'illust_id'){
id=keyValue[1];
}
});
var groups = $(this.selectedGroupIndexes).map(function(i,n){
return self.groups[n];
}).get().join(' ');
var data={
mode:'add'
,type:'illust'
,id:id
,group:groups
,restrict:options.restrict
,comment:options.comment
};
$.ajax({
type:'POST'
,url:'http://www.pixiv.net/bookmark_add.php'
,data:data
,success:(options.callback || function(){})
});
};
$('')
.attr({
type:'text/css',
rel:'stylesheet',
href:'http://dev.jquery.com/view/tags/ui/latest/themes/flora/flora.all.css'
})
.appendTo('head');
GM_addStyle(<>>);
$('.bookmark_bt a').click(function(e){
e.preventDefault();
var $self = $(this);
var groupSelector = $self.data('groupSelector');
if(groupSelector){
groupSelector.showDialog();
}else{
$self.data('groupSelector',new GroupSelector());
}
});
})(jQuery);