2021-04-02 22:34:09 +08:00
|
|
|
|
import * as PluginMsg from "./core/plugin-msg"
|
2019-03-18 12:05:07 +08:00
|
|
|
|
|
2021-04-02 22:34:09 +08:00
|
|
|
|
let Devtools: chrome.runtime.Port | null = null;
|
|
|
|
|
let DevtoolsPanel: chrome.runtime.Port | null = null;
|
|
|
|
|
let Content: chrome.runtime.Port | null = null;
|
2021-04-03 16:47:16 +08:00
|
|
|
|
console.log('on background')
|
2021-04-02 22:34:09 +08:00
|
|
|
|
|
2021-04-05 18:38:44 +08:00
|
|
|
|
chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
|
|
|
|
|
console.log(`%c[Connect] ${port.name}`, "color:blue;");
|
|
|
|
|
port.onMessage.addListener((data: any, sender: any) => {
|
|
|
|
|
console.log(`%c[Connect-Message] ${sender.name}\n${JSON.stringify(data)}`, "color:green;")
|
|
|
|
|
sender.postMessage(data);
|
|
|
|
|
if (data.msg === PluginMsg.Msg.UrlChange) {
|
|
|
|
|
if (sender.name === PluginMsg.Page.DevToolsPanel) {
|
|
|
|
|
Content && Content.postMessage({msg: PluginMsg.Msg.UrlChange, data: {}})
|
|
|
|
|
}
|
2019-03-18 18:03:07 +08:00
|
|
|
|
}
|
2021-04-05 18:38:44 +08:00
|
|
|
|
// chrome.tabs.executeScript(message.tabId, {code: message.content});
|
|
|
|
|
// port.postMessage(message);
|
|
|
|
|
});
|
|
|
|
|
port.onDisconnect.addListener(function (port: chrome.runtime.Port) {
|
|
|
|
|
console.log(`%c[Connect-Dis] ${port.name}`, "color:red");
|
|
|
|
|
// port.onMessage.removeListener(longConnectionLink);
|
2019-03-18 12:05:07 +08:00
|
|
|
|
if (port.name === PluginMsg.Page.Devtools) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
Devtools = null;
|
2019-03-18 18:03:07 +08:00
|
|
|
|
} else if (port.name === PluginMsg.Page.Content) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
Content = null;
|
2019-03-18 18:03:07 +08:00
|
|
|
|
} else if (port.name === PluginMsg.Page.DevToolsPanel) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
DevtoolsPanel = null;
|
2019-03-18 12:05:07 +08:00
|
|
|
|
}
|
2019-03-15 10:08:39 +08:00
|
|
|
|
});
|
2019-03-18 12:05:07 +08:00
|
|
|
|
|
|
|
|
|
// 缓存
|
|
|
|
|
if (port.name === PluginMsg.Page.Devtools) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
Devtools = port;
|
2019-03-18 18:03:07 +08:00
|
|
|
|
} else if (port.name === PluginMsg.Page.Content) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
Content = port;
|
2019-03-18 18:03:07 +08:00
|
|
|
|
} else if (port.name === PluginMsg.Page.DevToolsPanel) {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
DevtoolsPanel = port;
|
2019-03-18 12:05:07 +08:00
|
|
|
|
}
|
2019-03-15 10:08:39 +08:00
|
|
|
|
});
|
|
|
|
|
|
2019-03-16 18:44:32 +08:00
|
|
|
|
// background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致
|
2021-04-05 18:38:44 +08:00
|
|
|
|
// 监听来自content.js发来的事件
|
|
|
|
|
chrome.runtime.onMessage.addListener((request: any, sender: any, sendResponse: any) => {
|
|
|
|
|
console.log(`%c[Message]url:${sender.url}]\n${JSON.stringify(request)}`, 'color:green')
|
|
|
|
|
sendResponse && sendResponse(request);
|
|
|
|
|
if (request.msg === PluginMsg.Msg.Support ||
|
|
|
|
|
request.msg === PluginMsg.Msg.ListInfo ||
|
|
|
|
|
request.msg === PluginMsg.Msg.NodeInfo) {
|
|
|
|
|
// 将消息转发到devtools
|
|
|
|
|
Devtools && Devtools.postMessage(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-03-17 21:44:47 +08:00
|
|
|
|
|
2019-03-18 18:03:07 +08:00
|
|
|
|
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
|
|
|
|
if (changeInfo.status === "complete") {
|
|
|
|
|
// 加载新的url
|
2021-04-02 22:34:09 +08:00
|
|
|
|
if (Content) {
|
|
|
|
|
let data = {msg: PluginMsg.Msg.UrlChange, data: {url: tab.favIconUrl}}
|
|
|
|
|
Content.postMessage(data);
|
|
|
|
|
}
|
2019-03-18 18:03:07 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
2019-03-17 21:44:47 +08:00
|
|
|
|
|
|
|
|
|
function createPluginMenus() {
|
2021-04-05 18:38:44 +08:00
|
|
|
|
const menus = [];
|
|
|
|
|
|
2019-03-17 21:44:47 +08:00
|
|
|
|
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
|
2021-04-02 22:34:09 +08:00
|
|
|
|
contexts: ["page"],
|
2019-03-17 21:44:47 +08:00
|
|
|
|
});
|
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
|
id: "notify",
|
|
|
|
|
parentId: parent,
|
|
|
|
|
title: "通知"
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
chrome.contextMenus.onClicked.addListener(function (info, tab) {
|
|
|
|
|
if (info.menuItemId === "test") {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
alert("您点击了右键菜单!");
|
2019-03-17 21:44:47 +08:00
|
|
|
|
} else if (info.menuItemId === "notify") {
|
2021-04-02 22:34:09 +08:00
|
|
|
|
chrome.notifications.create("null", {
|
2019-03-17 21:44:47 +08:00
|
|
|
|
type: "basic",
|
2021-04-03 16:47:16 +08:00
|
|
|
|
iconUrl: "icons/48.png",
|
2019-03-17 21:44:47 +08:00
|
|
|
|
title: "通知",
|
|
|
|
|
message: "测试通知",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.contextMenus.removeAll(function () {
|
|
|
|
|
createPluginMenus();
|
|
|
|
|
});
|
|
|
|
|
|