Greasy Fork

来自缓存

Greasy Fork is available in English.

旋转的五分硬币排队

旋转的五分硬币直播间深渊排队脚本

当前为 2021-12-02 提交的版本,查看 最新版本

// ==UserScript==
// @name         旋转的五分硬币排队
// @namespace    http://tampermonkey.net/
// @version      0.0.4
// @description  旋转的五分硬币直播间深渊排队脚本
// @author       Mimiko
// @license      MIT
// @match        *://live.bilibili.com/3140454*
// @match        *://live.bilibili.com/743364*
// @icon         http://i0.hdslb.com/bfs/activity-plat/static/20211202/dddbda27ce6f43bf18f5bca141752a99/fCo7evLooK.webp@128w
// @grant        GM_xmlhttpRequest
// ==/UserScript==
// With lots of thx to Rexze
// http://greasyfork.icu/en/scripts/436443-%E6%97%8B%E8%BD%AC%E7%9A%84%E4%BA%94%E5%88%86%E7%A1%AC%E5%B8%81%E6%8E%92%E9%98%9F

(() => {

  if (window.top !== window.self) return

  // variable
  const cacheAdd = new Set()
  let cacheSearch = ''
  const port = 9644
  const speaker = new SpeechSynthesisUtterance()

  // function

  const add = async (
    name,
  ) => {
    if (cacheAdd.has(name)) return
    cacheAdd.add(name)
    const data = await get(`http://localhost:${port}/add?name=${name}`)
    if (!data) return
    speak(data.message)
  }

  const get = (
    url,
  ) => new Promise(resolve => {
    GM_xmlhttpRequest({
      method: 'GET',
      url,
      onError: () => resolve(null),
      onload: (response) => resolve(JSON.parse(response.responseText)),
    })
  })

  const main = () => {
    window.setInterval(pick, 1e3)
    window.setTimeout(pauseVideo, 1e3)
  }

  const pauseVideo = () => {
    const $video = document.querySelector('video')
    if (!$video) return
    $video.pause()
  }

  const pick = () => {

    const $list = Array.from(document.querySelectorAll('.chat-history-panel .danmaku-item')).reverse()

    for (const $danmaku of $list) {

      const content = $danmaku.getAttribute('data-danmaku').trim()
      const name = $danmaku.getAttribute('data-uname').trim()

      if (content === '排队') {
        add(name)
        break
      }

      if (content === '查询排队') {
        search(name)
        break
      }
    }
  }

  const search = async (
    name,
  ) => {
    console.log(name, cacheSearch)
    if (cacheSearch === name) return
    cacheSearch = name
    const data = await get(`http://localhost:${port}/search?name=${name}`)
    if (!data) return
    speak(data.message)
  }

  const speak = (
    message,
  ) => {
    speaker.text = message
    window.speechSynthesis.speak(speaker)
  }

  // execute
  main()

})()