-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.js
96 lines (78 loc) · 2.08 KB
/
context.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
92
93
94
95
96
/**
* Created on : 2016-11-30 14:39:32 星期三
* Encoding : UTF-8
* Description: 后台脚本
*
* @author @齐士博 <qii404.me>
*/
var qii404 = {
/*
* 右键菜单标题
*/
menuContext: '高亮页面内容 %s',
/*
* 创建右键菜单标识
*/
menuCreated: false,
/*
* 初始化
*/
init: function() {
this.bindRequest();
this.bindCommand();
},
/*
* 绑定接受content消息
*/
bindRequest: function() {
var this_ = this;
chrome.extension.onRequest.addListener(function (request){
console.log('receving content message', request);
if (request['action'] === 'createMenu' && !this_.menuCreated) {
this_.createHighlightMenu();
this_.menuCreated = true;
}
});
},
/*
* 绑定快捷键命令
*/
bindCommand: function () {
var this_ = this;
chrome.commands.onCommand.addListener(function (command) {
console.log('get command: ', command);
switch (command)
{
case 'highlight':
this_.contentHighlight();
break;
}
});
},
/*
* 创建右键菜单
*/
createHighlightMenu: function() {
console.log('creating menu...');
var menuProperties = {
'title' : this.menuContext,
'contexts' : ['all'],
'onclick' : this.contentHighlight
};
chrome.contextMenus.create(menuProperties);
},
/*
* 被点击 告知content进行高亮操作
*/
contentHighlight: function(clickData, tab)
{
console.log('telling content to highlight...');
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {'action': 'highlight'}, function(response) {
response && console.log('background reveved from content...', response);
});
});
}
}
qii404.init();
// end of file context.js