IMDB List Importer

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

目前为 2016-10-26 提交的版本。查看 最新版本

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

unsafeWindow.Neinei0k_import_list = function() {
  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 text = document.getElementById('Neinei0k-imdb-list-importer').children[0].value
    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_e = document.getElementById('Neinei0k-imdb-list-importer').children[1]
    var ready = 0
    function showReady() {
      if (this.readyState == 4 && this.status == 200) {
        ready += 1
        ready_e.innerHTML = 'Ready ' + ready + ' of ' + length + '.'
        if (ready == length)
        ready_e.innerHTML += ' Reload the page.'
      }
    }
    for (var i = 0; i < length; i++) {
      var xhttp = new XMLHttpRequest()
      console.log('Add element ' + (i + 1) + ': ' + list[i])
      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[i] + '&list_id=' + list_id)
    }
  }
  
  add_list(create_list())
}

var div = document.createElement('div')
div.setAttribute('id', 'Neinei0k-imdb-list-importer')
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\' onclick=\'Neinei0k_import_list()\'>Import List</button>'

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