38 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-01-06 19:46:38 +08:00
import { debugLog, Msg, Page, PluginEvent } from "../../core/types";
import { GA_EventName } from "../../ga/type";
import { DocumentEvent, GoogleAnalyticsData } from "../const";
2024-12-27 14:14:38 +08:00
import { Terminal } from "../terminal";
export class InjectEvent {
protected terminal = new Terminal("Inject ");
constructor() {
document.addEventListener(DocumentEvent.Content2Inject, (event: CustomEvent) => {
const pluginEvent: PluginEvent = PluginEvent.create(event.detail);
2025-01-06 19:46:38 +08:00
debugLog && console.log(...this.terminal.chunkMessage(pluginEvent.toChunk()));
2024-12-27 14:14:38 +08:00
this.onMessage(pluginEvent);
});
}
onMessage(data: PluginEvent) {}
sendMsgToContent(msg: Msg, data: any) {
const detail = new PluginEvent(Page.Inject, Page.Content, msg, data);
2025-01-06 19:46:38 +08:00
debugLog && console.log(...this.terminal.chunkSend(detail.toChunk()));
2024-12-27 14:14:38 +08:00
const event = new CustomEvent(DocumentEvent.Inject2Content, { detail });
document.dispatchEvent(event);
}
2025-01-12 17:57:02 +08:00
sendEngineVersion(version: string) {
const detail = version;
const event = new CustomEvent(DocumentEvent.EngineVersion, { detail });
document.dispatchEvent(event);
}
sendAppVersion(version: string) {
const detail = { event: GA_EventName.AppVersion, params: version } as GoogleAnalyticsData;
const event = new CustomEvent(DocumentEvent.GoogleAnalytics, { detail });
document.dispatchEvent(event);
}
2025-02-07 14:23:05 +08:00
sendUrl(url: string) {
const detail = { event: GA_EventName.GameUrl, params: url } as GoogleAnalyticsData;
const event = new CustomEvent(DocumentEvent.GoogleAnalytics, { detail });
document.dispatchEvent(event);
}
2024-12-27 14:14:38 +08:00
}