-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
79 lines (67 loc) · 2.15 KB
/
options.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
$(function() { //once options is opened
chrome.storage.sync.get(['limit','total'],function(budget) {
if(budget.limit && budget.limit != 0) {
$('#options-limit').val(budget.limit); //writes the limit in the limit box (check question #2)
$('#remove-limit').prop('disabled', false);
}
if(budget.total) {
$('#reset-total').prop('disabled', false);
}
})
$('#options-limit').keyup(function() { //check if limit box is empty
checkTextBox();
})
$('#save-limit').click(function() {
var limit = parseFloat($('#options-limit').val());
var limit = parseFloat(limit.toFixed(2));
if(limit < 0) {
alert("Can't remember the last time a negative limit was possible");
return;
}
chrome.storage.sync.set({'limit':limit});
chrome.storage.sync.get(['total','limit'], function(budget) {
if(budget.total) {
var newOverLimit = budget.total - budget.limit;
var newAvailable = budget.limit - budget.total;
if(newOverLimit < 0) {
newOverLimit = 0;
}
if(newAvailable < 0) {
newAvailable = 0;
}
chrome.storage.sync.set({'overlimit':newOverLimit,'available':newAvailable});
}
})
$('#remove-limit').prop('disabled', false);
alert("Successful");
});
$('#remove-limit').click(function(){
chrome.storage.sync.remove(['limit','overlimit','available']);
$('#options-limit').val(''); //clears the input box
$('#save-limit').prop('disabled', true);
$('#remove-limit').prop('disabled', true);
alert("Successful"); //closes the tab
});
$('#reset-total').click(function(){
chrome.storage.sync.remove(['total','overlimit']);
chrome.storage.sync.get('limit',function(budget) {
if(budget.limit) {
var newAvailable = budget.limit;
chrome.storage.sync.set({'available':newAvailable});
$('#available').text(budget.available);
}
})
$('#reset-total').prop('disabled', true);
alert("Successful");
});
//if limit box is empty, disable the submit button, otherwise enable it
var checkTextBox = function() {
var check = $('#options-limit').val();
if(check == "") {
$('#save-limit').prop('disabled', true);
}
else {
$('#save-limit').prop('disabled', false);
}
}
});