Theme——借助github的API来实现主题自动更新

2024-08-15 12:36:55 浏览数 (2)

前言

那么借助的是那个api呢?这里是为了方便测试,使用的是tags,如果是正式的那么应该使用releases

内容

侧边公告栏

测试环境

代码语言:javascript复制
<script>
async function request(url = '', method = 'GET', data = {}, headers = {}) {
    let options = {
        method: method,
        mode: 'cors',
        redirect: 'follow',
        referrerPolicy: 'no-referrer',
    }
    if (Object.keys(headers).length) {
        options.headers = headers
    }
    if (Object.keys(data).length) {
        options.body = JSON.stringify(data)
    }
    const response = await fetch(url, options)
    return response.json()
}

 request('https://api.github.com/repos/wangyang0210/cnblogs-theme/tags').then(r => {
        const url = `https://cdn.jsdelivr.net/gh/wangyang0210/cnblogs-theme@${r[0].name}/dist/simple-memory.js`;
         $.ajax({
            type: 'GET',
            dataType: 'script',
            cache: true,
            url,
            success: function (data) {
               console.log('simple-memory request success')
            },
            error: function (err) {
                console.log('simple-memory request', err)
            },
        })
    })
</script>

正式环境

代码语言:javascript复制
<script>
async function request(url = '', method = 'GET', data = {}, headers = {}) {
    let options = {
        method: method,
        mode: 'cors',
        redirect: 'follow',
        referrerPolicy: 'no-referrer',
    }
    if (Object.keys(headers).length) {
        options.headers = headers
    }
    if (Object.keys(data).length) {
        options.body = JSON.stringify(data)
    }
    const response = await fetch(url, options)
    return response.json()
}
 request('https://api.github.com/repos/wangyang0210/cnblogs-theme/releases/latest').then(r => {
        const url = `https://npm.elemecdn.com/cnblogs-theme@${r.tag_name}/dist/simple-memory.js`;
         $.ajax({
            type: 'GET',
            dataType: 'script',
            cache: true,
            url,
            success: function (data) {
               console.log('simple-memory request success')
            },
            error: function (err) {
                console.log('simple-memory request', err)
            },
        })
    })
</script>

0 人点赞