Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): add docs #12

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ $ cd my-extension
$ npm run build
```

![](./screenshot/npx-create-chrome-extension.png)


1. 克隆本项目
```shell
git clone https://github.com/raojinlin/chrome-extension-react-antd-template.git
Expand All @@ -43,7 +40,24 @@ npm install
npm run build
```

3. 构建后会在```extension```生成相应的文件。在浏览器加载插件就可以运行了。
3. 插件开发

自动编译代码。编译完成后需要重新加载插件。

![Alt text](./screenshot/image.png)

- Linux或Mac
```bash
npm run start
```

- Windows
```cmd
start.bat
```


4. 构建后会在```extension```生成相应的文件。在浏览器加载插件就可以运行了。

![](./screenshot/install.png)

Expand All @@ -64,24 +78,25 @@ Popup

## 项目布局

### ```src/manifest.development.js```
开发环境的manifest配置

### ```src/manifest.production.js```
生产环境的manifest配置

### ```/src/backgorund.js```
后台脚本入口


### ```/src/content-scripts.js```

内容脚本入口

### ```/src/content-options.js```

插件配置入口

### ```/src/components/```

一些通用的react组件

### ```/src/lib```

一些通用的库,如日志、消息通信、浏览器接口相关的函数

#### ```/src/lib/message.js```
Expand Down Expand Up @@ -129,6 +144,18 @@ consoleLogger.info('message to console');

```

### ```src/config.[test|prod|local].js```
这个目录放置了插件的配置文件,test、prod、local分别表示三个不同的环境。默认使用local(development)环境。

其中,default配置表示全局配置,在test或者prod环境下可以覆盖default的配置。

#### 获取配置
```js
import { getConfig } from './lib/config';

const config = getConfig(process.env.NODE_ENV);
```

### 在content-script使用插件内的资源(文件、html、css等)

```js
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"browserslist": "^4.22.2",
"core-js": "^3.9.1",
"date-fns": "^3.0.6",
"exceljs": "^4.4.0",
"lodash": "^4.17.11",
"luxon": "^3.4.4",
"mem": "^9.0.1",
Expand Down
Binary file added screenshot/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed screenshot/npx-create-chrome-extension.png
Binary file not shown.
21 changes: 0 additions & 21 deletions src/config.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
logger: {
handler: 'http',
options: {
url: 'https://api.mywebsite.com/api/v1/clientlogs',
method: 'POST',
formatter: 'json',
},
}
}
2 changes: 2 additions & 0 deletions src/config/config.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {
}
1 change: 1 addition & 0 deletions src/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
1 change: 1 addition & 0 deletions src/config/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
4 changes: 3 additions & 1 deletion src/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { getURL } from "./lib/brower";
import Message from "./lib/message";
import {Logger, ConsoleHandler} from "./lib/logger";
import Example from './components/Example';
import { getConfig } from './lib/config';

const logger = new Logger('ContentScript', new ConsoleHandler());
const config = getConfig(process.env.NODE_ENV);
const logger = Logger.createLogger('ContentScript', config.logger.handler, config.logger.options);

function ContentScript({ }) {
return (
Expand Down
23 changes: 23 additions & 0 deletions src/lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const envNames = {
development: 'local',
test: 'test',
production: 'prod',
prod: 'prod',
default: 'default',
};


const getEnvConfig = env => {
return require(`../config/config.${envNames[env] || 'prod'}`).default;
}

/**
* 获取环境配置,使用传入的env配置覆盖default中已有的配置
* @param {string} env
* @returns
*/
export function getConfig(env) {
const envConfig = getEnvConfig(env);
const defaultConfig = getEnvConfig('default');
return Object.assign({}, defaultConfig, envConfig);
}
70 changes: 60 additions & 10 deletions src/lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { extend } from "lodash";
import moment from "moment";

const LOG_LEVEL_INFO = 'INFO';
Expand All @@ -13,6 +14,10 @@ export class AbstractLoggerHandler {


export class ConsoleHandler extends AbstractLoggerHandler {
constructor(options) {
super(options);
}

emit(msg, level='INFO') {
switch (level) {
case LOG_LEVEL_INFO:
Expand All @@ -31,18 +36,52 @@ export class ConsoleHandler extends AbstractLoggerHandler {
}
}


class LoggerFormatter {
format(loggerName, level, msg) {}
}


class DefaultFormatter extends LoggerFormatter {
format(loggerName, msg, level) {
return `${moment().format()} ${loggerName} [${level}]: ${msg}`;
}
}


class JSONFormatter extends LoggerFormatter {
format(loggerName, msg, level) {
return {
time: moment().format(),
name: loggerName,
level,
msg
}
}
}


export class HttpHandler extends AbstractLoggerHandler {
constructor(url) {
super();
this.url = url;
constructor(options) {
super(options);
this.options = options;
if (this.options.format === 'json') {
this.formatter = new JSONFormatter();
}
}

emit(msg, level) {
if (!level) {
level = 'INFO';
}

fetch(`${this.url}/${level}?message=${encodeURIComponent(msg)}`);
fetch(this.options.url, {
method: this.options.method || 'POST',
body: JSON.stringify(msg),
headers: {
'content-type': 'application/json',
}
})
}
}

Expand All @@ -61,6 +100,7 @@ export class Logger {
this.loggerName = name;
this.handler = handler;
this._debug = false;
this.formatter = new DefaultFormatter();
}

setDebug(debug) {
Expand All @@ -77,7 +117,7 @@ export class Logger {
return `${moment().format()} ${loggerName} [${level}]: ${msg}`;
}

static messagesToString(...msg) {
static messageStringify(...msg) {
return msg.map(item => {
if (item instanceof Error) {
return item;
Expand All @@ -88,22 +128,32 @@ export class Logger {
}

info(...msg) {
this.handler.emit(Logger.getFormattedMessage(this.loggerName, Logger.messagesToString(...msg), Logger.INFO), 'INFO');
this.handler.emit(this.formatter.format(this.loggerName, Logger.messageStringify(...msg), Logger.INFO), 'INFO');
}

debug(...msg) {
if (!this._debug) {
return;
}

this.handler.emit(Logger.getFormattedMessage(this.loggerName, Logger.messagesToString(...msg), Logger.DEBUG), 'DEBUG');
this.handler.emit(this.formatter.format(this.loggerName, Logger.messageStringify(...msg), Logger.DEBUG), 'DEBUG');
}

error(...msg) {
this.handler.emit(Logger.getFormattedMessage(this.loggerName, Logger.messagesToString(...msg), Logger.ERROR), 'ERROR');
this.handler.emit(this.formatter.format(this.loggerName, Logger.messageStringify(...msg), Logger.ERROR), 'ERROR');
}

static createLogger(loggerName) {
return new Logger(loggerName, new ConsoleHandler());
static createLogger(loggerName, handlerName, handlerOptions) {
const handlers = {console: ConsoleHandler, http: HttpHandler};
if (!handlerName) {
handlerName = 'console';
}

const logger = new Logger(loggerName, new handlers[handlerName](handlerOptions));
if (handlerOptions.formatter === 'json') {
logger.formatter = new JSONFormatter();
}

return logger;
}
}
5 changes: 5 additions & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@REM set NODE_OPTIONS=--openssl-legacy-provider
set ENV=development


npx webpack watch --config webpack.config.js