Greasy Fork

Greasy Fork is available in English.

立创商城自动领券

立创商城自动领券~~~

目前为 2025-03-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         立创商城自动领券
// @version      0.10
// @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: 2.4375rem;
          line-height: .8125rem;
          font-size: .8125rem;
          white-space: pre-line;
          display: flex;
          top: 50%;
          right: 0px;
          writing-mode: vertical-lr;
          text-orientation: upright;
          padding: 10px 0;
          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%);
          user-select: none;
        }
    `
  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 is满减券(e) {
    const money = [1, 16, 26]
    for (const m of money) {
      if (e.textContent.includes(`满${m}可用`)) {
        return true
      }
    }
  }

  /**
   * @param {Element} e
   */
  function isPlus(e) {
    return e.textContent.includes('PLUS')
  }

  /**
   * @param {Element} e
   * @param {Element} btn
   */
  function is可领取(e, btn) {
    return !e.textContent.includes('新人专享') && btn.textContent.includes('立即抢券')
  }

  /**
   * @param {Element} e
   */
  function isMRO(e) {
    return e.textContent.includes('10元MRO工业品商品券')
  }

  /**
   * @param {Element} e
   */
  function is包邮券(e) {
    return (
      (e.textContent.includes('免邮') || e.textContent.includes('包邮')) &&
      !e.textContent.includes('MRO')
    )
  }

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

    let allTicketContainer = document.querySelectorAll(
      `.m-auto > div > div.flex.flex-wrap > div > div > div:nth-last-child(1)`
    )

    if (allTicketContainer.length <= 0) {
      console.warn('优惠券元素查询失败')
      return
    }

    window.getTicketElements = []

    for (let e of allTicketContainer) {
      let ellipsisE = e.querySelector('div:first-child')
      if (isPlus(e)) {
        continue
      }
      let btn = e.querySelector('button')
      if (btn) {
        if (is可领取(e, btn)) {
          if (is满减券(e) || isMRO(e) || is包邮券(e)) {
            // let ellipsisE = e.querySelector('.ellipsis')
            console.log(ellipsisE.textContent)
            window.getTicketElements.push({
              title: ellipsisE.textContent,
              ele: btn,
            })
          }
        } else if (btn.textContent.includes('立即使用')) {
          let dataset = btn.dataset
          if (dataset.url) {
            btn.innerHTML = `
            <a href="${dataset.url}" target="_blank">${btn.innerHTML}</a>
            `
            btn.dataset.isdone = true // 禁止触发点击
            delete dataset.url
          }
        }
      }
    }

    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) => {
    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) {
      isDragging = true
      realGetTicketsBtnE.style.cursor = 'grabbing'
      realGetTicketsBtnE.style.left = `${currentX - offsetX}px`
      realGetTicketsBtnE.style.top = `${currentY - offsetY}px`
      realGetTicketsBtnE.removeEventListener('click', window.getTickets)
    }
  }

  const onMouseDown = (event) => {
    console.log('onMouseDown')
    startX = event.clientX
    startY = event.clientY
    let btnRect = realGetTicketsBtnE.getBoundingClientRect()
    offsetX = startX - btnRect.left
    offsetY = startY - btnRect.top - btnRect.height / 2
    document.addEventListener('mousemove', onMouseMove)
    document.addEventListener('mouseup', onMouseUp)
  }

  const onMouseUp = (event) => {
    console.log('onMouseUp: ' + isDragging)
    realGetTicketsBtnE.style.cursor = 'pointer'
    if (isDragging) {
      isDragging = false
      setTimeout(() => {
        realGetTicketsBtnE.addEventListener('click', window.getTickets)
      }, 0)
    } else {
      setTimeout(() => {
        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('领券咯~~~')
      let hideAlert = setInterval(() => {
        let alert = document.querySelector('.common-alert-success-tip-tmpl')
        alert.style.opacity = 0
        alert.style.display = 'none'
        let mask = document.querySelector('.mask-alert')
        mask.style.opacity = 0
        mask.style.display = 'none'
      }, 10)
      for (const item of window.getTicketElements) {
        await new Promise((succ, _) => {
          showNotification(`正在领取 ${item.title}`)
          item.ele.click()
          setTimeout(() => {
            succ()
          }, 1000)
        })
      }
      clearInterval(hideAlert)

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

  // 客服
  document.querySelector("#lc-www > main > section > button").style.left = '0'

})()