Bilibili Auto Check-in with Share Function

Automatically perform Bilibili daily check-in tasks, including sharing a video, when you open www.bilibili.com

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Bilibili Auto Check-in with Share Function
// @namespace    https://www.bilibili.com/
// @version      1.5
// @description  Automatically perform Bilibili daily check-in tasks, including sharing a video, when you open www.bilibili.com
// @author        Hao
// @match        https://www.bilibili.com/
// @grant        GM_notification
// @grant        unsafeWindow
// @license GPL
// ==/UserScript==

(function() {
    'use strict';

    // Check if the script has already run today
    const lastRunDate = localStorage.getItem('bilibili_checkin_lastRunDate');
    const today = new Date().toDateString();

    if (lastRunDate === today) {
        console.log('Check-in already performed today.');
        return;
    }

    // Function to get bili_jct from the page
    function getBiliJct() {
        try {
            const bili_jct_match = document.cookie.match(/bili_jct=([^;]+)/);
            if (bili_jct_match) {
                console.log('Retrieved bili_jct from document.cookie.');
                return bili_jct_match[1];
            } else {
                console.log('bili_jct not found.');
                return null;
            }
        } catch (e) {
            console.error('Error retrieving bili_jct:', e);
            return null;
        }
    }

    const bili_jct = getBiliJct();

    if (!bili_jct) {
        console.log('bili_jct not found.');
        return;
    }

    // Function to perform the check-in task
    function performCheckIn() {
        const expAddUrl = "https://api.bilibili.com/x/vip/experience/add";
        const expAddPayload = `csrf=${bili_jct}`;

        fetch(expAddUrl, {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Origin': 'https://www.bilibili.com',
                'Referer': 'https://www.bilibili.com/'
            },
            credentials: 'include',
            body: expAddPayload
        })
        .then(response => response.json())
        .then(data => {
            console.log('AddExpResponse:', data);
            // Proceed to share a video after adding experience
            shareVideo();
        })
        .catch(err => {
            console.error('Error during experience add request:', err);
        });
    }

    // Function to share a video
    function shareVideo() {
        // Replace with the actual aid (video ID) you want to share
        const aid = '12345678'; // Example video ID
        const shareUrl = 'https://api.bilibili.com/x/web-interface/share/add';
        const sharePayload = `aid=${aid}&csrf=${bili_jct}`;

        fetch(shareUrl, {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Origin': 'https://www.bilibili.com',
                'Referer': 'https://www.bilibili.com/'
            },
            credentials: 'include',
            body: sharePayload
        })
        .then(response => response.json())
        .then(data => {
            console.log('ShareVideoResponse:', data);

            if (data.code === 0) {
                GM_notification({
                    text: 'Bilibili check-in and video share completed successfully!',
                    title: 'Success',
                    timeout: 5000
                });
            } else {
                GM_notification({
                    text: `Video share failed: ${data.message}`,
                    title: 'Error',
                    timeout: 5000
                });
            }

            // Update last run date
            localStorage.setItem('bilibili_checkin_lastRunDate', today);
        })
        .catch(err => {
            console.error('Error during video share request:', err);
        });
    }

    // Run the check-in function when the page loads
    window.addEventListener('load', performCheckIn);

})();