Skip to content

Commit 1370c19

Browse files
committed
Added chrome compatibility
1 parent 2be1e07 commit 1370c19

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

tabnotes.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
11
var timeoutId;
22
const notes = document.getElementById("notes");
33
document.addEventListener("keyup", logKey);
4-
browser.tabs.onActivated.addListener(tabOpen);
5-
browser.windows.onFocusChanged.addListener(tabOpen);
4+
5+
const browser_type = getBrowser();
6+
if (browser_type === "Chrome") {
7+
var browser_obj = chrome;
8+
} else {
9+
var browser_obj = browser;
10+
}
11+
12+
browser_obj.tabs.onActivated.addListener(tabOpen);
13+
browser_obj.windows.onFocusChanged.addListener(tabOpen);
614

715
function logKey(e) {
816
clearTimeout(timeoutId);
917
timeoutId = setTimeout(function() {
10-
// Runs 1 second (1000 ms) after the last change
1118
saveToDB();
1219
}, 10);
1320
}
1421

22+
function getBrowser() {
23+
if (typeof chrome !== "undefined") {
24+
if (typeof browser !== "undefined") {
25+
return "Firefox";
26+
} else {
27+
return "Chrome";
28+
}
29+
} else {
30+
return "Edge";
31+
}
32+
}
33+
1534
function saveToDB() {
16-
browser.storage.sync.set({
35+
data = {
1736
tab_note: document.querySelector("#notes").value
18-
});
37+
};
38+
if (browser_type === "Chrome") {
39+
chrome.storage.sync.set(data, function() {});
40+
} else {
41+
browser_obj.storage.sync.set(data);
42+
}
1943
}
2044

2145
function tabOpen(tab) {
22-
browser.storage.sync.get("tab_note").then(result => {
23-
if (typeof result.tab_note !== "undefined") {
24-
document.querySelector("#notes").value = result.tab_note;
25-
}
26-
});
46+
if (browser_type === "Chrome") {
47+
chrome.storage.sync.get(["tab_note"], function(result) {
48+
if (typeof result.tab_note !== "undefined") {
49+
document.querySelector("#notes").value = result.tab_note;
50+
}
51+
});
52+
} else {
53+
browser_obj.storage.sync.get(["tab_note"]).then(result => {
54+
if (typeof result.tab_note !== "undefined") {
55+
document.querySelector("#notes").value = result.tab_note;
56+
}
57+
});
58+
}
2759
}
2860

2961
window.addEventListener("load", () => {

0 commit comments

Comments
 (0)