Chrome 控制台查看喜马拉雅用户数据代码优化

原始代码:

fetch('https://www.ximalaya.com/revision/main/getCurrentUser', {
    method: 'GET',
    mode: 'cors',
    credentials: 'include'
}).then(function (response) {
    response.json().then(function (data) {
        console.log(
            '%c 用户  I D %c' + (data.data.uid)
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );
        console.log(
            '%c 用户昵称 %c' + (data.data.nickname)
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );
        console.log(
            '%c 手机账号 %c' + (data.data.mobile)
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );
        var date = new Date();
        date.setDate(date.getDate() + (data.data.vipExpireTime));
        console.log(
            '%c 到期时间 %c' + date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() +' 23:59:59'
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );
        console.log(
            '%c 剩余天数 %c' + (data.data.vipExpireTime) + ' 天'
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );
        console.log(
            '%c 使用天数 %c' + (data.data.totalVipDays) +' 天'
            , 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
            'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
        );

    });
});

优化后的代码:

const log = (label, value) => console.log(
    `%c ${label} %c ${value}`,
    'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #40404c; font-weight: bold;',
    'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #e82b24; font-weight: bold;',
);

fetch('https://www.ximalaya.com/revision/main/getCurrentUser', {
    method: 'GET',
    mode: 'cors',
    credentials: 'include'
}).then(response => response.json().then(data => {
    log('用户 ID', data.data.uid);
    log('用户昵称', data.data.nickname);
    log('手机账号', data.data.mobile);
    const date = new Date();
    date.setDate(date.getDate() + data.data.vipExpireTime);
    log('到期时间', `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 23:59:59`);
    log('剩余天数', `${data.data.vipExpireTime} 天`);
    log('使用天数', `${data.data.totalVipDays} 天`);
})); 

优化后的代码主要做了以下几个方面的改进:

  1. 使用箭头函数代替普通函数,使代码更简洁;
  2. 将重复的代码封装成函数,提高代码的可维护性;
  3. 使用模板字符串代替字符串拼接,使代码更简洁易读;
  4. 使用 const 和箭头函数代替 var 和普通函数,使代码更加规范和易读。

使用方法:

  1. 打开 Chrome 浏览器,并打开开发者工具(可以通过按 F12 键或者右键点击页面选择“检查”打开)。
  2. 切换到“控制台”选项卡。
  3. 将以上代码复制粘贴到控制台并回车执行。

注意:

  • 该代码需要在登录喜马拉雅账号的情况下才能正常运行。
  • 该代码只展示了部分用户数据,更多用户数据可以通过查看 data 对象获取。
  • 该代码仅供参考,请根据实际情况进行修改。

希望这篇文章对您有所帮助。如果您有任何问题,请随时在评论区留言。

Chrome 控制台查看喜马拉雅用户数据代码优化

原文地址: https://www.cveoy.top/t/topic/lHWA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录