Skip to content

Commit

Permalink
Added files via upload
Browse files Browse the repository at this point in the history
- fixed bugs with selecting/toggling bookmarks
- added undo for last deleted bookmarks
- show instructions in the bookmarks list page if empty
- always show bookmarks list button (now renamed to Manage)
- scroll into view selected bookmarks in the list page
  • Loading branch information
Siderite committed May 4, 2016
1 parent e84696e commit 3a7df19
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 13 deletions.
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

"name" : "Siderite's Bookmark Explorer",
"description" : "Allows you to go to the previous/next item in a bookmark folder on the same page.",
"version" : "1.2.3",
"version" : "1.3.0",

"permissions" : [
"tabs",
"activeTab",
"bookmarks",
"contextMenus",
"notifications",
"chrome://favicon/"
"chrome://favicon/",
"storage"
],
"browser_action" : {
"default_icon" : "icon.png",
Expand Down
1 change: 1 addition & 0 deletions pagify.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ a img {
bottom:0px;
background:#eeeeee;
width: 100%;
height: 2em;
padding: 0.75em 2.25em;
}
#tree>div {
Expand Down
122 changes: 111 additions & 11 deletions pagify.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createTree(itm, level) {
if (itm.url) {
$('<a></a>')
.text(itm.title || itm.url)
.prepend($('<img/>').attr('src','chrome://favicon/'+itm.url))
.prepend($('<img/>').attr('src', 'chrome://favicon/' + itm.url))
.attr('href', itm.url || '#')
.attr('target', '_blank')
.appendTo(elem);
Expand All @@ -24,23 +24,55 @@ function createTree(itm, level) {
.attr('title', 'Mark for delete');
if (level == 2)
chk.appendTo(elem);
elem.click(function () {
chk.click();
elem.click(function (ev) {
if (!chk.is(ev.target)) {
chk.prop('checked', !chk.prop('checked'));
}
});
}
return elem;
}

function createEmpty() {
var elem = $('<div></div>');
elem.append($('<h1></h1>').text('Bookmark for the URL not found'));
$('<h4></h4>').text('Move to a tab that has been bookmarked to populate this page.').appendTo(elem);
return elem;
}

var currentData;
function refresh(data) {
currentData = data;
var tree = $('#tree').empty();
if (!data.folder)
if (!data || !data.folder) {
tree.append(createEmpty());
$('#btnToggleAll,#btnToggleBefore,#btnRemove').hide();
return;
}
$('#btnToggleAll,#btnToggleBefore,#btnRemove').show();
var bottom = $('#buttons').offset().top;
tree.append(createTree(data.folder, 1));
var minTop = null;
var maxTop = null;
$('a', '#tree').each(function () {
if (sameUrls($(this).attr('href'), data.current.url)) {
$(this).parent().addClass('selected');
var par = $(this).parent();
par.addClass('selected');
var ofs = par.offset();
if (minTop === null)
minTop = ofs.top;
if (maxTop === null || ofs.top + par.height() > maxTop)
maxTop = ofs.top + par.height();
}
});
if (minTop && maxTop) {
var y = (minTop + maxTop) / 2;
if (y >= bottom) {
$('html,body').animate({
scrollTop : y - bottom / 2
});
}
}
}

$(function () {
Expand All @@ -51,7 +83,7 @@ $(function () {
$('#tree>div>ul>li>div>input').prop('checked', val);
});
$('#btnToggleBefore').click(function () {
var index = $('#tree>div>ul>li:has(a.selected)').index();
var index = $('#tree>div>ul>li:has(div.selected)').index();
var val = $('#tree>div>ul>li>div>input:lt(' + index + '):checked').length < $('#tree>div>ul>li>div>input:lt(' + index + '):not(:checked)').length;
$('#tree>div>ul>li>div>input:lt(' + index + ')').prop('checked', val);
});
Expand All @@ -61,16 +93,84 @@ $(function () {
return;
if (!confirm('Are you sure you want to delete ' + inputs.length + ' bookmarks?'))
return;
var ids = [];
var ids = []
inputs.each(function () {
var id = + ($(this).val());
if (id) {
id += '';
var input = this;
chrome.bookmarks.remove(id, function () {
$(input).closest('li').remove();
ids.push({
id : id + '',
input : this
});
}
});
chrome.bookmarks.get(ids.map(function (p) {
return p.id;
}), function (bookmarks) {
chrome.storage.local.set({
'lastDeletedBookmarks' : bookmarks
}, function () {
if (chrome.runtime.lastError) {
notify('Error saving bookmarks before delete: ' + chrome.runtime.lastError.message);
} else {
$('#btnUndo').text('Restore ' + bookmarks.length + ' bookmarks').show();
ids.forEach(function (p) {
chrome.bookmarks.remove(p.id, function () {
$(p.input).closest('li').remove();
});
});
}
});
});
});
chrome.storage.local.get('lastDeletedBookmarks', function (data) {
if (data && data.lastDeletedBookmarks) {
$('#btnUndo').text('Restore ' + data.lastDeletedBookmarks.length + ' bookmarks').show();
} else {
$('#btnUndo').hide();
}
});
$('#btnUndo').click(function () {
chrome.storage.local.get('lastDeletedBookmarks', function (data) {
var bookmarks = data.lastDeletedBookmarks;
if (!confirm('Are you sure you want to restore ' + bookmarks.length + ' bookmarks?'))
return;
var parentIds = {};
bookmarks.forEach(function (bm) {
if (bm.parentId)
parentIds[bm.parentId] = true;
});
parentIds = Object.keys(parentIds);
getBookmarksById(parentIds, function (parentBookmarks) {
if (!parentBookmarks || parentBookmarks.filter(function (bm) {
return !!bm;
}).length != parentIds.length) {
notify('Parent bookmarks are missing, copying all in a new folder');
getBookmarkBarId(function (barId) {
chrome.bookmarks.create({
title : 'Undeleted items',
parentId : barId
}, function (parent) {
bookmarks.forEach(function (bm) {
bm.parentId = parent.id;
});
createBookmarks(bookmarks);
notify('Bookmarks restored');
chrome.storage.local.remove('lastDeletedBookmarks', function () {
$('#btnUndo').hide();
refresh(currentData);
});
});
});
} else {
createBookmarks(bookmarks);
notify('Bookmarks restored');
chrome.storage.local.remove('lastDeletedBookmarks', function () {
$('#btnUndo').hide();
refresh(currentData);
});
}
});
});
});
refresh();
});

0 comments on commit 3a7df19

Please sign in to comment.