-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
83 lines (75 loc) · 2.48 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { getCurrentTab, showNotification } from "./utils.js";
// 监听插件图标点击事件
chrome.action.onClicked.addListener(async (tab) => {
// 获取默认操作
chrome.storage.sync.get(["defaultAction"], (result) => {
const defaultAction = result.defaultAction || "open-options"; // 设置默认操作为打开设置页面
if (defaultAction === "duplicate-tab") {
duplicateCurrentTab();
} else if (defaultAction === "copy-url") {
copyCurrentTabUrl();
} else if (defaultAction === "open-options") {
openOptionsPage();
}
});
});
// 监听快捷键命令
chrome.commands.onCommand.addListener(async (command) => {
console.log("Listener triggered"); // 确认监听器被触发
console.log("Command received:", command); // 打印接收到的命令
if (command === "duplicate-tab") {
duplicateCurrentTab();
} else if (command === "copy-url") {
copyCurrentTabUrl();
}
});
// 定义打开设置页面的函数
async function openOptionsPage() {
chrome.runtime.openOptionsPage();
}
// 复制当前标签页网址
async function copyCurrentTabUrl() {
let currentTab = await getCurrentTab();
console.log("Current tab:", currentTab); // 打印当前标签页信息
if (currentTab) {
try {
await chrome.scripting.executeScript({
target: { tabId: currentTab.id },
func: (url) => {
navigator.clipboard
.writeText(url)
.then(() => {
console.log("URL copied to clipboard:", url);
})
.catch((err) => {
console.error("复制失败:", err);
});
},
args: [currentTab.url],
});
console.log("URL copied:", currentTab.url);
showNotification("网址复制成功!");
} catch (err) {
console.error("执行脚本失败:", err);
showNotification("网址复制失败!");
}
} else {
console.error("未找到当前标签页");
showNotification("未找到当前标签页");
}
}
// 复制当前标签页
async function duplicateCurrentTab() {
let currentTab = await getCurrentTab();
if (currentTab) {
chrome.tabs.duplicate(currentTab.id);
}
}
// background.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Message received in background:", request); // 日志 19
if (request.action === "openInPopup") {
// 目前直接回复,可以在此添加更多逻辑
sendResponse({ status: "Message received by background" });
}
});