[掘金]自动签到&自动抽奖

在用户打开掘金页面后, 自动签到, 每天最多签到一次.签到完之后自动跳抽奖页面,每天最多免费抽一次. 基于iframe实现, 不用担心接口被禁. 只支持 Chrome80+ 浏览器.

As of 2024-04-22. See the latest version.

// ==UserScript==
// @name         [掘金]自动签到&自动抽奖
// @namespace    http://tampermonkey.net/
// @version      1.0.3
// @author       mgtx
// @description  在用户打开掘金页面后, 自动签到, 每天最多签到一次.签到完之后自动跳抽奖页面,每天最多免费抽一次. 基于iframe实现, 不用担心接口被禁. 只支持 Chrome80+ 浏览器.
// @license      MIT
// @icon         https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web//static/favicons/favicon-32x32.png
// @match        https://juejin.cn/*
// @grant        none
// @run-at       document-idle
// @noframes
// ==/UserScript==
(function () {
  "use strict";
  const NAMESPACE = "juejin-auto-check";
  const LOCAL_STORAGE_KEY = "tampermonkey-" + NAMESPACE;
  const LUCKY_PAGE_PATH = 'https://juejin.cn/user/center/lottery?from=lucky_lottery_menu_bar'
  function getDate () {
    const date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
  }
  function createIframe (id) {
    const iframe = document.createElement("iframe");
    iframe.id = id;
    iframe.style.position = "fixed";
    iframe.style.top = "120px";
    iframe.style.right = "24px";
    iframe.style.width = "375px";
    iframe.style.height = "850px";
    iframe.style.zIndex = "1000";
    iframe.src = "https://juejin.cn/user/center/signin";
    return iframe;
  }
  function removeIframe (id) {
    const ele = document.getElementById(id);
    if (ele) {
      document.body.removeChild(ele);
    }
  }
  function signIn () {
    iframeFactory()
    .then( (res) => {
      const { contentDocument, id } = res || {}
      const btn = contentDocument.querySelector(".signin.btn");
      if (btn) {
        btn.click();
        lucky()
      }
      const timer = setTimeout(() => {
        clear({id, timer})
      }, 1e3);
    })
  }
  function lucky () {
    window.location.href = LUCKY_PAGE_PATH
    setTimeout(()=>{
      const btn = document.querySelector("turntable-item-0");
      if (btn) {
        btn.click();
      }
    }, 0)
  }

  function clear({id, timer}={}){
    clearTimeout(timer);
    removeIframe(id);
  }


  function iframeFactory () {
    const id = `iframe-${Math.ceil(Math.random() * 100)}`;
    const iframe = createIframe(id);
    document.body.prepend(iframe);
    return new Promise((resolve) => {
      iframe.onload = () => {
        const dialog = document.getElementById(id);
        if (dialog) {
          resolve(dialog)
        }
      }
    })
  }

  function main () {
    const latestDay = localStorage.getItem(LOCAL_STORAGE_KEY);
    const today = getDate();
    if (!latestDay || latestDay !== today) {
      try {
        signIn();
        localStorage.setItem(LOCAL_STORAGE_KEY, today);
      } catch (error) {
        localStorage.removeItem(LOCAL_STORAGE_KEY);
      }
    }
  }
  main();
})();
长期地址
遇到问题?请前往 GitHub 提 Issues。