/
background.js
83 lines (74 loc) · 2.26 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
chrome.runtime.onInstalled.addListener(function (reason) {
chrome.contextMenus.create({
id: 'article-share',
title: '快速分享',
contexts: ['all']
});
//分割线
chrome.contextMenus.create({
type: "separator",
});
chrome.contextMenus.create({
id: 'test-menu',
title: '第二个菜单',
contexts: ['all']
});
chrome.contextMenus.create({
id: "test-menu-2.1",
parentId: "test-menu",
title: "浏览我的博客",
contexts: ["all"],
onclick: function (params){
chrome.tabs.create({
url: "https://willwong.gitee.io/will/"
})
}
});
if (reason === chrome.runtime.OnInstalledReason.INSTALL) {
checkCommandShortcuts();
}
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
switch(info.menuItemId){
case 'article-share':
handleArticleShare();
break;
}
});
chrome.commands.onCommand.addListener((command) => {
console.log(`Command: ${command}`);
if(command === 'run-share'){
handleArticleShare();
}
});
function handleArticleShare(){
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const title = tabs[0].title
const url = tabs[0].url
var copyFrom = document.createElement("textarea")
copyFrom.setAttribute("id","copyFrom")
copyFrom.value = `[${title}](${url})`
document.body.appendChild(copyFrom)
copyFrom.select();
document.execCommand('copy');
copyFrom.remove();
});
}
function checkCommandShortcuts() {
// 获取当前插件已注册的快捷键
chrome.commands.getAll((commands) => {
let missingShortcuts = [];
for (let {name, shortcut} of commands) {
// 如果冲突无法注册快捷键默认值,则 shortcut 值为空字符串
if (shortcut === '') {
missingShortcuts.push(name);
}
}
// 如果该扩展程序与其他扩展程序真的存在快捷键冲突
if (missingShortcuts.length > 0) {
// Update the extension UI to inform the user that one or more
// commands are currently unassigned.
console.log("您的快捷键存在冲突")
}
});
}