2024-12-27 14:14:38 +08:00
|
|
|
|
// content.js 和原始界面共享DOM,具有操作dom的能力
|
|
|
|
|
// 但是不共享js,要想访问页面js,只能通过注入的方式
|
|
|
|
|
import { ChromeConst } from "cc-plugin/src/chrome/const";
|
2025-01-06 19:46:38 +08:00
|
|
|
|
import { debugLog, Msg, Page, PluginEvent, ResponseSupportData } from "../../core/types";
|
2024-12-27 14:14:38 +08:00
|
|
|
|
import { DocumentEvent } from "../const";
|
|
|
|
|
import { Terminal } from "../terminal";
|
|
|
|
|
|
|
|
|
|
const terminal = new Terminal(Page.Content);
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.init());
|
2024-12-27 14:14:38 +08:00
|
|
|
|
|
|
|
|
|
// #region 注入脚本
|
|
|
|
|
export function injectScript(url: string) {
|
|
|
|
|
if (chrome && chrome.runtime && chrome.runtime.getURL) {
|
|
|
|
|
let content = chrome.runtime.getURL(url);
|
|
|
|
|
const script = document.createElement("script");
|
|
|
|
|
script.setAttribute("type", "text/javascript");
|
|
|
|
|
script.setAttribute("src", content);
|
|
|
|
|
script.onload = function () {
|
|
|
|
|
document.head.removeChild(script);
|
|
|
|
|
};
|
|
|
|
|
document.head.appendChild(script);
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.green(`inject script success: ${content}`));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
} else {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.red("inject script failed"));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// #region 和Inject通讯
|
|
|
|
|
document.addEventListener(DocumentEvent.Inject2Content, (event: CustomEvent) => {
|
|
|
|
|
let data: PluginEvent = PluginEvent.create(event.detail);
|
|
|
|
|
if (data.valid && data.check(Page.Inject, Page.Content)) {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.chunkMessage(data.toChunk()));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
data.reset(Page.Content, Page.Devtools);
|
|
|
|
|
if (connect) {
|
|
|
|
|
// 接受来自inject.js的消息数据,然后中转到background.js
|
|
|
|
|
connect.postMessage(data);
|
|
|
|
|
} else {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.log(`connect is null`));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
throw new Error("connect is null");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`invalid data: ${event.detail}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// #region 和background通讯
|
|
|
|
|
let connect: chrome.runtime.Port = chrome.runtime.connect({ name: Page.Content });
|
|
|
|
|
connect.onDisconnect.addListener(() => {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.disconnect(""));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
connect = null;
|
|
|
|
|
});
|
|
|
|
|
connect.onMessage.addListener((data: PluginEvent, sender: chrome.runtime.Port) => {
|
|
|
|
|
const event = PluginEvent.create(data);
|
|
|
|
|
if (event.valid && event.check(Page.Background, Page.Content)) {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.chunkMessage(event.toChunk()));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
event.reset(Page.Content, Page.Inject);
|
|
|
|
|
const e = new CustomEvent(DocumentEvent.Content2Inject, { detail: event });
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.chunkSend(event.toChunk()));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
document.dispatchEvent(e);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`invalid data: ${data}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function checkGame() {
|
|
|
|
|
let gameCanvas = document.querySelector("#GameCanvas");
|
2024-12-27 19:20:22 +08:00
|
|
|
|
const sendData = new PluginEvent(Page.Content, Page.Devtools, Msg.ResponseSupport, {
|
2024-12-27 14:14:38 +08:00
|
|
|
|
support: !!gameCanvas,
|
2025-01-07 19:44:05 +08:00
|
|
|
|
msg: "",
|
2024-12-27 19:20:22 +08:00
|
|
|
|
} as ResponseSupportData);
|
2024-12-27 14:14:38 +08:00
|
|
|
|
if (connect) {
|
|
|
|
|
connect.postMessage(sendData);
|
|
|
|
|
} else {
|
2025-01-06 19:46:38 +08:00
|
|
|
|
debugLog && console.log(...terminal.log(`connect is null`));
|
2024-12-27 14:14:38 +08:00
|
|
|
|
throw new Error("connect is null");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
injectScript(ChromeConst.script.inject);
|
2024-12-27 19:20:22 +08:00
|
|
|
|
// checkGame();
|