no message

This commit is contained in:
xyf-mac 2019-03-17 21:44:47 +08:00
parent f00ae2cf90
commit 53fb41ace7
8 changed files with 96 additions and 16 deletions

View File

@ -14,6 +14,7 @@ function longConnectionLink(data, sender) {
chrome.runtime.onConnect.addListener(function (port) { chrome.runtime.onConnect.addListener(function (port) {
console.log(`%c[长连接:${port.name}] 建立链接!`, 'background:#aaa;color:#ff0000'); console.log(`%c[长连接:${port.name}] 建立链接!`, 'background:#aaa;color:#ff0000');
port.onMessage.addListener(longConnectionLink); port.onMessage.addListener(longConnectionLink);
port.postMessage("ok");
port.onDisconnect.addListener(function (port) { port.onDisconnect.addListener(function (port) {
console.log(`%c[长连接:${port.name}] 断开链接!`, 'background:#aaa;color:#00ff00'); console.log(`%c[长连接:${port.name}] 断开链接!`, 'background:#aaa;color:#00ff00');
port.onMessage.removeListener(longConnectionLink); port.onMessage.removeListener(longConnectionLink);
@ -23,3 +24,39 @@ chrome.runtime.onConnect.addListener(function (port) {
// background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致 // background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致
// [短连接] 监听来自content.js发来的事件 // [短连接] 监听来自content.js发来的事件
chrome.runtime.onMessage.addListener(shortConnectionLink); chrome.runtime.onMessage.addListener(shortConnectionLink);
function createPluginMenus() {
// 右键菜单
let parent = chrome.contextMenus.create({id: "parent", title: "CC-Inspector"});
chrome.contextMenus.create({
id: "test",
title: "测试右键菜单",
parentId: parent,
// 上下文环境,可选:["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio"]默认page
contexts: ['page'],
});
chrome.contextMenus.create({
id: "notify",
parentId: parent,
title: "通知"
})
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "test") {
alert('您点击了右键菜单!');
} else if (info.menuItemId === "notify") {
chrome.notifications.create(null, {
type: "basic",
iconUrl: "icon/icon48.png",
title: "通知",
message: "测试通知",
})
}
})
}
chrome.contextMenus.removeAll(function () {
createPluginMenus();
});

View File

@ -1,6 +1,6 @@
// 具有操作dom的能力 // 具有操作dom的能力
// 加载其他脚本 // 加载其他脚本
// content.js 和原始界面共享DOM,但是不共享js,要想访问页面js,只能通过注入的方式
function injectScriptToPage(url) { function injectScriptToPage(url) {
let content = chrome.extension.getURL(url) let content = chrome.extension.getURL(url)
console.log(`[cc-inspector]注入脚本:${content}`); console.log(`[cc-inspector]注入脚本:${content}`);
@ -16,8 +16,18 @@ function injectScriptToPage(url) {
injectScriptToPage("js/inject.js"); injectScriptToPage("js/inject.js");
// 和background.js保持长连接通讯
let conn = chrome.runtime.connect({name: "connect.js"})
// conn.postMessage('test');
let EventMgr=require("../core/event-mgr");
debugger
EventMgr.id="inject-id";
conn.onMessage.addListener(function (port) {
debugger
})
// 接受来自inject.js的消息数据
window.addEventListener('message', function (event) { window.addEventListener('message', function (event) {
debugger
let data = event.data; let data = event.data;
// console.log("[contentScripts] " + JSON.stringify(data)); // console.log("[contentScripts] " + JSON.stringify(data));
chrome.runtime.sendMessage(data); chrome.runtime.sendMessage(data);
@ -33,6 +43,7 @@ if (gameCanvas) {
// gameCanvas.style.display = 'none'; // gameCanvas.style.display = 'none';
} else { } else {
// console.log("can't find GameCanvas element"); // console.log("can't find GameCanvas element");
// 和background.js保持短连接通讯
chrome.runtime.sendMessage({type: 0, msg: "no creator game!"}, function (data) { chrome.runtime.sendMessage({type: 0, msg: "no creator game!"}, function (data) {
console.log(data) console.log(data)
}); });

View File

@ -175,3 +175,14 @@ setInterval(function () {
} }
}, 1000); }, 1000);
window.ccinspector.testMsg1 = function () {
window.postMessage("testMsg1")
}
window.ccinspector.testMsg2 = function () {
debugger
chrome.runtime.connect({name: "inject"});
}
window.ccinspector.testMsg3=function () {
debugger
chrome.runtime.sendMessage("ffff");
}

View File

@ -0,0 +1,3 @@
module.exports = {
id: "event-mgr",
}

View File

@ -0,0 +1,3 @@
module.exports = {
id: "event-id",
}

View File

@ -18,12 +18,3 @@ chrome.devtools.panels.create("Cocos", "icon/icon48.png", "pages/devtools_panel.
} }
); );
// (function () {
// var t = window.setInterval(function () {
// egret && egret.devtool &&
// egret.devtool.start &&
// (window.clearInterval(t) || egret.devtool.start());
// console.log("waiting")
// }, 100);
// egret && egret.devtool && egret.devtool.start && (window.clearInterval(t) || egret.devtool.start());
// })();

View File

@ -1,8 +1,9 @@
<template> <template>
<div id="app"> <div id="app">
<el-button type="success" class="el-icon-refresh" size="mini" @click="onBtnClickUpdatePage">刷新</el-button> <el-button type="success" class="el-icon-refresh" size="mini" @click="onBtnClickUpdatePage">刷新</el-button>
<!--<el-button type="success" size="mini" @click="onTestData">测试</el-button>--> <el-button type="success" size="mini" @click="onBtnClickTest1">Test1</el-button>
<!--<el-button type="success" size="mini" @click="onBtnClickTest">test</el-button>--> <el-button type="success" size="mini" @click="onBtnClickTest2">Test2</el-button>
<el-button type="success" size="mini" @click="onBtnClickTest3">Test3</el-button>
<div v-show="isShowDebug"> <div v-show="isShowDebug">
<el-row> <el-row>
<el-col :span="8"> <el-col :span="8">
@ -46,6 +47,7 @@
// chrome.devtools.inspectedWindow.tabId // chrome.devtools.inspectedWindow.tabId
let conn = chrome.runtime.connect({name: "devtools"}); let conn = chrome.runtime.connect({name: "devtools"});
conn.onMessage.addListener(function (data, sender) { conn.onMessage.addListener(function (data, sender) {
debugger
if (data !== null) { if (data !== null) {
let msgType = { let msgType = {
nodeInfo: 2,// nodeInfo: 2,//
@ -184,7 +186,20 @@
chrome.devtools.inspectedWindow.eval(code, function () { chrome.devtools.inspectedWindow.eval(code, function () {
console.log("刷新成功!"); console.log("刷新成功!");
}); });
}, },
onBtnClickTest1() {
chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg1()`)
},
onBtnClickTest2() {
chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg2()`)
},
onBtnClickTest3() {
// chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg3()`)
let f = require("../../core/event-mgr");
console.log(f.id);
}
} }
} }
</script> </script>

View File

@ -23,7 +23,14 @@ module.exports = {
scripts: ["js/background.js"], scripts: ["js/background.js"],
persistent: false,// 需要时开启 persistent: false,// 需要时开启
}, },
// optionsV1的写法
options_page: "pages/options.html", options_page: "pages/options.html",
// optionsV2的写法
options_ui: {
page: "pages/options.html",
// 添加一些默认的样式,推荐使用
chrome_style: true,
},
manifest_version: 2, manifest_version: 2,
permissions: [ permissions: [
"tabs", "tabs",
@ -33,7 +40,9 @@ module.exports = {
"system.cpu", "system.cpu",
"tabs", "tabs",
"storage", "storage",
"nativeMessaging" "nativeMessaging",
"contextMenus",
"notifications",
], ],
web_accessible_resources: ["*/*", "*"], web_accessible_resources: ["*/*", "*"],
content_security_policy: "script-src 'self' 'unsafe-eval'; object-src 'self'" content_security_policy: "script-src 'self' 'unsafe-eval'; object-src 'self'"