fix: getCurTab

This commit is contained in:
Gabe Yuan
2024-03-14 16:28:32 +08:00
parent cc38ab6c45
commit 9d4c596b4b
2 changed files with 21 additions and 13 deletions

View File

@@ -1,5 +1,22 @@
import { browser } from "./browser";
/**
* 获取当前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;
};
/**
* 发送消息给background
* @param {*} action
@@ -16,15 +33,6 @@ export const sendBgMsg = (action, args) =>
* @returns
*/
export const sendTabMsg = async (action, args) => {
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
return browser.tabs.sendMessage(tabs[0].id, { action, args });
};
/**
* 获取当前tab信息
* @returns
*/
export const getTabInfo = async () => {
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
return tabs[0];
const tabId = await getCurTabId();
return browser.tabs.sendMessage(tabId, { action, args });
};

View File

@@ -5,7 +5,7 @@ import MenuItem from "@mui/material/MenuItem";
import FormControlLabel from "@mui/material/FormControlLabel";
import Switch from "@mui/material/Switch";
import Button from "@mui/material/Button";
import { sendBgMsg, sendTabMsg, getTabInfo } from "../../libs/msg";
import { sendBgMsg, sendTabMsg, getCurTab } from "../../libs/msg";
import { browser } from "../../libs/browser";
import { isExt } from "../../libs/client";
import { useI18n } from "../../hooks/I18n";
@@ -83,7 +83,7 @@ export default function Popup({ setShowPopup, translator: tran }) {
try {
let href = window.location.href;
if (!tran) {
const tab = await getTabInfo();
const tab = await getCurTab();
href = tab.url;
}
const newRule = { ...rule, pattern: href.split("/")[2] };