-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
67 lines (60 loc) 路 1.59 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
$(document).ready(function () {
$("#sessionStartBtn").click(function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
action: "START_SESSION",
});
});
// closing popup
window.close();
});
$("#removeCardBtn").click(function () {
if (confirmCancel()) {
chrome.storage.sync.set({ cards: [] }, function () {
console.log("all cards removed");
});
window.close();
}
});
// getting data from local storage
function onLoad() {
resetTextarea();
getFromStorage(function (cards) {
console.log(cards.length);
if (cards.length <= 10) {
const cardsRemaining = 10 - cards.length;
$("#popup-remainder").html(
`You can create <span id="remainder">${cardsRemaining}</span> more cards`
);
} else {
$("#popup-remainder").html(
`<p>You have already reached the limit. Please remove the old cards to add new cards to deck</p>`
);
}
});
}
// local storage
function getFromStorage(callback) {
chrome.storage.sync.get(["cards"], function (result) {
if (result && result.cards) {
callback(result.cards);
} else callback([]);
});
}
// reset text area
function resetTextarea() {
$("#question").val("");
$("answer").val("");
}
function confirmCancel() {
if (
window.confirm(
"The action will remove all cards from deck.Are you sure ?"
)
) {
return true;
} else return false;
}
// initial call
onLoad();
});