|
1 | 1 | var timeoutId;
|
2 | 2 | const notes = document.getElementById("notes");
|
3 | 3 | 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); |
6 | 14 |
|
7 | 15 | function logKey(e) {
|
8 | 16 | clearTimeout(timeoutId);
|
9 | 17 | timeoutId = setTimeout(function() {
|
10 |
| - // Runs 1 second (1000 ms) after the last change |
11 | 18 | saveToDB();
|
12 | 19 | }, 10);
|
13 | 20 | }
|
14 | 21 |
|
| 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 | + |
15 | 34 | function saveToDB() {
|
16 |
| - browser.storage.sync.set({ |
| 35 | + data = { |
17 | 36 | 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 | + } |
19 | 43 | }
|
20 | 44 |
|
21 | 45 | 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 | + } |
27 | 59 | }
|
28 | 60 |
|
29 | 61 | window.addEventListener("load", () => {
|
|
0 commit comments