mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-04-19 16:38:41 +00:00
devtools界面和content.js进行交互
This commit is contained in:
parent
a8a26154f8
commit
f00ae2cf90
@ -1,6 +1,5 @@
|
|||||||
function shortConnectionLink(request, sender, sendResponse) {
|
function shortConnectionLink(request, sender, sendResponse) {
|
||||||
console.log(`%c[短连接|id:${sender.id}|url:${sender.url}]\n${JSON.stringify(request)}`, 'background:#aaa;color:#BD4E19')
|
console.log(`%c[短连接|id:${sender.id}|url:${sender.url}]\n${JSON.stringify(request)}`, 'background:#aaa;color:#BD4E19')
|
||||||
console.log(request);
|
|
||||||
sendResponse && sendResponse(request);
|
sendResponse && sendResponse(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
// 具有操作dom的能力
|
// 具有操作dom的能力
|
||||||
// 加载其他脚本
|
// 加载其他脚本
|
||||||
// var content = chrome.extension.getURL('js/inject.js')
|
|
||||||
// var script = document.createElement('script')
|
function injectScriptToPage(url) {
|
||||||
// script.setAttribute('type', 'text/javascript')
|
let content = chrome.extension.getURL(url)
|
||||||
// script.setAttribute('src', content)
|
console.log(`[cc-inspector]注入脚本:${content}`);
|
||||||
// script.onload = function () {
|
let script = document.createElement('script')
|
||||||
// // 注入脚本执行完后移除掉
|
script.setAttribute('type', 'text/javascript')
|
||||||
// this.parentNode.removeChild(this);
|
script.setAttribute('src', content)
|
||||||
// }
|
script.onload = function () {
|
||||||
// document.body.appendChild(script)
|
// 注入脚本执行完后移除掉
|
||||||
|
this.parentNode.removeChild(this);
|
||||||
|
}
|
||||||
|
document.body.appendChild(script)
|
||||||
|
}
|
||||||
|
|
||||||
|
injectScriptToPage("js/inject.js");
|
||||||
|
|
||||||
|
|
||||||
window.addEventListener('message', function (event) {
|
window.addEventListener('message', function (event) {
|
||||||
@ -28,7 +34,6 @@ if (gameCanvas) {
|
|||||||
} else {
|
} else {
|
||||||
// console.log("can't find GameCanvas element");
|
// console.log("can't find GameCanvas element");
|
||||||
chrome.runtime.sendMessage({type: 0, msg: "no creator game!"}, function (data) {
|
chrome.runtime.sendMessage({type: 0, msg: "no creator game!"}, function (data) {
|
||||||
debugger
|
|
||||||
console.log(data)
|
console.log(data)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -164,3 +164,14 @@ export default function () {
|
|||||||
console.log("未发现cocos creator game");
|
console.log("未发现cocos creator game");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.ccinspector = window.ccinspector || {test:1};
|
||||||
|
|
||||||
|
setInterval(function () {
|
||||||
|
if (window.ccinspector.stop) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.log("我是注入的脚本");
|
||||||
|
}
|
||||||
|
|
||||||
|
}, 1000);
|
||||||
|
@ -43,27 +43,25 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
debugger
|
// chrome.devtools.inspectedWindow.tabId
|
||||||
let backgroundPageConnection = chrome.runtime.connect({
|
let conn = chrome.runtime.connect({name: "devtools"});
|
||||||
name: btoa("for" + String(chrome.devtools.inspectedWindow.tabId))
|
conn.onMessage.addListener(function (data, sender) {
|
||||||
});
|
if (data !== null) {
|
||||||
backgroundPageConnection.onMessage.addListener(function (message) {
|
|
||||||
if (message !== null) {
|
|
||||||
let msgType = {
|
let msgType = {
|
||||||
nodeInfo: 2,//节点信息
|
nodeInfo: 2,//节点信息
|
||||||
nodeListInfo: 1,// 节点列表信息
|
nodeListInfo: 1,// 节点列表信息
|
||||||
notSupport: 0,// 不支持的游戏
|
notSupport: 0,// 不支持的游戏
|
||||||
};
|
};
|
||||||
if (message.type === msgType.nodeListInfo) {// 游戏节点
|
if (data.type === msgType.nodeListInfo) {// 游戏节点
|
||||||
this.isShowDebug = true;
|
this.isShowDebug = true;
|
||||||
// let str = JSON.stringify(message.msg);
|
// let str = JSON.stringify(message.msg);
|
||||||
// console.log("onMessage: " + str);
|
// console.log("onMessage: " + str);
|
||||||
this._updateView(message.msg);
|
this._updateView(data.msg);
|
||||||
} else if (message.type === msgType.notSupport) {// 不支持调试
|
} else if (data.type === msgType.notSupport) {// 不支持调试
|
||||||
this.isShowDebug = false;
|
this.isShowDebug = false;
|
||||||
} else if (message.type === msgType.nodeInfo) {
|
} else if (data.type === msgType.nodeInfo) {
|
||||||
this.isShowDebug = true;
|
this.isShowDebug = true;
|
||||||
this.treeItemData = message.msg;
|
this.treeItemData = data.msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
@ -178,6 +176,10 @@
|
|||||||
|
|
||||||
onBtnClickUpdatePage() {
|
onBtnClickUpdatePage() {
|
||||||
debugger
|
debugger
|
||||||
|
|
||||||
|
let injectCode =`console.log(window.ccinspector);window.ccinspector.stop=!window.ccinspector.stop;`;
|
||||||
|
chrome.devtools.inspectedWindow.eval(injectCode);
|
||||||
|
return;
|
||||||
let code = this._getInjectScriptString();
|
let code = this._getInjectScriptString();
|
||||||
chrome.devtools.inspectedWindow.eval(code, function () {
|
chrome.devtools.inspectedWindow.eval(code, function () {
|
||||||
console.log("刷新成功!");
|
console.log("刷新成功!");
|
||||||
|
@ -67,11 +67,14 @@
|
|||||||
if (!this.longConn) {
|
if (!this.longConn) {
|
||||||
console.log("[popup] 初始化长连接");
|
console.log("[popup] 初始化长连接");
|
||||||
this.longConn = chrome.runtime.connect({name: "popup"});
|
this.longConn = chrome.runtime.connect({name: "popup"});
|
||||||
this.longConn.onMessage.addListener(function (data, info) {
|
this.longConn.onMessage.addListener(function (data, sender) {
|
||||||
debugger
|
this._onLongConnMsg(data, sender);
|
||||||
})
|
}.bind(this))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
_onLongConnMsg(data, sender) {
|
||||||
|
console.log(this.title);
|
||||||
|
},
|
||||||
onMsgToBg() {
|
onMsgToBg() {
|
||||||
// 因为webpack的原因,这种方式可能拿不到里面的function, var
|
// 因为webpack的原因,这种方式可能拿不到里面的function, var
|
||||||
// chrome.extension.getBackgroundPage();
|
// chrome.extension.getBackgroundPage();
|
||||||
@ -82,7 +85,6 @@
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
onSendMsg() {
|
onSendMsg() {
|
||||||
debugger
|
|
||||||
if (this.longConn) {
|
if (this.longConn) {
|
||||||
this.longConn.postMessage({send: "hello"});
|
this.longConn.postMessage({send: "hello"});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user