home.js 3.8 KB
Newer Older
A
acgotaku 已提交
1 2 3 4 5 6 7
import Core from './lib/core'
import UI from './lib/ui'
import Downloader from './lib/downloader'

class Home extends Downloader {
  constructor () {
    const search = {
A
acgotaku 已提交
8 9 10 11
      aid: 1,
      limit: 1000,
      show_dir: 1,
      cid: ''
A
acgotaku 已提交
12 13 14
    }
    const listParameter = {
      search,
A
acgotaku 已提交
15
      url: `//webapi.115.com/files?`,
A
acgotaku 已提交
16 17 18 19 20 21 22 23
      options: {
        credentials: 'include',
        method: 'GET'
      }
    }
    super(listParameter)
    this.context = document.querySelector('iframe[rel="wangpan"]').contentDocument
    UI.init()
A
acgotaku 已提交
24
    UI.addMenu(this.context.querySelector('#js_upload_btn'), 'beforebegin')
25 26
    UI.addContextMenuRPCSectionWithCallback(() => {
      this.addContextMenuEventListener()
27
    })
A
acgotaku 已提交
28 29 30 31 32 33 34
    Core.requestCookies([{ url: 'http://115.com/', name: 'UID' }, { url: 'http://115.com/', name: 'CID' }, { url: 'http://115.com/', name: 'SEID' }])
    Core.showToast('初始化成功!', 'inf')
    this.mode = 'RPC'
    this.rpcURL = 'http://localhost:6800/jsonrpc'
  }

  startListen () {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    const exportFiles = (files) => {
      files.forEach((item) => {
        if (item.isdir) {
          this.addFolder(item)
        } else {
          this.addFile(item)
        }
      })
      this.start(Core.getConfigData('interval'), (fileDownloadInfo) => {
        if (this.mode === 'RPC') {
          Core.aria2RPCMode(this.rpcURL, fileDownloadInfo)
        }
        if (this.mode === 'TXT') {
          Core.aria2TXTMode(fileDownloadInfo)
          document.querySelector('#textMenu').classList.add('open-o')
        }
      })
    }

A
acgotaku 已提交
54
    window.addEventListener('message', (event) => {
55 56 57 58 59
      const type = event.data.type
      if (!type) {
        return
      }
      if (type === 'selected' || type === 'hovered') {
A
acgotaku 已提交
60 61 62
        this.reset()
        const selectedFile = event.data.data
        if (selectedFile.length === 0) {
A
acgotaku 已提交
63
          Core.showToast('请选择一下你要保存的文件哦', 'war')
A
acgotaku 已提交
64 65
          return
        }
66
        exportFiles(selectedFile)
A
acgotaku 已提交
67 68
      }
    })
A
acgotaku 已提交
69
    const menuButton = this.context.querySelector('#aria2List')
A
acgotaku 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    menuButton.addEventListener('click', (event) => {
      const rpcURL = event.target.dataset.url
      if (rpcURL) {
        this.rpcURL = rpcURL
        this.getSelected()
        this.mode = 'RPC'
      }
      if (event.target.id === 'aria2Text') {
        this.getSelected()
        this.mode = 'TXT'
      }
    })
  }

84
  addContextMenuEventListener () {
85 86 87 88 89 90 91 92 93 94 95
    const section = this.context.querySelector('#more-menu-rpc-section')
    section.addEventListener('click', (event) => {
      const rpcURL = event.target.dataset.url
      if (rpcURL) {
        this.rpcURL = rpcURL
        this.getHovered()
        this.mode = 'RPC'
      }
    })
  }

A
acgotaku 已提交
96 97
  getSelected () {
    window.postMessage({ type: 'getSelected' }, location.origin)
98 99 100
  }
  getHovered () {
    window.postMessage({ type: 'getHovered' }, location.origin)
A
acgotaku 已提交
101
  }
A
acgotaku 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  getFile (file) {
    const options = {
      credentials: 'include',
      method: 'GET'
    }
    return new Promise((resolve) => {
      fetch(`//webapi.115.com/files/download?pickcode=${file}`, options).then((response) => {
        if (response.ok) {
          response.json().then((data) => {
            resolve(data)
          })
        } else {
          console.log(response)
        }
      }).catch((err) => {
        Core.showToast('网络请求失败', 'err')
        console.log(err)
      })
    })
  }
A
acgotaku 已提交
122
  getFiles (files) {
A
acgotaku 已提交
123 124 125 126 127 128 129 130 131 132 133
    const list = Object.keys(files).map(item => this.getFile(item))
    return new Promise((resolve) => {
      Promise.all(list).then((items) => {
        items.forEach((item) => {
          this.fileDownloadInfo.push({
            name: files[item.pickcode].path + item.file_name,
            link: item.file_url,
            sha1: files[item.pickcode].sha1
          })
        })
        resolve()
A
acgotaku 已提交
134
      })
A
acgotaku 已提交
135
    })
A
acgotaku 已提交
136 137 138 139 140 141
  }
}

const home = new Home()

home.startListen()