2021-02-23 03:02:19 +00:00
|
|
|
'use strict';
|
|
|
|
let bookmarkingTabs = false;
|
|
|
|
|
|
|
|
function createFolder(callback) {
|
|
|
|
chrome.bookmarks.create({'title': 'Autobookmarked Tabs'}, function(folder) {
|
2021-02-23 03:08:32 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
setActionStatus('Error bookmarking tabs', 'error', function() {});
|
|
|
|
return;
|
|
|
|
}
|
2021-02-23 03:02:19 +00:00
|
|
|
chrome.storage.local.set({'bookmarksFolder': folder.id});
|
|
|
|
callback(folder.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOrCreateFolder(callback) {
|
|
|
|
chrome.storage.local.get('bookmarksFolder', function (result) {
|
|
|
|
if ('bookmarksFolder' in result) {
|
|
|
|
let id = result.bookmarksFolder;
|
|
|
|
chrome.bookmarks.get(id, function(result) {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
// Bookmark id not valid, create new one
|
|
|
|
createFolder(callback);
|
|
|
|
} else {
|
|
|
|
callback(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
createFolder(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setActionStatus(title, imageset, callback) {
|
|
|
|
chrome.browserAction.setTitle({title: title}, function() {
|
|
|
|
chrome.browserAction.setIcon({path: {
|
|
|
|
'32': 'icons/' + imageset + '-32.png',
|
|
|
|
'48': 'icons/' + imageset + '-48.png',
|
|
|
|
'96': 'icons/' + imageset + '-96.png'
|
|
|
|
}}, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bookmarkTabs() {
|
|
|
|
if (bookmarkingTabs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarkingTabs = true;
|
|
|
|
|
|
|
|
let now = new Date();
|
2021-02-23 06:37:34 +00:00
|
|
|
let dateString = [now.getFullYear().toString(), (now.getMonth() + 1).toString().padStart(2, '0'), now.getDate().toString().padStart(2, '0')].join('-');
|
2021-02-23 03:02:19 +00:00
|
|
|
let timeString = [now.getHours().toString().padStart(2, '0'), now.getMinutes().toString().padStart(2, '0')].join(':');
|
|
|
|
setActionStatus('Bookmarking tabs…', 'processing', function() {
|
|
|
|
getOrCreateFolder(function(rootId) {
|
|
|
|
chrome.bookmarks.create({'title': dateString + ' ' + timeString, parentId: rootId}, function(folder) {
|
2021-02-23 03:08:32 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
setActionStatus('Error bookmarking tabs', 'error', function() {});
|
|
|
|
return;
|
|
|
|
}
|
2021-02-23 03:02:19 +00:00
|
|
|
chrome.tabs.query({}, function(tabs) {
|
|
|
|
let remaining = tabs.length;
|
|
|
|
for (let tab of tabs) {
|
|
|
|
chrome.bookmarks.create({'title': tab.title, url: tab.url, parentId: folder.id}, function() {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
setActionStatus('Error bookmarking tabs', 'error', function() {});
|
2021-02-23 03:08:32 +00:00
|
|
|
return;
|
2021-02-23 03:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remaining--;
|
|
|
|
if (remaining === 0) {
|
|
|
|
bookmarkingTabs = false;
|
|
|
|
setActionStatus('Bookmark open tabs', 'icon', function() {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.browserAction.onClicked.addListener(bookmarkTabs);
|
|
|
|
|
|
|
|
chrome.runtime.onInstalled.addListener(function () { getOrCreateFolder(function () {}); });
|