mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-06-06 08:14:02 +00:00
修复iframe导致检测异常的bug
This commit is contained in:
parent
f4c1a008e6
commit
9d74e5861c
@ -3,6 +3,7 @@ import {Msg, Page, PluginEvent} from "@/core/types";
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import * as UA from "universal-analytics"
|
import * as UA from "universal-analytics"
|
||||||
import {v4} from "uuid"
|
import {v4} from "uuid"
|
||||||
|
import {devtools_page} from "./manifest.json"
|
||||||
|
|
||||||
// 统计服务
|
// 统计服务
|
||||||
const userID = localStorage.getItem("userID") || v4()
|
const userID = localStorage.getItem("userID") || v4()
|
||||||
@ -11,8 +12,9 @@ UA("UA-134924925-3", userID);
|
|||||||
console.log("on background")
|
console.log("on background")
|
||||||
|
|
||||||
class PortMan {
|
class PortMan {
|
||||||
content: chrome.runtime.Port | null = null;
|
public currentUseContentFrameID = 0;
|
||||||
devtools: chrome.runtime.Port | null = null;
|
public content: Array<chrome.runtime.Port> = []; // 因为iframe的原因,可能对应多个,主iframe的id===0
|
||||||
|
public devtools: chrome.runtime.Port | null = null;
|
||||||
public id: number | null = null;// tab.id作为唯一标识
|
public id: number | null = null;// tab.id作为唯一标识
|
||||||
public title: string = "";
|
public title: string = "";
|
||||||
public url: string = "";
|
public url: string = "";
|
||||||
@ -35,22 +37,31 @@ class PortMan {
|
|||||||
});
|
});
|
||||||
port.onDisconnect.addListener((port: chrome.runtime.Port) => {
|
port.onDisconnect.addListener((port: chrome.runtime.Port) => {
|
||||||
console.log(`%c[Connect-Dis] ${port.name}`, "color:red");
|
console.log(`%c[Connect-Dis] ${port.name}`, "color:red");
|
||||||
onDisconnect && onDisconnect()
|
onDisconnect && onDisconnect(port)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCurrentUseContent(): chrome.runtime.Port | null {
|
||||||
|
return this.content.find(el => el.sender?.frameId !== undefined && el.sender.frameId === this.currentUseContentFrameID) || null;
|
||||||
|
}
|
||||||
|
|
||||||
dealConnect(port: chrome.runtime.Port) {
|
dealConnect(port: chrome.runtime.Port) {
|
||||||
switch (port.name) {
|
switch (port.name) {
|
||||||
case Page.Content: {
|
case Page.Content: {
|
||||||
this.content = port;
|
this.content.push(port);
|
||||||
this.onPortConnect(port,
|
this.onPortConnect(port,
|
||||||
(data: PluginEvent) => {
|
(data: PluginEvent) => {
|
||||||
if (data.target === Page.Devtools) {
|
if (data.target === Page.Devtools) {
|
||||||
this.sendDevtoolMsg(data);
|
this.sendDevtoolMsg(data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
() => {
|
(disPort: chrome.runtime.Port) => {
|
||||||
this.content = null;
|
const index = this.content.findIndex(el =>
|
||||||
|
disPort.sender?.frameId !== undefined
|
||||||
|
&& el.sender?.frameId !== undefined
|
||||||
|
&& el.sender?.frameId === disPort.sender?.frameId
|
||||||
|
);
|
||||||
|
this.content.splice(index, 1);
|
||||||
this.checkValid();
|
this.checkValid();
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
@ -62,7 +73,7 @@ class PortMan {
|
|||||||
// 从devtools过来的消息统一派发到Content中
|
// 从devtools过来的消息统一派发到Content中
|
||||||
if (PluginEvent.check(data, Page.Devtools, Page.Background)) {
|
if (PluginEvent.check(data, Page.Devtools, Page.Background)) {
|
||||||
PluginEvent.reset(data, Page.Background, Page.Content);
|
PluginEvent.reset(data, Page.Background, Page.Content);
|
||||||
this.content?.postMessage(data)
|
this.getCurrentUseContent()?.postMessage(data)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
@ -75,13 +86,13 @@ class PortMan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
checkValid() {
|
checkValid() {
|
||||||
if (!this.devtools && !this.content) {
|
if (!this.devtools && !this.content.length) {
|
||||||
this.mgr?.remove(this);
|
this.mgr?.remove(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendContentMsg(data: PluginEvent) {
|
sendContentMsg(data: PluginEvent) {
|
||||||
this.content?.postMessage(data);
|
this.getCurrentUseContent()?.postMessage(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendDevtoolMsg(data: PluginEvent) {
|
sendDevtoolMsg(data: PluginEvent) {
|
||||||
@ -124,6 +135,13 @@ class PortManagement {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isDevtools(port: chrome.runtime.Port) {
|
||||||
|
const devtoolsUrl = `chrome-extension://${port.sender?.id}/${devtools_page}`
|
||||||
|
if (port.sender?.url === devtoolsUrl) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initConnect() {
|
initConnect() {
|
||||||
chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
|
chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
|
||||||
if (port.name === Page.Devtools) {
|
if (port.name === Page.Devtools) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user