DD@Browser

DD@Home, Browser plugin

目前為 2020-05-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         DD@Browser
// @namespace    https://vtbs.moe/
// @version      0.1
// @description  DD@Home, Browser plugin
// @author       simon3000
// @include      *://www.bilibili.com*
// @include      *://live.bilibili.com*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==

const INTERVAL = 5000

const log = (...message) => console.log('DD@Browser:', ...message)
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
const set = (key, value) => GM_setValue(key, value)
const get = (key, d) => GM_getValue(key, d)

const id = Date.now()

let on = false

const runtime = () => {
  const uas = navigator.userAgent.split(' ')
  const ua = uas.filter(a => a.includes('/')).map(a => a.split('/')).reduce((o, [k, v]) => {
    o[k] = v
    return o
  }, {})
  if (ua.Chromium) {
    return `Chromium/${ua.Chromium}`
  }
  if (ua.Chrome) {
    return `Chrome/${ua.Chrome}`
  }
  if (ua.Safari) {
    return `Safari/${ua.Version}`
  }
  return uas[uas.length - 1]
}

const makeURL = () => {
  const url = new URL('wss://cluster.vtbs.moe')
  url.searchParams.set('runtime', runtime())
  url.searchParams.set('version', 0.1)
  url.searchParams.set('platform', navigator.platform)

  return url
}

const parse = string => {
  if (string === 'wait') {
    return { empty: true }
  }
  return JSON.parse(string)
}

const start = () => new Promise(resolve => {
  log('Start')
  const ws = new WebSocket(makeURL())

  ws.onclose = e => {
    log('Close', e.code)
    resolve()
  }

  ws.onmessage = async ({ data: message }) => {
    const { key, data } = parse(message)
    if (data) {
      const { type, url } = data
      if (type === 'http') {
        log('job received', url)
        const time = Date.now()
        const result = await fetch(url)
        const text = await result.text()
        ws.send(JSON.stringify({
          key,
          data: text
        }))
        log(`job complete ${((Date.now() - time) / 1000).toFixed(2)}s`)
      }
    }
  }

  ws.onopen = async () => {
    log('WebSocket Open')
    while (ws.readyState === 1) {
      ws.send('DDDhttp')
      await wait(780)
    }
  }
})

const open = async () => {
  log('open')
  while (true) {
    await start()
    await wait(1000)
  }
}

setInterval(() => {
  const currentClock = get('now', 0)
  const diff = Date.now() - currentClock
  if (diff > INTERVAL * 3) {
    set('me', id)
    if (!on) {
      open()
      on = true
    }
  }
  if (get('me', 0) === id) {
    set('now', Date.now())
  }
}, INTERVAL)

log('hi')