mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-05-02 23:08:41 +00:00
37 lines
924 B
TypeScript
37 lines
924 B
TypeScript
|
import * as PluginMsg from "@/core/plugin-msg";
|
||
|
import {PluginEvent} from "@/devtools/type";
|
||
|
|
||
|
class ConnectBackground {
|
||
|
connect: chrome.runtime.Port | null = null;
|
||
|
|
||
|
constructor() {
|
||
|
if (chrome && chrome.runtime) {
|
||
|
this.connect = chrome.runtime.connect({name: PluginMsg.Page.Devtools});
|
||
|
this.connect.onDisconnect.addListener(() => {
|
||
|
console.log(`%c[Connect-Dis]`, 'color:red;')
|
||
|
this.connect = null;
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
onBackgroundMessage(cb: Function) {
|
||
|
if (this.connect) {
|
||
|
this.connect.onMessage.addListener((event, sender) => {
|
||
|
console.log(`[Message] ${JSON.stringify(event)}`);
|
||
|
cb && cb(event, sender)
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
postMessage(data: PluginEvent) {
|
||
|
if (this.connect) {
|
||
|
this.connect.postMessage(data)
|
||
|
} else {
|
||
|
console.warn('没有和background建立链接')
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export const connectBackground = new ConnectBackground();
|