121《Console Importer》在Chrome直接使用npm军火库, 在控制台动态展示一张猫猫图

---
title: 121《Console Importer》在Chrome直接使用npm军火库, 在控制台动态展示一张猫猫图
---

最近发现了一个很不错的开发者扩展程序《Console Importer》, 让javascript程序员们,可以直接在浏览器快速安装各种好用的npm依赖包(npm包的丰富程度堪称军火库),并进行编程。


安装完成《Console Importer》后,想要使用《Console Importer》开始编程,首先要打开**开发者工具**,下图以Chrome浏览器打开**开发者工具**为例

![](https://cdn.fangyuanxiaozhan.com/assets/1703762115407cczZTTYf.png)


## 使用day.js 获取当前年月日


$i('dayjs')
const currentTime = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')

console.log(
This is %c ${currentTime},
"color: yellow; font-style: italic; background-color: blue;padding: 2px",
);


![](https://cdn.fangyuanxiaozhan.com/assets/1703757766204bEKYdmJ2.png)计算时间是经典功能了,如果想折腾练手,可以写一个类似下图的世界时钟,shell版本的参考代码 https://github.com/zhaoolee/WelcomeMrStark

![image-20231228190913056](https://cdn.fangyuanxiaozhan.com/assets/1703761753907aCR3zntB.png)

## 请求一言的api, 动态获取一句话

api能为程序带来无限可能,以下是请求一言api的的示例代码

$i('axios')
axios('https://v1.hitokoto.cn').then((res)=>{console.log(res.data)})


![image-20231228190336516](https://cdn.fangyuanxiaozhan.com/assets/1703761417247cm5zxNyr.png)

## 请求一张猫猫图,转换为base64, 并展示到Console 控制台

如果你感觉只是展示文字不够有趣,可以参考以下代码,通过Console 打印出图片。(Chrome早期版本可以直接通过图片url展示图片,但新版本Chrome只能把图片下载转换为base64, 才可以展示)



i('axios');i('crypto-js');

axios.get('https://placekitten.com/200/300', { responseType: 'arraybuffer' })
.then((response) => {
// Convert the ArrayBuffer to WordArray
const wordArray = CryptoJS.lib.WordArray.create(response.data);
// Convert the WordArray to a base64 string
const base64String = CryptoJS.enc.Base64.stringify(wordArray);
const imageUrl = data:image/jpeg;base64,${base64String}; // Assuming the image is a JPEG
// Create a new image and assign the base64 string as the source
const image = new Image();
image.onload = function() {
// Once the image is loaded, use its dimensions to set the console padding
const style = [
'font-size: 1px;',
padding: ${this.naturalHeight / 2}px ${this.naturalWidth / 2}px;, // Divide by 2 because of the 1px font-size trick
background: url(${imageUrl}) no-repeat;,
'background-size: contain;'
].join(' ');
console.log('%c ', style);
};
image.src = imageUrl;
})
.catch((error) => {
console.error('Error fetching or encoding the image:', error);
});




![Console控制台打印图片](https://cdn.fangyuanxiaozhan.com/assets/1703761331008zP4hFn3a.gif)



## Console Importer开源地址:

https://github.com/pd4d10/console-importer

## 《Console Importer》 下载链接


<table style="table-layout: fixed;">
<tbody>
<tr>
<td><div style="text-align: center;"><div style="font-weight: bold">Chrome</div><br/><div style="text-align: center;"><img  style="width:50px; height:auto;" src="https://v2fy.com/asset/0i/ChromeAppHeroes/page/001_markdown_here.assets/chromeappheroes-chrome-icon.png"/></div></div></td>
<td><div style="text-align: center;" ><div style="font-weight: bold">Edge</div><br/><div><img style="width:50px; height:auto;" src="https://v2fy.com/asset/0i/ChromeAppHeroes/page/001_markdown_here.assets/chromeappheroes-edge-icon.png"/></div></div></td>
<td><div style="text-align: center;" ><div style="font-weight: bold">FireFox</div><br/><div style="text-align: center;"><img  style="width:50px; height:auto;" src="https://v2fy.com/asset/0i/ChromeAppHeroes/page/001_markdown_here.assets/chromeappheroes-firefox-icon.png"/></div></div></td>
<td><div style="text-align: center;" ><div style="font-weight: bold">离线安装包</div><br/><div style="text-align: center;"><img  style="width:50px; height:auto;" src="https://v2fy.com/asset/0i/ChromeAppHeroes/page/001_markdown_here.assets/chromeappheroes-github-download.png"/></div></div></td>
</tr>
<tr>
<td>
<div style="text-align: center;">
<a  href="https://chromewebstore.google.com/detail/console-importer/hgajpakhafplebkdljleajgbpdmplhie">下载链接 / Download link</a>
</div>
</td>
<td>
<div style="text-align: center;">
暂无
</div>
</td>
<td>
<div style="text-align: center;">
暂无
</div>
</td>
<td>
<div style="text-align: center;"><a  href="https://cdn.jsdelivr.net/gh/zhaoolee/ChromeAppHeroes/backup/121-console-importer.zip">下载链接 / Download link</a></div>
</td>
</tbody>
</table>


## 小结

Chrome是理想的计算单元,任何设备装上浏览器,都能跑复杂的程序。

有了《Console Importer》这种好用的工具,我感觉《用Chrome学编程》是一个值得研究的工程。

《Console Importer》会让Web工程师感觉很爽,但项目本身还有一些需要完善的点,我认为作者可以添加卸载npm包的功能,对于国内的程序员而言,允许设置npm软件源也是刚需。




## 写在最后(我需要你的支持) / At the end (I need your support)

- 本文属于**Chrome插件英雄榜** 项目的一部分, 项目Github地址: [https://github.com/zhaoolee/ChromeAppHeroes](https://github.com/zhaoolee/ChromeAppHeroes)


- This article is part of the **ChromeAppHeroes** project. Github link : [https://github.com/zhaoolee/ChromeAppHeroes](https://github.com/zhaoolee/ChromeAppHeroes) 

- **Chrome插件英雄榜**, 为优秀的Chrome插件写一本中文说明书, 让Chrome插件英雄们造福人类, 如果你喜欢这个项目, 希望你能为本项目添加一颗 🌟星.

- ChromeAppHeroes, Write a Chinese manual for the excellent Chrome plugin, let the Chrome plugin heroes benefit the human, If you like this project, I hope you can add a star 🌟 to this project.
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,585评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,923评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,314评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,346评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,718评论 3 291
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,828评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,020评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,758评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,486评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,722评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,196评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,546评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,211评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,132评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,916评论 0 200
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,904评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,758评论 2 274

推荐阅读更多精彩内容