一段简单的jquery代码,抓取抖音直播间的实时弹幕

 

代码:

第二版:使用MutationObserver API监听dom

init()
async function init() {
  let jq = null
  if (!document.querySelector('#jquery')) {
    jq = document.createElement('script')
    jq.id = 'jquery'
    jq.src = 'https://libs.baidu.com/jquery/1.10.2/jquery.min.js'
    document.body.appendChild(jq)
  } else {
    jq = document.querySelector('#jquery')
  }

  await timeout(1000)
  let 用户名0 = '', 当前弹幕0 = '';
  const listen1 = 监听指定dom变化('#island_4a5da .rXSKGskq .webcast-chatroom___items>div', () => {
    //dom发生变化后内部只执行1次
    const $所有条 = $('#island_4a5da .rXSKGskq .webcast-chatroom___item')
    const 用户名 = $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .u2QdU6ht`).text()//最后有个冒号
    const 当前弹幕 = $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .WsJsvMP9`).text()
    //二层过滤
    if ((用户名0 === 用户名 && 当前弹幕0 === 当前弹幕) || (当前弹幕 === '')) {
      return
    }
    用户名0 = 用户名
    当前弹幕0 = 当前弹幕
    console.log(用户名, 当前弹幕)
    // elog(当前弹幕)
    根据弹幕召唤怪物(用户名, 当前弹幕)
    // if (当前弹幕.includes('送出了')) {
    //   const $礼物图 = $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .WsJsvMP9 img`)
    //   const 礼物链接 = $礼物图.attr('src')
    //   // elog(`送出了礼物:${礼物链接}`)
    // } else {
    //   // elog(当前弹幕)
    // }
  })
  //如果弹出长时间无操作,已暂停播放 则自动关闭
  const listen2 = setInterval(() => {
    if ($('.ezAK2PYX').length > 0) {
      $('.ezAK2PYX .JL05k7eS').click()
    }
  }, 1000)
  // console.log(listen1)
  alert('载入脚本成功')
}

function 根据弹幕召唤怪物(用户名, 当前弹幕) {
  if (当前弹幕 === '1') {
    zhguaiwu({ zy: 1, gwcode: 7 })
  } else if (当前弹幕 === '2') {
    zhguaiwu({ zy: 2, gwcode: 8 })
  }
  // const regex = /召唤阵营(\d+)怪物(\d+)/;
  // const match = 当前弹幕.match(regex);
  // if (match) {
  //   const result = {
  //     zy: parseInt(match[1]),
  //     gwcode: parseInt(match[2]),
  //   };
  //   zhguaiwu(result)
  // }
}



/**
 * utils.js
 */
//窗口中输出
function elog(msg) {
  $.ajax({
    url: 'http://localhost:8848/api/dm',
    type: 'get',
    data: {
      msg: msg,
    },
  })
}
//召唤怪物
function zhguaiwu(data) {
  console.log(data)
  $.ajax({
    url: 'http://localhost:8848/api/guaiwu',
    type: 'get',
    data: data,
  })
}

function 监听指定dom变化(dom, callback) {
  // 创建一个MutationObserver对象
  const observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      callback()
    });
  });
  // 配置MutationObserver要观察的目标
  const target = document.querySelector(dom);
  const config = { childList: true, characterData: true };
  // 开始观察DOM的变化
  observer.observe(target, config);
  return observer;
}

//防抖
function debounce(func, time) {
  let timeout = null
  return function () {
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.apply(this, arguments)
    }, time)
  }
}
//延迟
async function timeout(t) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, t)
  })
}

 

 

第一版:使用计时器

{
  let jq = null
  if (!document.querySelector('#jquery')) {
    jq = document.createElement('script')
    jq.id = 'jquery'
    jq.src = 'https://libs.baidu.com/jquery/1.10.2/jquery.min.js'
    document.body.appendChild(jq)
  } else {
    jq = document.querySelector('#jquery')
  }
  window.timer && clearInterval(window.timer)
  setTimeout(() => {
    let 上一条弹幕 = ''
    window.timer = setInterval(() => {
      const $所有条 = $('#island_4a5da .rXSKGskq .webcast-chatroom___item')
      const 当前弹幕 = $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .WsJsvMP9`).text()
      if (当前弹幕 !== 上一条弹幕) {
        上一条弹幕 = 当前弹幕
        console.log('当前弹幕', 当前弹幕)
        $.ajax({
          url: 'http://localhost:9090/api/dm',
          type: 'get',
          data: {
            msg: 当前弹幕,
          },
        })
        if (当前弹幕.includes('送出了') && $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .WsJsvMP9 img`).length > 0) {
          console.log('礼物', $(`#island_4a5da .rXSKGskq .webcast-chatroom___item:eq(${$所有条.length - 2}) .WsJsvMP9 img`).attr('src'))
        }
      }
      //如果弹出长时间无操作,已暂停播放 则自动关闭
      if ($('.ezAK2PYX').length > 0) {
        $('.ezAK2PYX .JL05k7eS').click()
      }
    }, 100)
  }, 1000)
}

 

posted @ 2024-01-12 15:34  herry菌  阅读(223)  评论(0编辑  收藏  举报