IMDB List Importer

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

目前為 2016-10-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

unsafeWindow.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
}

unsafeWindow.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)
  }
}

var field = document.createElement('textarea')
field.style.backgroundColor = 'white'

var readyText = document.createElement('div')
readyText.innerHTML = "Insert text with id's in the field above and press button 'Import list'"

var button = document.createElement('button')
button.setAttribute('class', 'btn')
button.setAttribute('onclick', 'add_list(create_list())')
button.textContent = 'Import list'

var div = document.createElement('div')
div.setAttribute('id','Neinei0k-imdb-list-importer')
div.setAttribute('class', 'add')
div.appendChild(field)
div.appendChild(readyText)
div.appendChild(button)

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