Files
kiss-translator/src/libs/msg.js

39 lines
756 B
JavaScript
Raw Normal View History

2023-08-04 16:48:40 +08:00
import { browser } from "./browser";
2023-07-20 13:45:41 +08:00
2024-03-14 16:28:32 +08:00
/**
* 获取当前tab信息
* @returns
*/
export const getCurTab = async () => {
const [tab] = await browser.tabs.query({
active: true,
lastFocusedWindow: true,
});
return tab;
};
export const getCurTabId = async () => {
const tab = await getCurTab();
return tab.id;
};
2023-07-20 13:45:41 +08:00
/**
* 发送消息给background
* @param {*} action
* @param {*} args
* @returns
*/
2023-08-31 13:38:06 +08:00
export const sendBgMsg = (action, args) =>
browser.runtime.sendMessage({ action, args });
2023-07-20 13:45:41 +08:00
/**
* 发送消息给当前页面
* @param {*} action
* @param {*} args
* @returns
*/
export const sendTabMsg = async (action, args) => {
2024-03-14 16:28:32 +08:00
const tabId = await getCurTabId();
return browser.tabs.sendMessage(tabId, { action, args });
2023-09-10 12:35:03 +08:00
};