0°

获取 B 站右上角所有小图的 JS 代码

2791 个字符,1 张图片,大约需要 4 分钟阅读

在逛博客的时候发现了这篇文章,挺有意思的就转过来了,试了试还能玩,改成https就行了

图片打包下载:https://t.cn/Rjf5xZu

使用 Chrome 浏览器打开 B 站任意网址,打开 开发者工具 -> console,粘贴下面的代码回车运行:

((url, delay) => {
    let starTime = new Date().getTime();
    let xhr = new XMLHttpRequest();
    let times = [];
    let style = document.createElement('style');
    style.textContent = 'body { display: flex; flex-wrap: wrap; } div { text-align: center; padding: 0.5rem; margin: 1rem; border: 1px solid #ddd; }';
    document.head.appendChild(style);
    document.body.innerHTML = '';
    xhr.open('GET', url, true);
    xhr.onload = event => {
        if (xhr.status >= 200 && xhr.status < 400) {
            console.debug(`资源已加载,耗时 ${new Date().getTime() - starTime} ms,开始获取...`);
            (loadImg = (i, srcs) => {
                (promise = (src, title, timeStamp) => new Promise(resolve => {
                    let img = new Image();
                    let div = document.createElement('div');
                    let p = document.createElement('p');
                    p.textContent = title;
                    img.src = src;
                    img.alt = title;
                    img.onload = newEvent => {
                        setTimeout(() => {
                            let usedTime = newEvent.timeStamp - timeStamp;
                            usedTime = Math.floor(i > 0 ? usedTime - delay : usedTime);
                            times.push(usedTime);
                            console.info(`第 ${i + 1} 张获取成功,耗时 ${usedTime} ms`, img.src);
                            div.appendChild(img);
                            div.appendChild(p);
                            document.body.appendChild(div);
                            resolve(newEvent.timeStamp);
                        }, delay);
                    };
                    img.onerror = err => {
                        console.warn(`第 ${i + 1} 张获取失败`, img.src);
                        resolve(err.timeStamp);
                    };
                }).then(timeStamp => {
                    if (i === srcs.length - 1) {
                        let min = Math.min.apply(null, times);
                        let max = Math.max.apply(null, times);
                        let avg = (times.reduce((p, c) => Number(p) + Number(c)) / times.length).toFixed(0);
                        console.debug(`图片下载完毕,最快用时 ${min} ms,最慢用时 ${max} ms,平均用时 ${avg} ms`);
                    } else {
                        i += 1;
                        promise(srcs[i].icon, srcs[i].title, timeStamp);
                    }
                }))(srcs[i].icon, srcs[i].title, event.timeStamp);
            })(0, (JSON.parse(xhr.responseText)).fix);
        }
    };
    xhr.send();
})('https://www.bilibili.com/index/index-icon.json', 100);

示例:

Fpcih.png

除非注明,否则均为喵喵喵博客原创文章,转载请以链接形式标明本文地址

本文链接:https://www.miaomiaomiao.org/754.html

0 条回复 A 作者 M 管理员
    当上帝赐给你荒野时,就意味着,他要你成为高飞的鹰
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论