Bangumi-Index-Batch-Edit

批量添加目录条目,直接修改条目排序和评论,批量保存已修改的条目

当前为 2017-10-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Bangumi-Index-Batch-Edit
  3. // @namespace https://github.com/bangumi/scripts/liaune
  4. // @author binota,Liaune
  5. // @description 批量添加目录条目,直接修改条目排序和评论,批量保存已修改的条目
  6. // @include /^https?:\/\/((bgm|bangumi)\.tv|chii\.in)\/index\/\d+/
  7. // @version 1.0
  8. // @grant none
  9. // @require https://code.jquery.com/ui/1.11.4/jquery-ui.min.js
  10. // ==/UserScript==
  11.  
  12. //Check the owner of index, then insert the button for modify orders
  13. if($('.idBadgerNeue a.avatar').attr('href').search($('.grp_box a.avatar').attr('href')) >= 0) {
  14. $('.grp_box .tip_j').append(' / <a id="modifyOrder" class="chiiBtn" href="#">批量编辑</a>');
  15. $('#indexCatBox ul').append('<li><a id="addRelateBatch" class="add thickbox" title="批量添加关联" href="#TB_inline?tb&height=500&width=420&inlineId=newIndexRelatedBatch"><span>+批量添加关联</span></a></li>');
  16. $('<div style="display:none;" id="newIndexRelatedBatch"><div class="bibeBox" style="padding:10px"><label>输入条目URL或ID,如 http://bgm.tv/subject/265 或 265,一行一个</label><textarea rows="25" class="quick" name="urls"></textarea><input type="button" class="inputBtn" value="批量添加关联" name="submit" onclick="addRelateBatch()"></div></div>').insertBefore('#indexCatBox');
  17. //Re-init the element we just inserted.
  18. tb_init('a.thickbox');
  19. }
  20.  
  21. //Get formhash
  22. var formhash = $('input[name="formhash"]').val();
  23.  
  24. var totalItems = 0;
  25. var saveItems = 0;
  26.  
  27. $('#modifyOrder').click(function() {
  28. $(this).remove();
  29. $('.grp_box .tip_j').append('<a id="saveOrder" class="chiiBtn" href="#">保存修改</a>');
  30.  
  31. //make items sortable.
  32. $('#browserItemList').sortable({
  33. handle: ".cover"
  34. });
  35.  
  36. //insert comment_box if needs.
  37. $('#browserItemList .tools').each(function() {
  38. var order0=parseInt($(this).find('a').attr('order'));
  39. if($(this).parent().find('.text').length === 0)
  40. $('<div id="comment_box"><div class="item"><div style="float:none;" class="text_main_even"><div class="text"><br></div><div class="text_bottom"></div></div></div></div>').insertBefore($(this));
  41. $('<span class="tip">排序:</span><input id="modify_order" name="order" type="text" value='+order0+' class="inputtext">').insertAfter($(this));
  42. });
  43. $('#browserItemList .text').attr('contenteditable', 'true');
  44. var content={};
  45. var itemid={};
  46. var order={};
  47. $('#browserItemList > li').each(function(i) {
  48. content[i] = $(this).find('.text').text().trim();
  49. itemid[i] = $(this).find('.tools :first-child').attr('id').match(/modify_(\d+)/)[1];
  50. order[i]=parseInt($(this).find('input').attr('value'));
  51. });
  52. $('#saveOrder').click(function() {
  53. if(!confirm('确定要保存么?')) return;
  54. $(this).attr('disabled', 'disabled');
  55. $(this).html('保存中...');
  56. // totalItems = $('#browserItemList > li').length;
  57. totalItems=0;
  58. savedItems = 0;
  59. $('#browserItemList > li').each(function(i) {
  60. var content1 = $(this).find('.text').text().trim();
  61. var itemid1 = $(this).find('.tools :first-child').attr('id').match(/modify_(\d+)/)[1];
  62. var order1=parseInt($(this).find('input').attr('value'));
  63. if((order1!=order[i])|(content1!=content[i])){
  64. saveRelateItem(itemid1, content1, order1);
  65. totalItems++;}
  66. });
  67. });
  68. });
  69.  
  70. var saveRelateItem = function(id, content, order) {
  71. var postData = {
  72. content: content.trim(),
  73. formhash: formhash,
  74. order: order,
  75. submit: '提交'
  76. };
  77.  
  78. $.post('/index/related/' + id + '/modify', postData, function() {
  79. if(++savedItems == totalItems){ location.reload(); return $('#saveOrder').html('保存完毕...!');}
  80. $('#saveOrder').html('保存中... (' + savedItems + '/' + totalItems +')');
  81. });
  82. };
  83.  
  84. window.addRelateBatch = function() {
  85. $('.bibeBox input[name="submit"]').val('添加关联中...');
  86. var url = $('#indexCatBox a')[0].href + '/add_related';
  87. var items = $('.bibeBox textarea').val();
  88. items = items.split("\n");
  89. for(var i in items) {
  90. $.post(url, {add_related: items[i].trim(), formhash: formhash, submit: '添加新关联'});
  91. }
  92. $('.bibeBox input[name="submit"]').val('添加完毕...!');
  93. };
  94.