自从掘金开了签到领钻石,经常忘记签到,作为一个程序员怎么能实现自动签到呢,最近刚好在做浏览器插件的项目,就花了5分钟时间写了个签到插件,从此再也没漏签
一、插件基础
关于谷歌插件的基础基本配置我这里就不详细说了,有兴趣的童鞋可以参考:(chrome.cenchy.com/)
二、实现思路
1:首先我们要自己先登录掘金,因为登录状态可以保存,所以我们可以实现持续的签到,加入登录状态到期了,需要我们从新登录下。
2:登录成功的状态下,我们直接使用插件打开一个掘金的页面,然后就是对应的操作dom,点击...实现起来非常的简单
三、核心代码
manifest.json配置
{
"manifest_version": 2,
"name": "掘金插件",
"version": "1.0.0",
"description": "补充浏览器功能",
"background":
{
"page": "background.html"
},
"content_scripts":
[
{
"matches": ["<all_urls>"],
"js": ["js/jquery.js","js/content_script.js"],
"run_at": "document_start"
}
],
"permissions":
[
"cookies",
"contextMenus",
"tabs",
"<all_urls>",
"notifications",
"webRequest",
"webRequestBlocking",
"storage",
"http://*/*",
"https://*/*"
],
"omnibox": { "keyword" : "go" }
}
/**
*默认加入了jquery和content_script这两个文件会在插件的作用下添加到我们打开的窗口
/
background.js 长时间运行在浏览器上,通常会有一些核心的代码逻辑
/**
* 我这里只有两个函数,一个定时器用来记录当前时间在19点签到,因为我20点下班,另外一个函数就是打开操作窗口
/
$(document).ready(function(){
let timer = null
timer = setInterval(()=>{
const time = new Date();
const nowTime = time.getHours();
console.log(nowTime)
if(nowTime===19){
openTabs('https://juejin.cn/user/center/signin?from=avatar_menu');
}
},1000)
function openTabs(url){
chrome.tabs.create({url:url},(tab)=>{
if(tab){
chrome.tabs.executeScript(tab.id, {file: 'js/content_script.js'});
clearInterval(timer)
}
})
}
});
content_script.js 这个函数主要用来操作dom实现签到
/**
* 需要获取dom的文字
* @param {*} str
* @returns
*/
function getDom(str){
let list = document.getElementsByTagName('*')
let dom = Array.from(list).find((item)=>item.innerText===str)
return dom
}
$(document).ready(function(){
// 获取url上的信息
let nowRouter =window.location.href.split('?')[0];
// 路由判断只在当前页面下执行逻辑
if(nowRouter==='https://juejin.cn/user/center/signin'){
isLogin()
}
})
// 登录判断
function isLogin(){
const demo = document.querySelector('.login-button')
if(demo?.innerText==='登录'){
console.log(111)
// 此时未登录,需要自己实现一次登录
return
}
const qian = document.querySelector('.code-calender').childNodes[0]
if(qian.innerText==='立即签到'){
setTimeout(()=>{
qian.click()
goLottery()
},4000)
}
}
// 获取去抽奖按钮
function goLottery(){
let dom = document.querySelector('.btn-area').childNodes[0];
dom.click();
setTimeout(()=>{
lottery();
},4000)
}
// 单击抽奖
function lottery(){
let dom =document.querySelector('.lottery-text')
if(dom.innerText==='单抽'){
console.log('已经抽过奖了')
}else{
dom.parentNode.click();
}
}
基本就是这些核心的代码,是不是很简单,有了他再也不会遗漏签到,只要安装到我们工作的电脑上,到点就会自动代开窗口,实现签到了