fix: getCurTab
This commit is contained in:
@@ -1,5 +1,22 @@
|
|||||||
import { browser } from "./browser";
|
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
|
* 发送消息给background
|
||||||
* @param {*} action
|
* @param {*} action
|
||||||
@@ -16,15 +33,6 @@ export const sendBgMsg = (action, args) =>
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const sendTabMsg = async (action, args) => {
|
export const sendTabMsg = async (action, args) => {
|
||||||
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
const tabId = await getCurTabId();
|
||||||
return browser.tabs.sendMessage(tabs[0].id, { action, args });
|
return browser.tabs.sendMessage(tabId, { action, args });
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前tab信息
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
export const getTabInfo = async () => {
|
|
||||||
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
|
||||||
return tabs[0];
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import MenuItem from "@mui/material/MenuItem";
|
|||||||
import FormControlLabel from "@mui/material/FormControlLabel";
|
import FormControlLabel from "@mui/material/FormControlLabel";
|
||||||
import Switch from "@mui/material/Switch";
|
import Switch from "@mui/material/Switch";
|
||||||
import Button from "@mui/material/Button";
|
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 { browser } from "../../libs/browser";
|
||||||
import { isExt } from "../../libs/client";
|
import { isExt } from "../../libs/client";
|
||||||
import { useI18n } from "../../hooks/I18n";
|
import { useI18n } from "../../hooks/I18n";
|
||||||
@@ -83,7 +83,7 @@ export default function Popup({ setShowPopup, translator: tran }) {
|
|||||||
try {
|
try {
|
||||||
let href = window.location.href;
|
let href = window.location.href;
|
||||||
if (!tran) {
|
if (!tran) {
|
||||||
const tab = await getTabInfo();
|
const tab = await getCurTab();
|
||||||
href = tab.url;
|
href = tab.url;
|
||||||
}
|
}
|
||||||
const newRule = { ...rule, pattern: href.split("/")[2] };
|
const newRule = { ...rule, pattern: href.split("/")[2] };
|
||||||
|
|||||||
Reference in New Issue
Block a user