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) {
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();
});