2021-04-05 18:38:44 +08:00
|
|
|
|
// content.js 和原始界面共享DOM,具有操作dom的能力
|
|
|
|
|
// 但是不共享js,要想访问页面js,只能通过注入的方式
|
2021-04-26 22:27:47 +08:00
|
|
|
|
import {injectScript} from "@/core/util";
|
2021-04-28 13:59:16 +08:00
|
|
|
|
import {PluginEvent, Page, Msg} from "@/core/types";
|
2021-04-04 20:48:18 +08:00
|
|
|
|
|
2021-04-26 22:27:47 +08:00
|
|
|
|
injectScript("js/inject.js");
|
2021-04-04 20:48:18 +08:00
|
|
|
|
|
|
|
|
|
// 和background.js保持长连接通讯
|
2021-04-28 13:59:16 +08:00
|
|
|
|
let conn = chrome.runtime.connect({name: Page.Content})
|
|
|
|
|
conn.onMessage.addListener((data: PluginEvent, sender) => {
|
2021-04-04 20:48:18 +08:00
|
|
|
|
// 将background.js的消息返回到injection.js
|
2021-04-05 18:38:44 +08:00
|
|
|
|
console.log(`%c[Connect-Message] ${JSON.stringify(data)}`, "color:green;")
|
2021-04-28 13:59:16 +08:00
|
|
|
|
if (data.target === Page.Content) {
|
|
|
|
|
data.target = Page.Inject;
|
|
|
|
|
window.postMessage(data, "*");
|
|
|
|
|
}
|
2021-04-04 20:48:18 +08:00
|
|
|
|
})
|
2021-04-28 13:59:16 +08:00
|
|
|
|
|
2021-04-04 20:48:18 +08:00
|
|
|
|
// 接受来自inject.js的消息数据,然后中转到background.js
|
2021-04-28 13:59:16 +08:00
|
|
|
|
window.addEventListener('message', function (event) {
|
|
|
|
|
let data: PluginEvent = event.data;
|
|
|
|
|
console.log(`%c[Window-Message] ${JSON.stringify(data)}`, "color:green;");
|
|
|
|
|
if (data.target === Page.Inject) {
|
|
|
|
|
data.target = Page.Background;
|
|
|
|
|
chrome.runtime.sendMessage(data);
|
|
|
|
|
}
|
|
|
|
|
}, false);
|
2021-04-04 20:48:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let gameCanvas = document.querySelector("#GameCanvas");
|
2021-04-26 22:27:47 +08:00
|
|
|
|
if (!gameCanvas) {
|
2021-04-28 13:59:16 +08:00
|
|
|
|
let sendData = new PluginEvent(Page.Background, Msg.Support, {
|
2021-04-26 22:27:47 +08:00
|
|
|
|
support: false,
|
|
|
|
|
msg: "未发现GameCanvas,不支持调试游戏!"
|
|
|
|
|
})
|
|
|
|
|
chrome.runtime.sendMessage(sendData, (ret) => {
|
|
|
|
|
console.log(ret)
|
2021-04-04 20:48:18 +08:00
|
|
|
|
});
|
|
|
|
|
}
|