Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bookmark folder for each window #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
function bookmarkAll(folderTitle) {
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) {
Expand All @@ -18,20 +37,48 @@ 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, windowsAsFolders, i);
}
}
);
}
);
}

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);
}
}

function bookmarkTab(folder, tab) {
chrome.bookmarks.create(
{
parentId : folder.id,
title : tab.title,
url : tab.url
}
);
}

function pad(n) {
return n<10 ? '0'+n : n
}
14 changes: 11 additions & 3 deletions extension/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
body {
min-width:200px;
}
label {
white-space: nowrap;
}
</style>
</head>
<body>
<h1 style="text-align:center;">Bookmark All</h1>
<form>
<label>Folder Name</label>
<input id="folderName">
<button type="submit">Bookmark</button>
Bookmark all open tabs ...<br>
<label>... to a new folder named <input id="folderName"></label> ...<br>
<label>... created inside <select id="parentFolder"></select>.</label><br>
<label><input type="checkbox" id="windowsAsFolders">Create a sub-folder ("01", "02", ...) for each window.</label><br>
<p style="text-align:center;">
<button type="submit">Bookmark</button>
</p>
</form>
<script src="jquery-1.10.2.min.js"></script>
<script src="script.js"></script>
Expand Down
42 changes: 35 additions & 7 deletions extension/script.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
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());
// 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 = $('<option>');
option.attr('value', parentOption.id);
option.text(parentOption.title);
if(parentOption.id == previousParentFolderId) {
option.prop('selected', true);
}
option.appendTo(parentFolder);
}
}
);

// 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
var folderName = $('#folderName').val();
var parentFolderId = parentFolder.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, parentFolderId, windowsAsFolders);
// Save options
localStorage['windowsAsFolders'] = $('#windowsAsFolders').prop('checked') ? 'true' : 'false';
localStorage['parentFolderId'] = parentFolderId;
return false;
});
});