-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
91 lines (80 loc) · 2.37 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
84
85
86
87
88
89
90
91
// 创建右键菜单项
function createContextMenu() {
chrome.contextMenus.create({
id: 'menu-1',
title: '复制标题链接',
type: 'normal',
});
chrome.contextMenus.create({
id: 'copyHtml',
title: '复制html',
type: 'normal',
contexts: ["selection"]
});
chrome.contextMenus.create({
id: "copyImage",
title: "Copy Image",
contexts: ["image"]
});
}
// 处理复制链接操作
function handleCopyLink() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
if (tabs.length === 0) return;
const msg = `<a href="${tabs[0].url}">${tabs[0].title}</a>`;
sendMessageToTab(tabs[0].id, {action: 'copyLink', value: msg}, (res) => {
if (res === 'ok') {
showNotification(`标题为:${tabs[0].title}`, '复制成功');
}
});
});
}
// 处理复制HTML操作
function handleCopyHtml() {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
if (tabs.length === 0) return;
sendMessageToTab(tabs[0].id, {action: 'copyHtml'});
});
}
// 处理复制图片操作
function handleCopyImg(imageUrl) {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
if (tabs.length === 0) return;
sendMessageToTab(tabs[0].id, {action: 'copyImage', value: imageUrl});
});
}
// 发送消息给当前活动的标签页
function sendMessageToTab(tabId, message, callback) {
chrome.tabs.sendMessage(tabId, message, callback);
}
// 显示通知
function showNotification(message, title) {
chrome.notifications.create({
type: 'basic',
title: title,
message: message,
iconUrl: 'icon48.png'
});
}
// 监听右键菜单点击事件
chrome.contextMenus.onClicked.addListener(function (data) {
if (data.menuItemId === 'menu-1') {
handleCopyLink(data);
}
if (data.menuItemId === "copyImage" && data.srcUrl) {
handleCopyImg(data.srcUrl);
}
if (data.menuItemId === "copyHtml") {
handleCopyHtml();
}
});
// 监听扩展程序安装事件
chrome.runtime.onInstalled.addListener(function () {
createContextMenu();
});
// 监听键盘快捷键事件
chrome.commands.onCommand.addListener(function (command) {
if (command === "copyLink") {
handleCopyLink();
}
});