IMDB List Importer

Import list of titles, people or characters in the imdb list

目前為 2017-05-02 提交的版本,檢視 最新版本

// ==UserScript==
// @name        IMDB List Importer
// @namespace   Neinei0k_imdb
// @include     http://www.imdb.com/list/edit*
// @version     4.0
// @grant       none
// @description Import list of titles, people or characters in the imdb list
// ==/UserScript==

var import_list = function(event) {
  var element = event.target.parentElement;
  var text = element.children[0].value;
  var ready_e = element.children[3];
  var checkboxes = element.querySelectorAll('input[type="checkbox"]');
  var csv = checkboxes[0].checked;
  var unique = checkboxes[1].checked;
  
  var log = function(msg) {
    console.log("IMDB List Importer: " + msg);
  };
  var create_list = function() {
    var re;
    if (document.getElementsByClassName('list_characters').length !== 0) {
      re = /ch[0-9]{7}/;
    } else if (document.getElementsByClassName('list_people').length !== 0) {
      re = /nm[0-9]{7}/;
    } else if (document.getElementsByClassName('list_titles').length !== 0) {
      re = /tt[0-9]{7}/;
    } else {
      return [];
    }
    var list = [];
    if (csv) {
      text = text.split('\n');
      var fl = JSON.parse('[' + text[0] + ']');
      // Find const and description field numbers.
      var const_field = fl.indexOf('const')
      var description_field = fl.indexOf('description');
      if (const_field === -1 || description_field === -1) {
        ready_e = "Error: data structure is not csv or invalid.";
        return [];
      }
      // Add elements to the list
      text.shift();
      for (var i in text) {
        fl = JSON.parse('[' + text[i] + ']');
        if (const_field >= fl.length || description_field >= fl.length) {
          ready_e = "Error: data structure is not csv or invalid.";
          return [];
        }
        if (unique) {
          var exists = list.findIndex(function(v){
            return v.const === fl[const_field];
          });
          if (exists === -1) continue;
        }
        list.push({const: fl[const_field],description: fl[description_field]});
      }
    } else {
      var e;
      while ((e = re.exec(text)) !== null) {
        var rer;
        if (unique) rer = new RegExp(e[0], 'g');
        else rer = new RegExp(e[0]);
        text = text.replace(rer, '');
        list.push({const: e[0], description: ""});
      }
    }
    
    return list;
  };
  
  var add_list = function(list) {
    var length = list.length;
    if (length == 0)
      return;
    var list_id = /ls[0-9]{1,}/.exec(location.href) [0];
    var ready = 0;
    log("Elements to add: " + list);
    function showReady() {
      log('Element was added');
      ready += 1;
      ready_e.innerHTML = 'Ready ' + ready + ' of ' + length + '.';
      if (ready == length) {
        location.reload();
      } else {
        sendNext();
      }
    }
    function sendNext() {
      var xhttp = new XMLHttpRequest();
      log('Add element ' + ready + ': ' + list[ready].const);
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          if (list[ready].description !== "") {
            log('Add description');
            var xhttp2 = new XMLHttpRequest();
            xhttp2.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
                log('Description was added');
                showReady();
              } 
            };
            xhttp2.open('POST', '/list/_ajax/edit', true);
            xhttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            xhttp2.send('description=' + list[ready].description + '&list_id=' + list_id + '&list_item_id=' + JSON.parse(xhttp.responseText).list_item_id);
          } else {
            showReady();
          }
        }
      };
      xhttp.open('POST', '/list/_ajax/edit', true);
      xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
      xhttp.send('const=' + list[ready].const + '&list_id=' + list_id);
    }
    sendNext();
  };
  
  add_list(create_list());
};

var div = document.createElement('div');
div.setAttribute('class', 'add');
div.innerHTML = '<textarea style=\'background-color: white\'></textarea>' + 
'<br><table><tr><td>Data from .csv file:</td><td><input type="checkbox" checked="true" style="width: initial;"></td></tr>' +
'<tr><td>Add only unique elements:</td><td><input type="checkbox" style="width: initial;"></td></tr></table>' +
'<div>Insert text with id\'s in the field above and press button \'Import list\'</div>' +
'<button class=\'btn\'>Import List</button>';
div.querySelector('button').addEventListener('click',import_list,false);

var list_edit = document.querySelector('.list_edit');
var footer = document.querySelector('.footer');
list_edit.insertBefore(div, footer);