Skip to content

Commit

Permalink
Changed background tracking logic to tab event listeners rather than …
Browse files Browse the repository at this point in the history
…setInterval. Added support for tab deletions.
  • Loading branch information
roman-spiridonov committed Jan 19, 2017
1 parent 695be8f commit 8f59a4f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 13 additions & 4 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions src/db/localStorageDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function LocalStorageDb(options) {
return new DbWrapper({data: _parseDbStr(curDbStr)});
}


function _parseDbStr(curDbStr) {
let curDb = [];
if (curDbStr) { // if not empty
Expand Down
44 changes: 26 additions & 18 deletions src/extension.js
Original file line number Diff line number Diff line change
@@ -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;
exports.updateTabs = updateTabs;
exports.updateTab = updateTab;
exports.removeTab = removeTab;

0 comments on commit 8f59a4f

Please sign in to comment.