wsmud_api

使用于 Tampermonkey 的武神传说脚本的前置 API 库

Per 26-08-2020. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         wsmud_api
// @namespace    com.wsmud
// @version      0.0.4
// @description  使用于 Tampermonkey 的武神传说脚本的前置 API 库
// @author       sq
// @date         2020/08/24
// @modified     2020/08/26
// @match        http://*.wsmud.com/*
// @exclude      http://*.wsmud.com/news/*
// @exclude      http://*.wsmud.com/pay.html
// @homepage     https://greasyfork.org/zh-CN/scripts/409901
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js
// @run-at       document-start
// @grant        unsafeWindow
// @grant        GM_addStyle
// ==/UserScript==

(function() {

'use strict'

if (!WebSocket) return

console.green = function(log) {
  console.log(`%c${log}`, 'color:green')
}
console.orange = function(log) {
  console.log(`%c${log}`, 'color:orange')
}
console.red = function(log) {
  console.log(`%c${log}`, 'color:red')
}
unsafeWindow.console.green = console.green
unsafeWindow.console.orange = console.orange
unsafeWindow.console.red = console.red

unsafeWindow.setValue = function(key, value) {
  localStorage.setItem(key, JSON.stringify(value))
}
unsafeWindow.getValue = function(key) {
  return JSON.parse(localStorage.getItem(key))
}
unsafeWindow.cookie = function() {
  const cookies = document.cookie.split(';').reduce((accumulator, currentValue) => {
    const i = currentValue.indexOf('=')
    const name = currentValue.substr(0, i).trim()
    const value = currentValue.substr(i + 1)
    accumulator[name] = value
    return accumulator
  }, {})
  const setCookie = (name, value) => document.cookie = name + '=' + value
  const deleteCookie = name => document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
  return new Proxy(cookies, {
    set: (target, name, value) => {
      setCookie(name, value)
      return Reflect.set(target, name, value)
    },
    deleteProperty: (target, name) => {
      deleteCookie(name)
      return Reflect.deleteProperty(target, name)
    },
  })
}

unsafeWindow.addStyle = css => GM_addStyle(css)

unsafeWindow.WebSocket = function (uri) {
  unsafeWindow._websocket = new WebSocket(uri)
}
unsafeWindow.WebSocket.prototype = {
  set onopen(fn) {
    unsafeWindow._websocket.onopen = fn
  },
  set onclose(fn) {
    unsafeWindow._websocket.onclose = fn
  },
  set onerror(fn) {
    unsafeWindow._websocket.onerror = fn
  },
  set onmessage(fn) {
    unsafeWindow._onmessage = fn
    unsafeWindow._websocket.onmessage = onmessage
  },
  get readyState() {
    return unsafeWindow._websocket.readyState
  },
  send: function (command) {
    onsend(command)
  },
}

class Api {
  constructor() {
    this.version = GM_info.script.version
    this.roles = getValue('roles')
    this.id = String()
    this.name = String()
    this.state = String()
  }
  set roles(value) {
    this._roles = Object(value)
    setValue('roles', this._roles)
  }
  get roles() {
    return this._roles
  }

  ondata(data) { ondata(data) }
}

unsafeWindow.api = Vue.observable(new Api())
unsafeWindow.monitors = {}
const api = unsafeWindow.api
const monitors = unsafeWindow.monitors

unsafeWindow.addMonitor = function(type, name, callback) {
  if (!type || !name || typeof callback !== 'function') return
  if (!monitors[type]) monitors[type] = {}
  monitors[type][name] = callback.bind(api)
  console.green(`AddMonitor: type = ${type}; name = ${name};`)
}
unsafeWindow.removeMonitor = function(type, name) {
  if (!type || !name) return
  delete monitors[type][name]
  console.red(`RemoveMonitor: type = ${type}; name = ${name};`)
  api.roles = new Object(api.roles)
}

addMonitor('roles', 'RoleList', function(data) {
  if (!(data.roles instanceof Array)) return
  data.roles.forEach(item => {
    const { id, name, title } = item
    if (!api.roles[id]) api.roles[id] = {}
    api.roles[id].name = name
    api.roles[id].title = title
  })
  api.roles = Object.assign(api.roles)
})
addMonitor('login', 'Login', function(data) {
  const id = data.id
  if (api.id || !id) return
  api.id = id
  api.name = api.roles[id].name
  api.roles[id].u = cookie().u
  api.roles[id].p = cookie().p
  api.roles[id].s = cookie().s
  api.roles[id].server = ['一区', '二区', '三区', '四区', '测试'][cookie().s]
  api.roles = Object.assign(api.roles)
})

addMonitor('', '', function(data) {})

function onsend(command) {
  unsafeWindow._websocket.send(command)
}
function onmessage(event) {
  const data = event2data(event)
  ondata(data)
}
function ondata(data) {
  const type = data.type === 'dialog' ? data.dialog : data.type
  if (monitors[type]) {
    Object.keys(monitors[type]).forEach(name => {
      const callback = monitors[type][name]
      callback(data)
    })
  }
  if (data.destroyed) return
  const event = data2event(data)
  unsafeWindow._onmessage(event)
}

function event2data(event) {
  const data = event.data
  if (typeof data === 'string' && data[0] === '{') {
    try {
      return new Function('return ' + data)()
    } catch (error) {
      console.red(error)
      console.red(data)
    }
  }
  return { 'type': 'text', 'text': data }
}
function data2event(data) {
  if (!data.type) return
  if (data.type === 'text') return { data: data.text }
  else return { data: JSON.stringify(data) }
}

unsafeWindow.Vue = Vue

document.addEventListener('DOMContentLoaded', function() {

}, false)



// To be continued...
})()