diff --git a/.jshintrc b/.jshintrc index a49c9ec..9f84909 100755 --- a/.jshintrc +++ b/.jshintrc @@ -7,7 +7,7 @@ // Enforcing "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) "camelcase" : false, // true: Identifiers must be in camelCase - "curly" : true, // true: Require {} for every new block or scope + "curly" : false, // true: Require {} for every new block or scope "eqeqeq" : true, // true: Require triple equals (===) for comparison "forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty() "freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc. diff --git a/src/background.js b/src/background.js index fb8e28c..72f946c 100755 --- a/src/background.js +++ b/src/background.js @@ -8,10 +8,19 @@ if(window === chrome.extension.getBackgroundPage()) { // execute only if in the extension.updateTabs(); - setInterval(() => { // TODO: update on the fly instead of setInterval - // db.dbInit(); - extension.updateTabs(); - }, 5000); + chrome.tabs.onCreated.addListener(function (tab) { + extension.updateTab(tab); + }); + + chrome.tabs.onUpdated.addListener(function (tabId) { + chrome.tabs.get(tabId, tab => extension.updateTab(tab)); + }); + + chrome.tabs.onRemoved.addListener(function (tabId) { + chrome.tabs.get(tabId, tab => extension.updateTab(tab)); + }); + + } exports.db = require('./db').db; \ No newline at end of file diff --git a/src/db/localStorageDb.js b/src/db/localStorageDb.js index 08a67ab..6e0ae49 100755 --- a/src/db/localStorageDb.js +++ b/src/db/localStorageDb.js @@ -64,6 +64,7 @@ function LocalStorageDb(options) { return new DbWrapper({data: _parseDbStr(curDbStr)}); } + function _parseDbStr(curDbStr) { let curDb = []; if (curDbStr) { // if not empty diff --git a/src/extension.js b/src/extension.js index ae50d0f..69b598c 100755 --- a/src/extension.js +++ b/src/extension.js @@ -1,33 +1,41 @@ "use strict"; -const helpers = require('./helpers'); const db = require('./db').db; /** * Scans tabs and saves to local storage. */ -function updateTabs() { // TODO: handle removal of tabs +function updateTabs(commit = false) { // TODO: handle removal of tabs if (!chrome.tabs) { // execute only in extension context return; } chrome.tabs.query({}, function (tabs) { - tabs.forEach(function (tab) { // async forEach - setTimeout(() => { - let dbEntry = { - id: String(helpers.hashCode(JSON.stringify(tab))), - source: "tab", - type: "tab", - selectedText: tab.title, - text: tab.url + '\n' + tab.title, - tab: tab - }; - console.log("Saving tab to localStorage: ", tab.title); - db.push(dbEntry); - // db.save(); - }, 0); - }); + setTimeout(tabs.forEach( + tab => updateTab(tab, commit) + ), 0); }); } +function updateTab(tab, commit = false) { + let dbEntry = { + id: String(tab.id), + source: "tab", + type: "tab", + selectedText: tab.title, + text: tab.url + '\n' + tab.title, + tab: tab + }; + console.log("Saving tab: ", tab.id, tab.title); + db.push(dbEntry); + if (commit) db.save(); +} + +function removeTab(tab, commit = false) { + db.delete(tab); + console.log("Removing tab: ", tab.id, tab.title); + if (commit) db.save(); +} -exports.updateTabs = updateTabs; \ No newline at end of file +exports.updateTabs = updateTabs; +exports.updateTab = updateTab; +exports.removeTab = removeTab; \ No newline at end of file