wsmud_api

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

目前為 2020-08-26 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(function() {

'use strict'

if (!WebSocket) return

unsafeWindow.console.green = function(log) {
  console.log(`%c${log}`, 'color:green')
}
unsafeWindow.console.orange = function(log) {
  console.log(`%c${log}`, 'color:orange')
}
unsafeWindow.console.red = function(log) {
  console.log(`%c${log}`, 'color: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.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
  }
}

const api = Vue.observable(new Api())
unsafeWindow.api = api

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


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

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 = Object.assign(api.roles)
})

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


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