Bangumi-Index-Batch-Edit

Easilly modify multiple subjects in your index by drag-and-sort, and more!

  1. // ==UserScript==
  2. // @name Bangumi-Index-Batch-Edit
  3. // @namespace org.binota.scripts.bangumi.bibe
  4. // @description Easilly modify multiple subjects in your index by drag-and-sort, and more!
  5. // @include /^https?:\/\/((bgm|bangumi)\.tv|chii\.in)\/index\/\d+/
  6. // @version 0.0.8
  7. // @grant none
  8. // @require https://code.jquery.com/ui/1.11.4/jquery-ui.min.js
  9. // ==/UserScript==
  10.  
  11. //Check the owner of index, then insert the button for modify orders
  12. if($('.idBadgerNeue a.avatar').attr('href').search($('.grp_box a.avatar').attr('href')) >= 0) {
  13. $('.grp_box .tip_j').append(' / <a id="modifyOrder" class="chiiBtn" href="#">批量编辑</a>');
  14. $('#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>');
  15. $('<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');
  16. //Re-init the element we just inserted.
  17. tb_init('a.thickbox');
  18. }
  19.  
  20. //Get formhash
  21. var formhash = $('input[name="formhash"]').val();
  22.  
  23. var totalItems = 0;
  24. var saveItems = 0;
  25.  
  26. $('#modifyOrder').click(function() {
  27. $(this).remove();
  28. $('.grp_box .tip_j').append('<a id="saveOrder" class="chiiBtn" href="#">保存修改</a>');
  29.  
  30. //make items sortable.
  31. $('#browserItemList').sortable({
  32. handle: ".cover"
  33. });
  34.  
  35. //insert comment_box if needs.
  36. $('#browserItemList .tools').each(function() {
  37. if($(this).parent().find('.text').length == 0)
  38. $('<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));
  39. });
  40. $('#browserItemList .text').attr('contenteditable', 'true');
  41.  
  42. $('#saveOrder').click(function() {
  43. if(!confirm('确定要保存么?')) return;
  44. $(this).attr('disabled', 'disabled');
  45. $(this).html('保存中...');
  46. totalItems = $('#browserItemList > li').length;
  47. savedItems = 0;
  48. $('#browserItemList > li').each(function(i) {
  49. var content = $(this).find('.text').text().trim();
  50. var itemid = $(this).find('.tools :first-child').attr('id').match(/modify_(\d+)/)[1];
  51.  
  52. saveRelateItem(itemid, content, i);
  53. });
  54. });
  55. });
  56.  
  57. var saveRelateItem = function(id, content, order) {
  58. var postData = {
  59. content: content.trim(),
  60. formhash: formhash,
  61. order: order,
  62. submit: '提交'
  63. };
  64.  
  65. $.post('/index/related/' + id + '/modify', postData, function() {
  66. if(++savedItems == totalItems) return $('#saveOrder').html('保存完毕...!');
  67. $('#saveOrder').html('保存中... (' + savedItems + '/' + totalItems +')');
  68. });
  69. };
  70.  
  71. window.addRelateBatch = function() {
  72. $('.bibeBox input[name="submit"]').val('添加关联中...');
  73. var url = $('#indexCatBox a')[0].href + '/add_related';
  74. var items = $('.bibeBox textarea').val();
  75. var items = items.split("\n");
  76. for(i in items) {
  77. $.post(url, {add_related: items[i].trim(), formhash: formhash, submit: '添加新关联'});
  78. }
  79. $('.bibeBox input[name="submit"]').val('添加完毕...!');
  80. };
  81.