127 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-04-12 17:04:48 +08:00
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create(
{
title: '添加到书签',
id: "addBookmark",
},
2022-05-11 14:30:38 +08:00
() => console.debug("创建右键菜单成功")
2022-04-12 17:04:48 +08:00
);
});
2022-04-08 17:04:13 +08:00
2022-04-11 17:42:00 +08:00
chrome.contextMenus.onClicked.addListener(async function (info, tab) {
2022-05-11 14:30:38 +08:00
console.debug(info, tab);
2022-04-11 17:42:00 +08:00
let body = {
name: tab.title,
url: tab.url,
2022-04-17 14:53:44 +08:00
iconUrl: tab.favIconUrl
2022-04-11 17:42:00 +08:00
};
sendToContent(tab.id, { code: "addBookmark", data: body, token: await getVal("token") });
2022-04-12 17:04:48 +08:00
});
2022-04-11 17:42:00 +08:00
2022-04-12 17:04:48 +08:00
// 接收content/popup发送的消息
2022-04-11 17:42:00 +08:00
chrome.runtime.onMessage.addListener(async (data, sender, sendResponse) => {
2022-04-12 17:04:48 +08:00
if (!data.code || !data.receiver == 'background') {
2022-04-11 17:42:00 +08:00
return;
}
2022-04-12 17:04:48 +08:00
sendResponse("ok");
2022-05-11 14:30:38 +08:00
console.debug("收到消息:", data, sender);
2022-04-11 17:42:00 +08:00
if (data.code == 'setToken') {
2022-04-12 17:04:48 +08:00
await setVal("token", data.data);
// sendToContent
await sendToContent(sender.tab.id, { code: "setTokenOk" });
} else if (data.code == 'getToken') {
let token = await getVal("token");
2022-04-17 14:53:44 +08:00
sendToPopup({ code: "setToken", data: token });
2022-04-12 17:04:48 +08:00
} else if (data.code == "clearToken") {
await clearVal("token");
2022-04-11 17:42:00 +08:00
}
})
/**
* 向content发送消息
* @param {*} tabId
* @param {*} data
*/
function sendToContent (tabId, data) {
2022-05-11 14:30:38 +08:00
console.debug(tabId, data);
2022-04-12 17:04:48 +08:00
data.receiver = "content";
2022-04-11 17:42:00 +08:00
chrome.tabs.sendMessage(tabId, data, res => {
2022-05-11 14:30:38 +08:00
console.debug("接受响应", res);
2022-04-11 17:42:00 +08:00
})
}
2022-04-12 17:04:48 +08:00
/**
* 向popup发送消息
* @param {*} data
*/
function sendToPopup (data) {
data.receiver = "popup";
2022-05-11 14:30:38 +08:00
chrome.runtime.sendMessage(data, res => console.debug(res));
2022-04-12 17:04:48 +08:00
}
/**
* 设置值
* @param {*} key
* @param {*} val
* @returns
*/
2022-04-11 17:42:00 +08:00
function setVal (key, val) {
return new Promise((resolve, reject) => {
chrome.storage.local.set({ [key]: val }, function () {
2022-05-11 14:30:38 +08:00
console.debug("设置值成功:", key, val)
2022-04-11 17:42:00 +08:00
resolve();
})
})
}
2022-04-12 17:04:48 +08:00
/**
* 获取值
* @param {*} key
* @returns
*/
2022-04-11 17:42:00 +08:00
function getVal (key) {
return new Promise((resolve, reject) => {
2022-04-17 14:53:44 +08:00
chrome.storage.local.get([key], async function (res) {
if (key === 'token' && !checkTokenValid(res[key])) {
2022-05-11 14:30:38 +08:00
console.debug("token过期");
2022-04-17 14:53:44 +08:00
await clearVal("token");
res[key] = null;
}
2022-05-11 14:30:38 +08:00
console.debug("取值成功", res);
2022-04-11 17:42:00 +08:00
resolve(res[key]);
})
})
2022-04-12 17:04:48 +08:00
}
function clearVal (key) {
return new Promise((resolve, reject) => {
chrome.storage.local.remove(key, function () {
2022-05-11 14:30:38 +08:00
console.debug("remove成功", key);
2022-04-12 17:04:48 +08:00
resolve();
})
})
2022-04-17 14:53:44 +08:00
}
/**
* 检查token是否有效
* @param {*} token
* @returns
*/
function checkTokenValid (token) {
try {
if (token && token.trim().length > 0) {
//检查token是否还有效
let content = JSON.parse(atob(token.split(".")[1]));
if (content.exp > Date.now() / 1000) {
return true;
}
}
} catch (err) {
console.error(token, err);
}
return false;
2022-04-11 17:42:00 +08:00
}