-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
143 lines (124 loc) · 3.76 KB
/
main.js
File metadata and controls
143 lines (124 loc) · 3.76 KB
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
$(() => {
let send_to_page = (data, callback) => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, data, callback);
});
}
// variables
let crn_field_template = $('#crn-template').html();
let message_div = $('.ac-message');
let form = $('.ac-crn-form');
var crn_list = [],
settings = [];
// version
$('#version').text(chrome.app.getDetails().version);
// get crn_list and settings
chrome.storage.local.get('crn_list', (object) => {
crn_list = object.crn_list;
if (crn_list && crn_list.length) {
crn_list.forEach((e) => {
append_crn_field(e);
})
} else {
append_crn_field();
}
});
chrome.storage.local.get('crn_settings', (object) => {
settings = object.crn_settings;
if (settings.avoid_session_invalid) {
$('#ac-settings-session').prop('checked', settings.avoid_session_invalid);
}
if (settings.auto_refresh) {
let enabled = settings.auto_refresh.enabled;
let hh = settings.auto_refresh.hour;
let mm = settings.auto_refresh.minute;
$('#ac-settings-refresh').prop('checked', enabled);
$('#ac-h').val(hh);
$('#ac-m').val(mm);
}
})
// event listeners
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type == "ac_show_msg") {
message_div.text(request.message).css(request.color);
}
});
let append_crn_field = (value) => {
if (form.find('.ac-crn').length >= 10) {
return;
}
let new_field = $(crn_field_template)
new_field.find('.ac-btn-del').on('click', () => {
if (form.find('.ac-crn').length <= 1) {
return;
}
new_field.remove();
});
if (value) {
new_field.find('.ac-input').val(value);
}
form.append(new_field);
}
$('.ac-btn-master').on('click', () => { window.open('https://banweb.cityu.edu.hk/pls/PROD/hwscrssh_cityu.P_SelTerm'); });
$('.ac-btn-add').on('click', () => { append_crn_field(); });
$('.ac-btn-save').on('click', () => {
crn_list = [];
var valid = true;
form.find('.ac-input').each((index, el) => {
let value = $(el).val();
if (value.match(/^[0-9]{4,5}$/)) {
crn_list.push(value);
} else {
valid = false;
}
});
if (valid) {
chrome.storage.local.set({crn_list: crn_list}, () => {
message_div.text('CRNs saved.').css('color', 'green');
});
} else {
message_div.text('Invalid CRN Number!').css('color', 'red');
}
});
let save_settings = (reload = true) => {
chrome.storage.local.set({
crn_settings: {
avoid_session_invalid: $('#ac-settings-session').prop('checked'),
auto_refresh: {
enabled: $('#ac-settings-refresh').prop('checked'),
hour: parseInt($('#ac-h').val()),
minute: parseInt($('#ac-m').val())
}
}
}, () => {
if (reload) {
message_div.text('Settings saved.').css('color', 'green');
send_to_page({type: 'ac_reload'})
}
});
}
$('#ac-settings-session').on('change', () => {
save_settings();
});
save_settings(false);
$('#ac-settings-refresh').on('change', () => {
if ($('#ac-settings-refresh').prop('checked')) {
var valid = true;
[$('#ac-h').val(), $('#ac-m').val()].forEach((e) => {
let int_value = window.parseInt(e);
// rough check
if (isNaN(int_value) || int_value < 0 || int_value >= 60) {
valid = false;
}
})
if (valid) {
save_settings();
} else {
$('#ac-settings-refresh').prop('checked', false);
message_div.text('AutoCRN: Invalid Time').css('color', 'red')
}
} else {
save_settings();
}
});
});