Greasy Fork

Greasy Fork is available in English.

立创商城自动领券

立创商城自动领券~~~

目前为 2024-07-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         立创商城自动领券
// @version      0.3
// @license      MIT
// @namespace    http://tampermonkey.net/
// @description  立创商城自动领券~~~
// @author       Clistery
// @match        https://www.szlcsc.com/huodong.html*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=szlcsc.com
// @grant        none
// ==/UserScript==

;(function () {
  'use strict'

  // 添加通知样式
  const style = document.createElement('style')
  style.textContent = `
        .notification {
            position: fixed;
            top: 50px;
            right: 20px;
            padding: 15px;
            background-color: #444;
            color: white;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            opacity: 0;
            transition: opacity 0.5s ease, right 0.5s ease;
            z-index: 9999;
            transform: translate(0%, -50%);
        }
        .notification.show {
            opacity: 1;
            top: 50%;
            right: 50px;
        }
        .notification.hide {
            opacity: 0;
            top: 50%;
            right: 20px;
        }
        #get-tickets-btn {
          background-image: linear-gradient(0deg, #558b2f, #7cb342);
          cursor: pointer;
          position: fixed;
          width: 20px;
          display: flex;
          top: 50%;
          right: 0px;
          writing-mode: vertical-lr;
          text-orientation: upright;
          padding: 5px 10px;
          z-index: 100001;
          border-radius: 10px;
          opacity: 0.7;
          box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 1px 5px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.2);
          overflow: hidden;
          transform: translate(0%, -50%);
        }
    `
  document.head.appendChild(style)

  // 显示通知函数
  function showNotification(message, duration = 3000) {
    const notification = document.createElement('div')
    notification.className = 'notification'
    notification.textContent = message
    document.body.appendChild(notification)

    // 显示通知
    setTimeout(() => {
      notification.classList.add('show')
    }, 100)

    // 隐藏通知
    setTimeout(() => {
      notification.classList.remove('show')
      notification.classList.add('hide')
      setTimeout(() => {
        document.body.removeChild(notification)
      }, 500)
    }, duration)
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', (event) => {
      loadTickets()
    })
  } else {
    loadTickets()
  }

  function loadTickets() {
    if (window.getTicketElements) {
      console.warn('ticket elements loaded!')
    }
    console.log('load ticket elements')

    let allTicketContainer = document.querySelectorAll('.coupon-item')
    if (allTicketContainer.length <= 0) {
      console.error('优惠券元素查询失败')
      return
    }

    window.getTicketElements = []

    for (let e of allTicketContainer) {
      let btn = e.querySelector('.coupon-item-btn')
      if (
        btn &&
        !btn.classList.contains('use-btn') &&
        !e.textContent.includes('新人专享') &&
        !e.textContent.includes('立即使用') &&
        e.textContent.includes('满16可用')
      ) {
        let ellipsisE = e.querySelector('.ellipsis')
        console.log(ellipsisE.textContent)
        window.getTicketElements.push({
          title: ellipsisE.textContent,
          ele: btn,
        })
      }
    }

    if (window.getTicketElements.length > 0) {
      showNotification(`可领 ${window.getTicketElements.length} 张券`)
    }
  }

  let getTicketBtn = document.createElement('div')
  getTicketBtn.innerHTML = `
      <div id="get-tickets-btn">
        自动领取优惠券
      </div>
      `
  document.body.appendChild(getTicketBtn)

  let realGetTicketsBtnE = getTicketBtn.querySelector('#get-tickets-btn')
  realGetTicketsBtnE.onmouseenter = () => {
    realGetTicketsBtnE.style.opacity = 1
  }
  realGetTicketsBtnE.onmouseleave = () => {
    realGetTicketsBtnE.style.opacity = 0.7
  }

  let isDragging = false
  let startX, startY, offsetX, offsetY

  const onMouseMove = (event) => {
    if (!isDragging) return
    console.log('onMouseMove')
    const currentX = event.clientX
    const currentY = event.clientY
    const dx = currentX - startX
    const dy = currentY - startY

    if (Math.abs(dx) > 5 || Math.abs(dy) > 5) {
      realGetTicketsBtnE.style.left = `${currentX - offsetX}px`
      realGetTicketsBtnE.style.top = `${currentY - offsetY}px`
      realGetTicketsBtnE.removeEventListener('click', window.getTickets)
    }
  }

  const onMouseDown = (event) => {
    console.log('onMouseDown')
    isDragging = true
    startX = event.clientX
    startY = event.clientY
    offsetX = startX - realGetTicketsBtnE.getBoundingClientRect().left
    offsetY = startY - realGetTicketsBtnE.getBoundingClientRect().top
    realGetTicketsBtnE.style.cursor = 'grabbing'
    document.addEventListener('mousemove', onMouseMove)
    document.addEventListener('mouseup', onMouseUp)
  }

  const onMouseUp = (event) => {
    if (isDragging) {
      console.log('onMouseUp')
      isDragging = false
      realGetTicketsBtnE.style.cursor = 'pointer'
      setTimeout(() => {
        realGetTicketsBtnE.addEventListener('click', window.getTickets)
      }, 0)
      document.removeEventListener('mousemove', onMouseMove)
      document.removeEventListener('mouseup', onMouseUp)
    }
  }

  realGetTicketsBtnE.addEventListener('mousedown', onMouseDown)
  realGetTicketsBtnE.addEventListener('click', window.getTickets)

  window.getTickets = async () => {
    if (window.getTicketElements) {
      console.log('领券咯~~~')
      for (const item of window.getTicketElements) {
        await new Promise((succ, _) => {
          showNotification(`正在领取 ${item.title}`)
          item.ele.click()
          setTimeout(() => {
            succ()
          }, 1000)
        })
      }

      showNotification(`共领取 ${window.getTicketElements.length} 张优惠券`)
    }
  }
})()