Greasy Fork

Greasy Fork is available in English.

立创商城自动领券

立创商城自动领券~~~

当前为 2024-07-09 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         立创商城自动领券
// @version      0.2
// @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('满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} 张优惠券`)
    }
  }
})()