ブックマークのタグ追加

今のところ使えそう。

(function() {
  function bookmark_add(id, tags)
  {
    return $.ajax({
      url: 'https://www.pixiv.net/bookmark_add.php?id=' + id,
      type: 'POST',
      data: {
        comment: '',
        from_sid: '',
        id: id,
        mode: 'add',
        original_order: '',
        original_p: 1,
        original_rest: 'show',
        original_tag: '',
        original_untagged: 0,
        restrict: 0,
        tag: tags,
        tt: pixiv.context.token,
        type: 'illust',
      },
      timeout:10000,
    });
  }
  
  var c = 0;
  function update()
  {
    console.log("update", c);
    if(++c > 10) {
      console.log("Finish");
      return;
    }
    
    var deferred = (new $.Deferred()).resolve();
    $.ajax({url: 'https://www.pixiv.net/bookmark.php?untagged=1'}).done(function(data) {
      var items = $('div.display_editable_works ul li.image-item', data);
      items.each(function() {
        var img = $("img", this);
        var id = img.attr('data-id');
        var tags = img.attr('data-tags');
        deferred = deferred.then(function() {
          var ret = new $.Deferred();
          console.log(id, tags);
          bookmark_add(id, tags).
            done(function(data) { console.log("done"); }).
            fail(function(XMLHttpRequest, textStatus, errorThrown) { console.log("fail", textStatus); }).
            always(function() { ret.resolve(); });
          return ret.promise()
        });
      });
      if(items.length > 0)
      {
        deferred.then(function() { update(); });
      } else {
        console.log("Finish");
      }
    });
  }
  update();
})();