-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.js
64 lines (56 loc) · 1.54 KB
/
note.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
var noteArea = document.getElementById('note');
var tabIDText = document.getElementById('tabIDText');
var currentTabID;
var storageKey;
var init = false;
var onTabLoaded = function () {
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function (tabs) {
var tab = tabs[0];
if (tab.url) {
currentTabID = tab.id;
storageKey = getStorageKey(tab);
tabIDText.innerHTML = currentTabID;
init = true;
// Load note value from chrome.storage.sync
chrome.storage.sync.get(storageKey, function (result) {
if (storageKey in result) {
noteArea.value = result[storageKey];
togglePageIcon(true, currentTabID);
} else {
togglePageIcon(false, currentTabID);
}
});
}
});
};
var onNoteChanged = function () {
if (init) {
var newNoteValue = noteArea.value;
if (newNoteValue === '') {
chrome.storage.sync.remove(storageKey);
} else {
var newNoteData = {};
newNoteData[storageKey] = newNoteValue;
chrome.storage.sync.set(newNoteData);
}
}
};
var onStorageChanged = function (changes, areaName) {
if (areaName === 'sync') {
// change page icon based on whether note is full
if (storageKey in changes && changes[storageKey].newValue) {
var newValue = changes[storageKey].newValue;
noteArea.value = newValue;
togglePageIcon(true, currentTabID);
} else {
togglePageIcon(false, currentTabID);
}
}
};
chrome.storage.onChanged.addListener(onStorageChanged);
document.addEventListener('DOMContentLoaded', onTabLoaded);
noteArea.onkeyup = onNoteChanged;
noteArea.onblur = onNoteChanged;