-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
86 lines (74 loc) · 2.04 KB
/
popup.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
'use strict';
var cStorage = chrome.storage.sync;
var defaultCommand = {
makeHighlight: 'Ctrl_B',
scrollRedPoint: 'Shift_N',
clearData: 'Ctrl_Shift_L',
createFile: 'Ctrl_Shift_F'
};
function sendRequest(data) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, data, function (response) {
//console.log(response.mess);
});
});
}
(function main() {
cStorage.get(function (items) {
defaultCommand = $.isEmptyObject(items) ? defaultCommand : items['userDefine'];
console.log(defaultCommand);
$('#customCmd').find('input').each(function (e) {
$(this).val(defaultCommand[$(this).attr('prop')]);
});
});
})();
function sendJSON(data) {
if (data.length > 0) {
try {
JSON.parse(data);
// Send message request to content script
sendRequest({
type: 'addJson',
jsontext: data
});
} catch (e) {
return;
}
}
}
jQuery(document).ready(function ($) {
$('#form-import').submit(function (e) {
var el = $('#jsontext'),
json_val = el.val();
sendJSON(json_val);
el.val('').focus();
e.preventDefault();
});
$('#btn-file').click(function () {
$('#files').click();
});
$('#files').change(function (e) {
var file = e.target.files[0],
reader = new FileReader();
reader.onload = function (event) {
sendJSON(event.target.result);
};
reader.readAsText(file);
});
$('#customCmd').find('input').change(function (e) {
var prop = $(this).attr('prop'),
val = $(this).val();
defaultCommand[prop] = val;
cStorage.set({
'userDefine': defaultCommand
});
sendRequest({
type: 'custom_command',
data: defaultCommand
});
$('#customCmd').find('.alert').show();
});
});