From 542a38e3f3ca8de79863eb01d5658e31f9eb319f Mon Sep 17 00:00:00 2001 From: "Eduardo R. D'Avila" Date: Mon, 11 May 2015 23:40:03 -0300 Subject: [PATCH 1/6] refactor nested loops --- extension/background.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/extension/background.js b/extension/background.js index 6f9b829..6fe6da3 100644 --- a/extension/background.js +++ b/extension/background.js @@ -18,20 +18,27 @@ function bookmarkAll(folderTitle) { }, function(windowList) { for (var i = 0; i < windowList.length; i++) { - for (var j = 0; j < windowList[i].tabs.length; j++) { - var tab = windowList[i].tabs[j]; - // Bookmark the tab - chrome.bookmarks.create( - { - parentId : folder.id, - title : tab.title, - url : tab.url - } - ); - } + bookmarkWindowTabs(windowList[i], folder); } } ); } ); } + +function bookmarkWindowTabs(window, folder) { + for (var j = 0; j < window.tabs.length; j++) { + var tab = window.tabs[j]; + bookmarkTab(folder, tab); + } +} + +function bookmarkTab(folder, tab) { + chrome.bookmarks.create( + { + parentId : folder.id, + title : tab.title, + url : tab.url + } + ); +} From 6efa964905c091491eab59d0110545f1d65d445e Mon Sep 17 00:00:00 2001 From: "Eduardo R. D'Avila" Date: Mon, 11 May 2015 23:28:44 -0300 Subject: [PATCH 2/6] add option to bookmark windows as folders --- extension/background.js | 27 ++++++++++++++++++++++++--- extension/popup.html | 3 ++- extension/script.js | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/extension/background.js b/extension/background.js index 6fe6da3..5a24995 100644 --- a/extension/background.js +++ b/extension/background.js @@ -1,4 +1,4 @@ -function bookmarkAll(folderTitle) { +function bookmarkAll(folderTitle, windowsAsFolders) { console.log("Creating new bookmarks in " + folderTitle); // Create folder chrome.bookmarks.create( @@ -18,7 +18,7 @@ function bookmarkAll(folderTitle) { }, function(windowList) { for (var i = 0; i < windowList.length; i++) { - bookmarkWindowTabs(windowList[i], folder); + bookmarkWindowTabs(windowList[i], folder, windowsAsFolders, i); } } ); @@ -26,7 +26,24 @@ function bookmarkAll(folderTitle) { ); } -function bookmarkWindowTabs(window, folder) { +function bookmarkWindowTabs(window, rootFolder, windowsAsFolders, index) { + if(windowsAsFolders) { + chrome.bookmarks.create( + { + parentId : rootFolder.id, + title : pad(index+1), + url : null + }, + function(windowFolder) { + bookmarkWindowTabsInFolder(window, windowFolder); + } + ); + } else { + bookmarkWindowTabsInFolder(window, rootFolder); + } +} + +function bookmarkWindowTabsInFolder(window, folder) { for (var j = 0; j < window.tabs.length; j++) { var tab = window.tabs[j]; bookmarkTab(folder, tab); @@ -42,3 +59,7 @@ function bookmarkTab(folder, tab) { } ); } + +function pad(n) { + return n<10 ? '0'+n : n +} diff --git a/extension/popup.html b/extension/popup.html index 7baba7b..093a580 100644 --- a/extension/popup.html +++ b/extension/popup.html @@ -9,7 +9,8 @@
- +
+
diff --git a/extension/script.js b/extension/script.js index 4f69129..6b45dc8 100644 --- a/extension/script.js +++ b/extension/script.js @@ -15,11 +15,12 @@ $(document).ready(function(){ $('form').submit(function(){ // This seems the stupidest way to get the value var folderName = $('#folderName').val(); + var windowsAsFolders = $('#windowsAsFolders').is(':checked'); window.close(); // Create bookmarks from background page // I couldn't get it to work from here // and there seemed to be a race condition with window.close() - chrome.extension.getBackgroundPage().bookmarkAll(folderName); + chrome.extension.getBackgroundPage().bookmarkAll(folderName, windowsAsFolders); return false; }); }); From 6cae63ea3166386305e23e64e61759febd6be012 Mon Sep 17 00:00:00 2001 From: "Eduardo R. D'Avila" Date: Mon, 11 May 2015 23:39:12 -0300 Subject: [PATCH 3/6] remember last option for checkbox --- extension/script.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extension/script.js b/extension/script.js index 6b45dc8..cfe229b 100644 --- a/extension/script.js +++ b/extension/script.js @@ -11,6 +11,8 @@ $(document).ready(function(){ // Set default value to current date $('#folderName').val(folderTitle); console.log("Set placeholder to " + folderTitle); + // Remember last option for checkbox + $('#windowsAsFolders').prop('checked', localStorage['windowsAsFolders'] == 'true'); // Handle button click $('form').submit(function(){ // This seems the stupidest way to get the value @@ -21,6 +23,8 @@ $(document).ready(function(){ // I couldn't get it to work from here // and there seemed to be a race condition with window.close() chrome.extension.getBackgroundPage().bookmarkAll(folderName, windowsAsFolders); + // Save option for checkbox + localStorage['windowsAsFolders'] = $('#windowsAsFolders').prop('checked') ? 'true' : 'false'; return false; }); }); From fb3b756b4535e55578b5bd49cdf9edb26bdd2bdc Mon Sep 17 00:00:00 2001 From: "Eduardo R. D'Avila" Date: Mon, 7 Sep 2015 23:34:34 -0300 Subject: [PATCH 4/6] Include the time in the created folder name --- extension/script.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/extension/script.js b/extension/script.js index cfe229b..ce6dbfe 100644 --- a/extension/script.js +++ b/extension/script.js @@ -1,10 +1,11 @@ function toShortISODateString(d) { - function pad(n) { - return n<10 ? '0'+n : n - } - return d.getFullYear()+'-' - + pad(d.getMonth()+1)+'-' - + pad(d.getDate()); + var pad = chrome.extension.getBackgroundPage().pad; + return d.getFullYear() + '-' + + pad(d.getMonth()+1) + '-' + + pad(d.getDate()) + ' ' + + pad(d.getHours()) + ':' + + pad(d.getMinutes()) + ':' + + pad(d.getSeconds()); } $(document).ready(function(){ var folderTitle = toShortISODateString(new Date()); From 3778a2ce4a889f291dbff795f84aa564ef024e43 Mon Sep 17 00:00:00 2001 From: "Eduardo R. D'Avila" Date: Tue, 8 Sep 2015 22:31:46 -0300 Subject: [PATCH 5/6] Include option to save in "Other Bookmarks" child folders --- extension/background.js | 23 +++++++++++++++++++++-- extension/popup.html | 11 +++++++---- extension/script.js | 26 ++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/extension/background.js b/extension/background.js index 5a24995..982e19a 100644 --- a/extension/background.js +++ b/extension/background.js @@ -1,10 +1,29 @@ -function bookmarkAll(folderTitle, windowsAsFolders) { +var OTHER_BOOKMARKS_ID = '2'; +function getParentFolderOptions(cb) { + chrome.bookmarks.get(OTHER_BOOKMARKS_ID, function(folders) { + var otherBookmarksFolder = folders[0]; + var results = [{id: otherBookmarksFolder.id, title: otherBookmarksFolder.title}]; + chrome.bookmarks.getChildren(OTHER_BOOKMARKS_ID, function(children) { + for(var i = 0; i < children.length; i++) { + var child = children[i]; + if(!child.url) { + results.push({id: child.id, title: otherBookmarksFolder.title + ' / ' + child.title}); + } + } + cb(results); + }); + }); +} + + +function bookmarkAll(folderTitle, parentFolderId, windowsAsFolders) { console.log("Creating new bookmarks in " + folderTitle); // Create folder chrome.bookmarks.create( { title: folderTitle, - url: null + url: null, + parentId: parentFolderId }, function(folder){ if (folder == null) { diff --git a/extension/popup.html b/extension/popup.html index 093a580..f947074 100644 --- a/extension/popup.html +++ b/extension/popup.html @@ -7,11 +7,14 @@ +

Bookmark All

- -
- - +
+
+
+

+ +

diff --git a/extension/script.js b/extension/script.js index ce6dbfe..f28e3ee 100644 --- a/extension/script.js +++ b/extension/script.js @@ -7,25 +7,47 @@ function toShortISODateString(d) { + pad(d.getMinutes()) + ':' + pad(d.getSeconds()); } + $(document).ready(function(){ var folderTitle = toShortISODateString(new Date()); // Set default value to current date $('#folderName').val(folderTitle); console.log("Set placeholder to " + folderTitle); + + var parentFolder = $('#parentFolder'); + chrome.extension.getBackgroundPage().getParentFolderOptions( + function(parentOptions) { + var previousParentFolderId = localStorage['parentFolderId']; + for(var i = 0; i < parentOptions.length; i++) { + var parentOption = parentOptions[i]; + var option = $('