-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtabnotes.js
63 lines (56 loc) · 1.4 KB
/
tabnotes.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 timeoutId;
const notes = document.getElementById("notes");
document.addEventListener("keyup", logKey);
const browser_type = getBrowser();
if (browser_type === "Chrome") {
var browser_obj = chrome;
} else {
var browser_obj = browser;
}
browser_obj.tabs.onActivated.addListener(tabOpen);
browser_obj.windows.onFocusChanged.addListener(tabOpen);
function logKey(e) {
clearTimeout(timeoutId);
timeoutId = setTimeout(function() {
saveToDB();
}, 10);
}
function getBrowser() {
if (typeof chrome !== "undefined") {
if (typeof browser !== "undefined") {
return "Firefox";
} else {
return "Chrome";
}
} else {
return "Edge";
}
}
function saveToDB() {
data = {
tab_note: document.querySelector("#notes").value
};
if (browser_type === "Chrome") {
chrome.storage.sync.set(data, function() {});
} else {
browser_obj.storage.sync.set(data);
}
}
function tabOpen(tab) {
if (browser_type === "Chrome") {
chrome.storage.sync.get(["tab_note"], function(result) {
if (typeof result.tab_note !== "undefined") {
document.querySelector("#notes").value = result.tab_note;
}
});
} else {
browser_obj.storage.sync.get(["tab_note"]).then(result => {
if (typeof result.tab_note !== "undefined") {
document.querySelector("#notes").value = result.tab_note;
}
});
}
}
window.addEventListener("load", () => {
tabOpen();
});