Skip to content

Commit

Permalink
Merge pull request #25 from DaniloNovakovic/v2
Browse files Browse the repository at this point in the history
v2.2.5 - bugfixes and small code refactored
  • Loading branch information
DaniloNovakovic authored Oct 15, 2018
2 parents 1b1a154 + f932032 commit 6af3531
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-dynamic-bookmarks",
"version": "2.2.2",
"version": "2.2.5",
"description": "Chrome extension which dynamically updates bookmarks based on the specified regular expression.",
"scripts": {
"dev": "webpack --mode development",
Expand Down
32 changes: 15 additions & 17 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as dbm from './lib/dynBookmarks';

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (!changeInfo.url) return;

const newUrl = changeInfo.url;

// get bookmarks
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
let dynBook = dynBookmarks || {};
dbm.findAll((err, dynBook) => {
if (err) console.warn(err);
for (let id in dynBook) {
let { regExp } = dynBook[id];
try {
Expand All @@ -29,18 +31,13 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
});

chrome.bookmarks.onRemoved.addListener((id) => {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
let dynBook = dynBookmarks || {};
if (dynBook[id]) {
delete dynBook[id];
dbm.findByIdAndRemove(id, (err) => {
if (err) {
console.warn(err);
} else {
console.log(
`Successfully removed bookmark with id of ${id} from storage`
);
chrome.storage.sync.set({ dynBookmarks: dynBook }, () => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
}
});
}
});
});
Expand All @@ -49,16 +46,17 @@ chrome.bookmarks.onRemoved.addListener((id) => {
const maxHistorySize = 10;
chrome.bookmarks.onChanged.addListener((id, changeInfo) => {
if (changeInfo.url) {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
const dynBook = dynBookmarks || {};
if (dynBook[id]) {
dbm.findAll((err, dynBook) => {
if (err) {
console.warn(err);
} else if (dynBook[id]) {
if (dynBook[id].history.length >= maxHistorySize) {
dynBook[id].history.pop();
}
dynBook[id].history.unshift(changeInfo.url);
chrome.storage.sync.set({ dynBookmarks: dynBook }, () => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
dbm.overwrite(dynBook, (err) => {
if (err) {
console.warn(err);
}
});
}
Expand Down
71 changes: 50 additions & 21 deletions src/js/bookmarkManager/folderInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
renderChildren,
createChildInfoId
} from '../utils/folderInfo';
import * as dynBookmarks from '../lib/dynBookmarks';

const {
trackedFileIconColor,
Expand All @@ -18,21 +19,29 @@ document.addEventListener('DOMContentLoaded', () => {

chrome.bookmarks.onCreated.addListener((id, bookmark) => {
setTimeout(() => {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
const dynBook = dynBookmarks || {};
const color = dynBook[bookmark.id]
? trackedFileIconColor
: defaultFileIconColor;
dynBookmarks.findById(bookmark.id, (err, dynBookItem) => {
if (err) console.warn(err);
const color = dynBookItem ? trackedFileIconColor : defaultFileIconColor;
const childrenList = document.getElementById('folder-children-info');
if (childrenList) {
childrenList.appendChild(
createFolderInfoChild(
bookmark.id,
bookmark.title,
bookmark.url,
color
)
const infoChild = createFolderInfoChild(
bookmark.id,
bookmark.title,
bookmark.url,
color
);
if (infoChild) {
childrenList.appendChild(infoChild);
} else {
console.warn(
`failed to create infoChild ${JSON.stringify({
id: bookmark.id,
title: bookmark.title,
url: bookmark.url,
color
})}`
);
}
}
});
}, 100);
Expand Down Expand Up @@ -61,18 +70,31 @@ function initFolderInfo() {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
} else {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
const dynBook = dynBookmarks || {};
dynBookmarks.findAll((err, dynBook) => {
if (err) console.warn(err);
childrenList.innerHTML = '';
for (let child of results) {
findLeafNodes(child, (node) => {
const color = dynBook[node.id]
? trackedFileIconColor
: defaultFileIconColor;

childrenList.appendChild(
createFolderInfoChild(node.id, node.title, node.url, color)
const infoChild = createFolderInfoChild(
node.id,
node.title,
node.url,
color
);
if (infoChild) {
childrenList.appendChild(infoChild);
} else {
console.warn(
`failed to create infoChild ${JSON.stringify({
id: node.id,
title: node.title,
url: node.url
})}`
);
}
});
}
});
Expand All @@ -88,9 +110,14 @@ function initFolderInfo() {
* @param {string} url - (bookmark.url - url to which bookmark points to)
* @param {string} color - materializecss className color of text (note: lightened/darkened are added automatically so don't use them)
*/
function createFolderInfoChild(id, title, url, color = defaultFolderIconColor) {
if (!id || !url || !title) {
console.warn('in createFolderInfo id, url or/and title were invalid');
function createFolderInfoChild(
id,
title = 'unknown',
url = 'about:blank',
color = defaultFolderIconColor
) {
if (!id) {
console.warn('in createFolderInfoChild id was undefined');
return null;
}
let hostName = url.match(/^(http[s]?:\/\/.*?\/)/i);
Expand All @@ -99,7 +126,9 @@ function createFolderInfoChild(id, title, url, color = defaultFolderIconColor) {
hostName = hostName[0].replace(/http[s]:\/\//, '');
faviconLink = 'https://www.google.com/s2/favicons?domain=' + hostName;
} else {
faviconLink = '../images/default_favicon.png';
// i used this instead of ../images/ because default_favicon is located
// in same folder after `npm run dev` (or build)
faviconLink = './default_favicon.png';
}

return div(
Expand Down
12 changes: 4 additions & 8 deletions src/js/bookmarkManager/footerButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { clearSearchBar, disableSearchFilter } from '../utils/searchBar';
import { displayFolderInfo } from './displayFunctions';
import globalSelectHandler from './selectHandler';
import * as dynBookmarks from '../lib/dynBookmarks';

document.addEventListener('DOMContentLoaded', () => {
const editBtn = document.getElementById('edit-btn');
Expand Down Expand Up @@ -94,14 +95,9 @@ document.addEventListener('DOMContentLoaded', () => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
} else if (regExp) {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
const dynBook = dynBookmarks || {};
dynBook[bookmark.id] = {
...dynBook[bookmark.id],
regExp,
history: []
};
chrome.storage.sync.set({ dynBookmarks: dynBook });
dynBookmarks.findByIdAndUpdate(bookmark.id, {
regExp,
history: []
});
}
const modal = document.getElementById('addFileModal');
Expand Down
13 changes: 9 additions & 4 deletions src/js/bookmarkManager/managerForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hideInfoEditForm
} from '../utils/managerForm';
import { enableSearchFilter } from '../utils/searchBar';
import * as dynBookmarks from '../lib/dynBookmarks';

document.addEventListener('DOMContentLoaded', () => {
/* info-edit form */
Expand Down Expand Up @@ -39,8 +40,8 @@ document.addEventListener('DOMContentLoaded', () => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
} else {
chrome.storage.sync.get(['dynBookmarks'], ({ dynBookmarks }) => {
const dynBook = dynBookmarks || {};
dynBookmarks.findAll((err, dynBook) => {
if (err) return console.warn(err);
if (regExp) {
dynBook[bookmark.id] = {
...dynBook[bookmark.id],
Expand All @@ -56,8 +57,12 @@ document.addEventListener('DOMContentLoaded', () => {
title,
url
});
chrome.storage.sync.set({ dynBookmarks: dynBook }, () => {
updateTreeColor();
dynBookmarks.overwrite(dynBook, (err) => {
if (err) {
console.warn(err);
} else {
updateTreeColor();
}
});
});
}
Expand Down
109 changes: 109 additions & 0 deletions src/js/lib/dynBookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const dynBookmarksPropName = 'dynBookmarks';

/**
* finds dynBook object in hash map form: `{ bookmark_id: { regExp:String, history:[String] } }`
* @param {function} done - callback function called with `done(error, dynBook)`
*/
export function findAll(done) {
chrome.storage.sync.get([dynBookmarksPropName], (result) => {
if (chrome.runtime.lastError) {
done(chrome.runtime.lastError.message);
} else {
const dynBook = result[dynBookmarksPropName] || {};
done(null, dynBook);
}
});
}

/**
* finds dynBook[id] in form `{regExp:String, history: [String]}`
* @param {string} id - id of dynamic bookmark
* @param {function} done - callback function called with `done(error, dynBookItem)`
*/
export function findById(id, done) {
findAll((err, dynBook) => {
if (err) done(err);
else done(null, dynBook[id]);
});
}

/**
* Updates dynamic bookmark with given id (creates one if it doesn't exist)
* @param {string} id - id of dynamic bookmark
* @param {object} options - `{regExp: String, history:[String]}`
* @param {function} done - (optional) callback function called with done(error, updatedDynBookItem)
*/
export function findByIdAndUpdate(id, options, done) {
findAll((err, dynBook) => {
if (err) {
if (typeof done == 'function') {
done(err);
} else {
console.warn(err);
}
} else {
dynBook[id] = {
regExp: options.regExp || dynBook[id].regExp,
history: options.history || dynBook[id].history
};
overwrite(dynBook, (err) => {
if (typeof done == 'function') {
if (err) done(err);
else done(null, dynBook[id]);
}
});
}
});
}

/**
* Overwrites dynamic bookmarks object from storage with `newDynBook`.
* `Warning`: This function is DANGEROUS! Potential data loss!
* @param {object} newDynBook - new dynamic bookmarks object in form `{bookmark_id: {regExp: String, history:[String]}}`
* @param {function} done - callback function called with done(error)
*/
export function overwrite(newDynBook, done) {
chrome.storage.sync.set({ [dynBookmarksPropName]: newDynBook }, () => {
if (typeof done == 'function') {
if (chrome.runtime.lastError) {
done(chrome.runtime.lastError.message);
} else {
done(null);
}
}
});
}

/**
* Creates dynBookmark item and sets it into the storage
* @param {object} props - `{id:String, regExp:String, history:[String] (optional)}`
* @param {function} done - callback function called with (err, createdItem)
*/
export function create(props, done) {
if (!props.id || !props.regExp) {
done('id or regExp props are missing in dynBookmarks.create!');
}
findByIdAndUpdate(
props.id,
{
regExp: props.regExp,
history: props.history || []
},
done
);
}

export function findByIdAndRemove(id, done) {
findAll((err, dynBook) => {
if (err) {
if (typeof done == 'function') {
done(err);
} else {
console.warn(err);
}
} else if (dynBook[id]) {
delete dynBook[id];
overwrite(dynBook, done);
}
});
}
Loading

0 comments on commit 6af3531

Please sign in to comment.