阅读 298

掘金整活小插件【每日自动签到 | 免费抽奖 | 矿石清空计划】

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

本文评论区已加入抽奖豪华套餐,详情见文末~

自动完成每日任务

在掘金的站点下访问任意资源,都会触发每日任务检测,如果检测到未做,则后台自动请求相关连接来完成这些日常任务 【指签到+白嫖抽奖】,顺带弹窗提示,再自动关闭

image.png

如果已经完成日活,则在控制台打印一下信息

image.png

抽奖功能整合

在页面的最上一列,添加了一个神奇的按钮,欧皇鉴定器

image.png

点击后,会在弹窗上,会一条条累加显示抽奖结果,效果如下:

image.png

等抽奖完成后,会显示抽奖总汇结果,同时会输出到控制台:

image.png

使用方式

我就默认大伙都是已经安装了油猴插件的,直接 左键插件图标 -> 添加新脚本,然后将后面的代码整个复制粘贴上去即可

image.png

源码

注:现在本人还在研究如何自动获取到 aid 和 uuid,所以脚本里都是直接写死的,如果使用有问题,建议开控制台看下自己的请求里携带的 aid 及 uuid,然后替换掉第一行的 userInfo 变量

image.png

// ==UserScript==
// @name         Juejin Activities Enhancer
// @name:zh-CN   掘金日常一键操作
// @version      0.1
// @description:zh-CN
// @match        https://juejin.cn/**
// @run-at       document-end
// ==/UserScript==
{
  let userInfo = 'aid=2608&uuid=7016487434494658063';
  let httpConfig = {
    "headers": {
      "accept": "*/*",
      "accept-language": "zh-CN,zh;q=0.9",
      "content-type": "application/json",
      "sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Windows\"",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site"
    },
    "referrer": "https://juejin.cn/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "{}",
    "method": "POST",
    "mode": "cors",
    "credentials": "include"
  };
  let run = (function() {
    let lastUrl;
    let clickTime;
    return {
      once: () => {
        clearTimeout(clickTime);
        clickTime = setTimeout(() => {
          if (lastUrl != unsafeWindow.location.href) {
            lastUrl = unsafeWindow.location.href;
            console.log('reload by setInterval', lastUrl);
            business(unsafeWindow.$)
          }
        }, 500)
      },
      setLastUrl: (url) => {
        lastUrl = url;
      }
    }
  })();
  let htmlInDoc = (function() {
    return {
      appendDialog: () => {
        let html =
          `<div id="lotteryResultDialog" style="position: absolute;z-index: 9999;width: 200px;height: 200px;left: calc(50vw - 100px);
            top: calc(30vh - 100px);background: #fff;border: 1px solid;border-radius: 5px;padding: 10px;overflow: auto;"></div>`;
        $('body').append(html);
      },
      appendRecord: (msg) => {
        let html = `<div style="padding: 5px;font-size: 16px;">${msg}</div>`;
        $('#lotteryResultDialog').prepend(html);
      },
      closeDialog: (time = 1200) => {
        setTimeout(() => $('#lotteryResultDialog').remove(), time)
      },
      empty: () => {
        $('#lotteryResultDialog').empty();
      }
    }
  })();
  // 用于加载 jq
  function loadScript(src, callback) {
    var script = document.createElement('script'),
      head = document.getElementsByTagName('head')[0];
    script.type = 'text/javascript';
    script.charset = 'UTF-8';
    script.src = src;
    if (script.addEventListener) {
      script.addEventListener('load', function() {
        callback();
      }, false);
    } else if (script.attachEvent) {
      script.attachEvent('onreadystatechange', function() {
        var target = window.event.srcElement;
        if (target.readyState == 'loaded') {
          callback();
        }
      });
    }
    head.appendChild(script);
  }
  // 延迟调用,确保html渲染的差不多
  setTimeout(() => {
    if (typeof jQuery == 'undefined') {
      loadScript('https://code.jquery.com/jquery-3.1.1.min.js', () => {});
    }
    let loadTime;
    loadTime = setInterval(() => { // 轮询检查 jq 载入
      if (typeof jQuery != 'undefined') {
        clearInterval(loadTime);
        business(unsafeWindow.$)
        // 当页面点击时,延迟检测页面 url 变化
        run.setLastUrl(unsafeWindow.location.href);
        $(document).on('click', '*', function() {
          run.once();
        })
      }
    }, 1000);
  }, 500)

  function business($) {
    async function dayTask() {
      console.log('调用日活函数')
      let msgs = [];
      let freeLottery = true;
      // 签到
      await fetch(
          `https://api.juejin.cn/growth_api/v1/check_in?${userInfo}&_signature=_02B4Z6wo00101q966EAAAIDCL3gSAGCq5SKvfuzAAMrJs3JkiZSLejXKJy5lR-3Rot9hYdZVnmHKrdQPh0MmwDCQsjT9tEIN0G3uIK8RsU7pcEznZ9.oqqfseed8PMV.rul6lxG-dkqFtwsq61`,
          httpConfig)
        .then(function(response) {
          return response.json();
        }).then(data => {
          if (data.err_msg == 'success') {
            msgs.push('签到成功!');
          } else {
            freeLottery = false;
            msgs.push(data.err_msg);
          }
          console.log(data);
        });
      if (!freeLottery) return false; // 没有免费抽奖就退出
      await fetch(`https://api.juejin.cn/growth_api/v1/lottery/draw?${userInfo}`, httpConfig)
        .then(function(response) {
          return response.json();
        }).then(data => {
          if (data.err_msg == 'success') {
            msgs.push(`获得奖品 ${data.data.lottery_name}`);
          }
          console.log(data);
        });
      htmlInDoc.appendDialog();
      htmlInDoc.appendRecord(msgs.join('<br>'));
      htmlInDoc.closeDialog(1200);
    }
    unsafeWindow.choujiang = async function choujiang() {
      console.log('调用抽奖函数')
      let obj = {};
      let flag = true;
      htmlInDoc.appendDialog();
      for (; flag;) {
        await fetch(`https://api.juejin.cn/growth_api/v1/lottery/draw?${userInfo}`, httpConfig)
          .then(function(response) {
            return response.json();
          }).then(data => {
            if (data.err_msg == 'success') {
              let name = data.data.lottery_name;
              console.log(name);
              htmlInDoc.appendRecord(name)
              obj[name] = obj[name] || 0;
              obj[name] = obj[name] + 1;
            } else {
              flag = false;
            }
          })
        console.log(obj)
      }
      let keys = Object.keys(obj);
      htmlInDoc.empty();
      htmlInDoc.appendRecord(keys.length > 0 ? keys.map(k => k + ': ' + obj[k]).join('<br>') : '抽奖次数不足!')
      htmlInDoc.closeDialog(1200);
      return false;
    }
    let $menu = $('.nav-list>.main-nav-list>ul');
    // $menu.find('li:last').remove();
    $menu.append(`<li class="nav-item link-item" data-v-e29b3e60=""><a href="javascript:void(0)" data-v-e29b3e60="" onclick="choujiang()" 
      style="font-weight:bolder;color: #f00a;font-family: cursive;">血统鉴定</a></li>`);
    // 延迟执行
    setTimeout(() => {
      // 检测判断是否需要自动执行签到及免费抽奖
      let val = unsafeWindow.localStorage.juejinDayTask;
      let date = new Date();
      let currDate = [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-');
      if (val == null || val != currDate) {
        dayTask();
        unsafeWindow.localStorage.juejinDayTask = currDate;
      } else {
        console.log('已经签到 + 抽奖')
      }
    }, 2000)
  }
}
复制代码

觉得 nice 的 xdm 来评论区留言支持一下丫~

欢迎在评论区讨论,掘金官方将在掘力星计划活动结束后,在评论区抽送100份掘金周边,抽奖详情见活动文章

文章分类
前端
文章标签