自动更改生日

名字后面加蛋糕!

目前为 2024-03-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         自动更改生日
// @namespace    https://linux.do
// @version      0.0.2
// @description  名字后面加蛋糕!
// @author       DengDai
// @match        https://linux.do/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=linux.do
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant        none
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';
    // 获取当前日期
    const currentDate = new Date().toLocaleDateString();
    // 检查本地存储中是否已经记录了今天的日期
    let lastExecutedDate = localStorage.getItem('lastExecutedDate');
    // 如果本地存储中的日期与当前日期不相等,或者本地存储中不存在日期项,说明今天还没执行过
    if (!lastExecutedDate || lastExecutedDate !== currentDate) {
            let now = new Date();
            let month = now.getMonth() + 1;
            let day = now.getDate();
            let date_of_birth = `2004-${month}-${day}`;
            let old_birthdate = "";
            let csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
            let username = document.getElementsByClassName("header-dropdown-toggle current-user")[0].querySelector("button").getAttribute("href").replace("/u/","");
            let headers = {
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
                "cache-control": "no-cache",
                "content-type": "application/json; charset=utf-8",
                "discourse-logged-in": "true",
                "discourse-present": "true",
                "pragma": "no-cache",
                "x-csrf-token": csrfToken,
                "x-requested-with": "XMLHttpRequest"
            }
            fetch(`https://linux.do/u/${username}.json`, {
                "headers": headers,
                "method": "GET",
                "mode": "cors",
                "credentials": "include"
            })
                .then(response => response.json())
                .then(data => {
                console.log(data.user.birthdate);
                old_birthdate = data.user.birthdate;
                if ((parseInt(old_birthdate.split("-")[1]) == month)&&(parseInt(old_birthdate.split("-")[2]) == day)){
                    console.log("无需修改!");
                }else{
                    headers['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
                    fetch(`https://linux.do/u/${username}.json`, {
                        "headers": headers,
                        "body": `date_of_birth=${date_of_birth}`,
                        "method": "PUT",
                        "mode": "cors",
                        "credentials": "include"
                    })
                        .then((response) => {
                        console.log("已经修改!");
                    })
                        .catch((err) => {
                        console.log('Error:', err);
                    });
                }
                // 记录当前日期到本地存储中,表示今天已经执行过
                localStorage.setItem('lastExecutedDate', currentDate);
            })
                .catch((err) => {
                console.log('Error:', err);
            });
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址