-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
77 lines (71 loc) · 2.04 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
window.addEventListener('injectScript', (event) => {
console.log(event.detail)
chrome.runtime.sendMessage({
type: 'demo',
data: event.detail.data
}, (response) => {
console.log('demo response:', response)
})
})
function onKeywordMenuClick(keyword) {
if (keyword.length > 10) {
confirm("选中的关键词长度超过 10 无法添加")
return
}
chrome.storage.sync.get({
keywords: '',
}, (result) => {
if (result.keywords) {
var keywords = result.keywords.split(',')
if (keywords.indexOf(keyword) > -1) {
confirm('【' + keyword + '】已存在,不能重复添加')
return
}
} else {
var keywords = []
}
keywords.push(keyword)
chrome.storage.sync.set({
keywords: keywords.join(',')
}, function () {
console.log("keyword change suc ");
confirm("关键词黑名单添加成功")
});
});
}
function onUserMenuClick(username) {
if (username.length > 10) {
confirm("选中的用户名长度超过 10 无法添加")
return
}
chrome.storage.sync.get({
usernames: '',
}, (result) => {
if (result.usernames) {
var usernames = result.usernames.split(',')
if (usernames.indexOf(username) > -1) {
confirm('【' + username + '】已存在,不能重复添加')
return
}
} else {
var usernames = []
}
usernames.push(username)
chrome.storage.sync.set({
usernames: usernames.join(',')
}, function () {
console.log("username change suc ");
confirm("用户黑名单添加成功")
});
});
}
chrome.contextMenus.create({
"title": "添加到微博关键词黑名单",
"contexts": ['selection'], // 只有当选中文字时才会出现此右键菜单
"onclick": function (params) { onKeywordMenuClick(params.selectionText) }
});
chrome.contextMenus.create({
"title": "添加到微博用户黑名单",
"contexts": ['selection'], // 只有当选中文字时才会出现此右键菜单
"onclick": function (params) { onUserMenuClick(params.selectionText) }
});