将popup移植了过去

This commit is contained in:
xu_yanfeng
2024-01-08 19:50:06 +08:00
parent 64260e567d
commit fb028828f7
30 changed files with 711 additions and 680 deletions

View File

@@ -0,0 +1,79 @@
export enum Page {
Inject = "Inject",
Devtools = "Devtools",
Background = "Background",
Content = "Content",
Popup = "Popup",
Options = "Options",
}
export enum Msg {
NodeInfo = "node-info",// 具体的节点信息
TreeInfo = "tree-info",// 节点树信息
Support = "game-support",// 游戏支持信息
MemoryInfo = "memory-info",//
TabsInfo = "tabs_info", // 当前页面信息
GetTabID = "GetTabID", // 获取页面ID
UpdateFrames = "UpdateFrames", // 更新页面的frame
UseFrame = "UseFrame",
GetObjectItemData = "GetObjectItemData",
LogData = "LogData",
SetProperty = "set-property", // 设置node属性
UpdateProperty = "update-property", // 更新属性
}
export class PluginEvent {
msg: Msg | null = null;
data: any = null;
source: Page | null = null; // 事件发送的源头
target: Page | null = null; // 事件要发送的目标
static check(event: PluginEvent, source: Page, target: Page) {
return event && source && target && event.source === source && event.target === target;
}
static reset(event: PluginEvent, source: Page | null, target: Page | null) {
if (event && source && target) {
event.source = source;
event.target = target;
}
}
static finish(event: PluginEvent) {
event.source = event.target = null;
}
constructor(source: Page, target: Page, msg: Msg, data?: any) {
if (PageInclude(target)) {
this.source = source;
this.target = target;
this.msg = msg;
this.data = data;
} else {
console.warn(`无效的target: ${target}`)
}
}
}
function inEnum(enumValues: any, value: Page | Msg) {
for (let key in enumValues) {
if (enumValues.hasOwnProperty(key)) {
//@ts-ignore
let itemEnum = enumValues[key] as string;
if (itemEnum === value) {
return true
}
}
}
return false;
}
export function PageInclude(page: Page) {
return inEnum(Page, page);
}
export function MsgInclude(msg: Msg) {
return inEnum(Msg, msg)
}

View File

@@ -0,0 +1,43 @@
export function injectScript(url: string) {
if (chrome && chrome.extension && chrome.extension.getURL) {
let content = chrome.extension.getURL(url)
console.log(`[cc-inspector]注入脚本:${content}`);
const script = document.createElement("script")
script.setAttribute("type", "text/javascript")
script.setAttribute("src", content)
script.onload = function () {
document.body.removeChild(script);
}
document.body.appendChild(script)
}
}
interface LogOptions {
data: any;
flag?: string;
color?: "red" | "blue";
}
export function log(options: LogOptions) {
const data: any = options.data;
const time = new Date().toLocaleString()
let log = ""
if (typeof data === "string") {
log = data;
} else if (typeof data === "object") {
log = JSON.stringify(data)
}
let str = "";
if (options.flag) {
str = `[${time}][${options.flag}]: ${log} `;
} else {
str = `[${time}]: ${log} `;
}
if (options.color) {
console.log(`%c${str}`, `color:${options.color};`)
} else {
console.log(str);
}
}