# console-listener

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://www.lujiahao.com/sponsor)
[![npm](https://img.shields.io/npm/v/console-listener)](https://www.npmjs.com/package/console-listener)
[![cdn version](https://data.jsdelivr.com/v1/package/npm/console-listener/badge)](https://www.jsdelivr.com/package/npm/console-listener)
[![codecov](https://codecov.io/gh/ajiho/console-listener/graph/badge.svg?token=G2P1AI238H)](https://codecov.io/gh/ajiho/console-listener)
[![Test](https://img.shields.io/github/actions/workflow/status/ajiho/console-listener/test.yml?label=Unit%20Test&branch=main)](https://github.com/ajiho/console-listener/actions/workflows/test.yml)

---

一个用于监听和处理控制台方法调用的轻量级(minified and brotlied: <= 345B)实用程序。

## 安装

- [npm](https://www.npmjs.com/package/console-listener): `npm install console-listener --save`
- CDN
  ```html
  <script src="https://unpkg.com/console-listener@latest/dist/console-listener.browser.min.js"></script>
  <!-- or -->
  <script src="https://cdn.jsdelivr.net/npm/console-listener@latest/dist/console-listener.browser.min.js"></script>
  ```

## 用法

```js
listener.on("log", (message) => {
  console.log(message)
})

// or
listener.on((methodName, ...args) => {
  console.log(`Method: ${methodName}, Args:`, args)
})

// or
listener.on({
  log: (message) => console.log("Log:", message),
  warn: (message) => console.warn("Warning:", message),
  all: (methodName, ...args) =>
    console.log(`Method: ${methodName}, Args:`, args),
})

// You can call the methods on the console object normally
console.log("this is log")
console.warn("this is warn")
```

## 浏览器支持

具体可以查看[.browserslistrc](https://github.com/ajiho/console-listener/blob/main/.browserslistrc)文件。

## 变更日志

每个版本的详细更改记录在[CHANGELOG.md](https://github.com/ajiho/console-listener/blob/main/CHANGELOG.md)中.

## License

[MIT](https://github.com/ajiho/console-listener/blob/master/LICENSE)

Copyright (c) 2025-present, ajiho
