// ==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(callback){ 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.initialized = $.isFunction(callback) ? callback :function(){}; 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(); self.groups = groups || []; 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 ; }); self.initialized(); } }); } GroupSelector.prototype.checkGroup = function(index){ if(~$.inArray(index,this.selectedGroupIndexes))return; this.selectedGroupIndexes.push(index); }; GroupSelector.prototype.unCheckGroup = function(index){ var i = $.inArray(index,this.selectedGroupIndexes); if(i == -1)return; this.selectedGroupIndexes.splice(i,1); }; GroupSelector.prototype.toggleCheckGroup = function(index){ var i = $.inArray(index,this.selectedGroupIndexes); // check if(i == -1){ this.selectedGroupIndexes.push(index); // unCheck }else{ this.selectedGroupIndexes.splice(i,1); } this.rewriteSelectedGroups(); }; var $selectedGroups = $('
'); GroupSelector.prototype.rewriteSelectedGroups = function(){ var groups = this.groups; $selectedGroups.empty(); $(this.selectedGroupIndexes).each(function(i,n){ $selectedGroups .append($('').text(groups[n])) .append(' '); }); }; GroupSelector.prototype.addBookmark = function(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'); 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 $container = $('
') .append($selectedGroups) .append($mainTable); $('.bookmark_bt a').click(function(e){ e.preventDefault(); new GroupSelector(function(){ var self = this; 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'); } self.rewriteSelectedGroups(); $td.append($a).append(' '); }); $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');} }); } } }); }); }); })(jQuery);