Skip to content

Commit

Permalink
Added chrome compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nsht committed May 16, 2020
1 parent 2be1e07 commit 1370c19
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tabnotes.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
var timeoutId;
const notes = document.getElementById("notes");
document.addEventListener("keyup", logKey);
browser.tabs.onActivated.addListener(tabOpen);
browser.windows.onFocusChanged.addListener(tabOpen);

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() {
// Runs 1 second (1000 ms) after the last change
saveToDB();
}, 10);
}

function getBrowser() {
if (typeof chrome !== "undefined") {
if (typeof browser !== "undefined") {
return "Firefox";
} else {
return "Chrome";
}
} else {
return "Edge";
}
}

function saveToDB() {
browser.storage.sync.set({
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) {
browser.storage.sync.get("tab_note").then(result => {
if (typeof result.tab_note !== "undefined") {
document.querySelector("#notes").value = result.tab_note;
}
});
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", () => {
Expand Down

0 comments on commit 1370c19

Please sign in to comment.