From 53fb41ace7ec8c94eed5e860efe3ba8a0a3727a0 Mon Sep 17 00:00:00 2001 From: xyf-mac Date: Sun, 17 Mar 2019 21:44:47 +0800 Subject: [PATCH] no message --- CocosCreatorInspector/src/background/index.js | 37 +++++++++++++++++++ CocosCreatorInspector/src/content/index.js | 15 +++++++- CocosCreatorInspector/src/content/inject.js | 13 ++++++- CocosCreatorInspector/src/core/event-mgr.js | 3 ++ CocosCreatorInspector/src/core/event.js | 3 ++ CocosCreatorInspector/src/devtools/index.js | 9 ----- .../src/devtools/panel/index.vue | 21 +++++++++-- CocosCreatorInspector/src/manifest.js | 11 +++++- 8 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 CocosCreatorInspector/src/core/event-mgr.js create mode 100644 CocosCreatorInspector/src/core/event.js diff --git a/CocosCreatorInspector/src/background/index.js b/CocosCreatorInspector/src/background/index.js index e4068cc..192167b 100644 --- a/CocosCreatorInspector/src/background/index.js +++ b/CocosCreatorInspector/src/background/index.js @@ -14,6 +14,7 @@ function longConnectionLink(data, sender) { chrome.runtime.onConnect.addListener(function (port) { console.log(`%c[长连接:${port.name}] 建立链接!`, 'background:#aaa;color:#ff0000'); port.onMessage.addListener(longConnectionLink); + port.postMessage("ok"); port.onDisconnect.addListener(function (port) { console.log(`%c[长连接:${port.name}] 断开链接!`, 'background:#aaa;color:#00ff00'); port.onMessage.removeListener(longConnectionLink); @@ -23,3 +24,39 @@ chrome.runtime.onConnect.addListener(function (port) { // background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致 // [短连接] 监听来自content.js发来的事件 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(); +}); + diff --git a/CocosCreatorInspector/src/content/index.js b/CocosCreatorInspector/src/content/index.js index 1970df9..132edba 100644 --- a/CocosCreatorInspector/src/content/index.js +++ b/CocosCreatorInspector/src/content/index.js @@ -1,6 +1,6 @@ // 具有操作dom的能力 // 加载其他脚本 - +// content.js 和原始界面共享DOM,但是不共享js,要想访问页面js,只能通过注入的方式 function injectScriptToPage(url) { let content = chrome.extension.getURL(url) console.log(`[cc-inspector]注入脚本:${content}`); @@ -16,8 +16,18 @@ function injectScriptToPage(url) { 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) { + debugger let data = event.data; // console.log("[contentScripts] " + JSON.stringify(data)); chrome.runtime.sendMessage(data); @@ -33,6 +43,7 @@ if (gameCanvas) { // gameCanvas.style.display = 'none'; } else { // console.log("can't find GameCanvas element"); + // 和background.js保持短连接通讯 chrome.runtime.sendMessage({type: 0, msg: "no creator game!"}, function (data) { console.log(data) }); diff --git a/CocosCreatorInspector/src/content/inject.js b/CocosCreatorInspector/src/content/inject.js index f2e11e8..1b78703 100644 --- a/CocosCreatorInspector/src/content/inject.js +++ b/CocosCreatorInspector/src/content/inject.js @@ -165,7 +165,7 @@ export default function () { } } -window.ccinspector = window.ccinspector || {test:1}; +window.ccinspector = window.ccinspector || {test: 1}; setInterval(function () { if (window.ccinspector.stop) { @@ -175,3 +175,14 @@ setInterval(function () { } }, 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"); +} diff --git a/CocosCreatorInspector/src/core/event-mgr.js b/CocosCreatorInspector/src/core/event-mgr.js new file mode 100644 index 0000000..e432721 --- /dev/null +++ b/CocosCreatorInspector/src/core/event-mgr.js @@ -0,0 +1,3 @@ +module.exports = { + id: "event-mgr", +} diff --git a/CocosCreatorInspector/src/core/event.js b/CocosCreatorInspector/src/core/event.js new file mode 100644 index 0000000..998067b --- /dev/null +++ b/CocosCreatorInspector/src/core/event.js @@ -0,0 +1,3 @@ +module.exports = { + id: "event-id", +} diff --git a/CocosCreatorInspector/src/devtools/index.js b/CocosCreatorInspector/src/devtools/index.js index 7373fa3..8e542c2 100644 --- a/CocosCreatorInspector/src/devtools/index.js +++ b/CocosCreatorInspector/src/devtools/index.js @@ -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()); -// })(); diff --git a/CocosCreatorInspector/src/devtools/panel/index.vue b/CocosCreatorInspector/src/devtools/panel/index.vue index 94ba2b7..5b036ea 100644 --- a/CocosCreatorInspector/src/devtools/panel/index.vue +++ b/CocosCreatorInspector/src/devtools/panel/index.vue @@ -1,8 +1,9 @@