Greasy Fork

Greasy Fork is available in English.

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     3.4
// @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[1];
  
  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 = [];
    var e;
    while ((e = re.exec(text)) !== null) {
      var rer = new RegExp(e[0], 'g');
      text = text.replace(rer, '');
      list.push(e[0]);
    }
    return list;
  };
  
  var add_list = function(list) {
    var length = list.length;
    var list_id = /ls[0-9]{1,}/.exec(location.href) [0];
    var ready = 0;
    log("Elements to add: " + list);
    function showReady() {
      if (this.readyState == 4 && this.status == 200) {
        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]);
      xhttp.onreadystatechange = 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] + '&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>' +
'<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);