-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_plate.js
176 lines (157 loc) · 3.58 KB
/
command_plate.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var html = '';
var current_li = false;
$(document).keydown(function(event) {
if (event.ctrlKey) {
if (event.keyCode == 81) {
// CTRL + Q
toggle_plate();
}
}
if (event.keyCode == 27) {
hide_plate();
};
})
function set_up_plate() {
if (html == '') {
chrome.runtime.sendMessage({'action': 'command_plate'}, function(resp) {
html = resp.html;
$('body').append(html);
hide_plate();
fix_plate();
});
}
}
setTimeout(set_up_plate, 1000);
function toggle_plate() {
$('#pp_plate').toggle();
$('#cmd_field').focus();
}
function fix_plate() {
var plate = $('#pp_plate');
var w = $(window);
plate.css('top', (w.height() / 2 - 180) + 'px');
plate.css('left', (w.width() / 2 - 260) + 'px');
var cmd_list = plate.find('li');
cmd_list.hover(
function() {
current_li = parseInt(this.id);
set_li(current_li);
},
function() {
$(this).css('background-color', 'white');
$(this).css('color', 'black');
}
);
cmd_list.click(function() {
var cmd = $(this).attr('cmd');
exec_cmd(cmd);
})
$('#cmd_field').keyup(function(event) {
var kw = event.keyCode;
if (kw == 38 || kw == 40 || kw == 13) {
return;
}
var str = $(this).val();
var lis = $('#cmd_list li');
if (str.length <= 0) {
current_li = false;
lis.css('background-color', 'white').css('color', 'black');
return;
}
var cmds = {};
lis.each(function() {
cmds[$(this).attr('cmd')] = parseInt(this.id);
});
var t = 0.2;
for (var cmd in cmds) {
var s = cmd.score(str);
if (s > t) {
t = s;
current_li = cmds[cmd];
set_li(current_li);
}
}
});
$('#cmd_field').keydown(function(event) {
var kw = event.keyCode;
if (kw == 38) {
li_move_up();
}
if (kw == 40) {
li_move_down();
}
if (kw == 13) {
var cmd = $('#cmd_list').find('li:eq(' + current_li + ')').attr('cmd');
if (cmd) {
exec_cmd(cmd);
} else {
var cmd = $('#cmd_field').val();
var cmds = cmd.split(' ');
var new_cmds = [];
cmds.forEach(function(i) {
new_cmds.push(encodeURIComponent(i));
});
var url = SEARCH_PRODUCT_URL + new_cmds.join('+');
open_url(url);
}
}
});
}
function exec_cmd(cmd) {
if (cmd === 'upload product') {
chrome.runtime.sendMessage({
action: 'upload_product',
product: scratch()
});
} else if (cmd === 'search keyword') {
open_url(SEARCH_HOT_KEYWORD_URL);
} else if (cmd === 'manage product') {
open_url(MANAGE_PRODUCT_URL);
} else if (cmd === 'capture product') {
// invoke function from scratch.js
capture_product(scratch());
} else if (cmd === 'download images') {
download_images();
} else if (cmd === 'copy product') {
chrome.runtime.sendMessage({
action: 'copy_product',
product: scratch()
}, function(resp) {
if (resp.status) {
alert('复制成功');
};
});
} else if (cmd === 'paste product') {
paste_product();
}
toggle_plate();
}
function set_li(index) {
var ul = $('#cmd_list');
ul.find('li').css('background-color', 'white').css('color', 'black');
var li = ul.find('li:eq(' + index + ')');
li.css('background-color', '#0865bb').css('color', 'white');
var page = Math.floor(index/10);
ul.animate({scrollTop: page*311}, 500);
}
function li_move_up() {
var count = $('#cmd_list').find('li').length;
if (current_li === false || current_li <= 0) {
current_li = count - 1;
} else {
current_li -= 1;
}
set_li(current_li);
}
function li_move_down() {
var count = $('#cmd_list').find('li').length;
if (current_li === false || current_li >= count - 1) {
current_li = 0;
} else {
current_li += 1;
}
set_li(current_li);
}
function hide_plate() {
$('#pp_plate').hide();
}