论坛抽奖规则更新

越来越规范了

河南拔智齿 :+1:

越来越规范了!!!

支持 :tieba_091: :tieba_091:

必须无条件支持

慢速是啥!!

前排支持一下


ᅟᅠ ‌‍‎‏ ᅟᅠ ‌‍‎‏

支持支持支持

支持支持 :hand_with_index_finger_and_thumb_crossed:

冲冲冲tieba_013

支持一波~~~ :bili_019:

前排支持~~

来源:https://linux.do/t/topic/103888 https://linux.do/t/topic/113457/35?u=dengdai
我抽奖是用的这个(

抽奖代码(js)
function getPosts(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var res = JSON.parse(xhr.responseText);
            callback(res.post_stream.posts);
        }
    };
    xhr.send();
}

function selectWinner(posts) {
    while (true) {
        var winner = posts[Math.floor(Math.random() * posts.length)];
        if (winner.post_number === 1) {
            console.log('抽中楼主(1楼),再抽一次!\n');
        } else {
            return winner;
        }
    }
}

function main() {
    let fullUrl = window.location.href;
    let baseUrlRegex = /^(https?:\/\/[^\/]+\/t\/topic\/\d+)/i;
    let match = fullUrl.match(baseUrlRegex);
    if (match && match[0]) {
        let baseUrl = match[0];
        let url = baseUrl + '.json?print=true';
        getPosts(url, function(posts) {
            let now = new Date();
            let year = now.getFullYear();
            let month = String(now.getMonth() + 1).padStart(2, '0');
            let date = String(now.getDate()).padStart(2, '0');
            let hours = String(now.getHours()).padStart(2, '0');
            let minutes = String(now.getMinutes()).padStart(2, '0');
            let seconds = String(now.getSeconds()).padStart(2, '0');
            let formattedTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
            console.log(`抽取时间为:${formattedTime}`)
            console.log(`一共有 ${posts.length} 个回复(含楼主,若抽中1楼,则再抽一次!)\n`);
            let winner = selectWinner(posts);
            console.log(`成功抽取第 ${winner.post_number} 楼,具体信息如下:\n`);
            console.log(winner.id, winner.display_username, winner.username, winner.cooked );
        });
}}

main();