Add remove and retag commands for keysnail bookmarks

This commit is contained in:
Tom Willemse 2015-11-18 01:15:10 +01:00
parent 0f5e07d009
commit b980af6a77

View file

@ -1,4 +1,5 @@
// ======================= KeySnail Init File ======================== // // ======================= KeySnail Init File ======================== //
/* jshint moz: true */
/*global key plugins hook command util gBrowser _content goDoCommand /*global key plugins hook command util gBrowser _content goDoCommand
* KeySnail KeyEvent userscript ext openHelpLink * KeySnail KeyEvent userscript ext openHelpLink
* BrowserCloseTabOrWindow closeWindow undoCloseTab OpenBrowserWindow * BrowserCloseTabOrWindow closeWindow undoCloseTab OpenBrowserWindow
@ -545,18 +546,30 @@ key.setViewKey([':', 't'], function (ev, arg) {
var oni = var oni =
(function() { (function() {
function _saveBookmark(tags, bookmark) { const Cc = Components.classes;
var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"] const Ci = Components.interfaces;
.getService(Components.interfaces.nsINavBookmarksService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var taggingSvc = Components.classes["@mozilla.org/browser/tagging-service;1"]
.getService(Components.interfaces.nsITaggingService);
var uri = ios.newURI(bookmark.url, null, null);
bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri, const bmService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
bmsvc.DEFAULT_INDEX, bookmark.title); .getService(Ci.nsINavBookmarksService);
taggingSvc.tagURI(uri, tags.split(',').map(String.trim)); const ioService = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
const tagService = Cc["@mozilla.org/browser/tagging-service;1"]
.getService(Ci.nsITaggingService);
function _saveBookmark(tags, bookmark) {
var uri = ioService.newURI(bookmark.url, null, null);
if (!bmService.isBookmarked(uri)) {
bmService.insertBookmark(bmService.bookmarksMenuFolder, uri,
bmService.DEFAULT_INDEX, bookmark.title);
tagService.tagURI(uri, tags.split(',').map(String.trim));
display.prettyPrint("Successfully bookmarked \"" + bookmark.title + "\"",
{timeout: 3000, fade: 250});
} else {
display.prettyPrint("Already bookmarked",
{timeout: 3000, fade: 250});
}
} }
function _readTags(url, bookmark) { function _readTags(url, bookmark) {
@ -587,10 +600,50 @@ var oni =
); );
} }
var self = { function addBookmark() {
addBookmark: function(arg) {
_readTitle(); _readTitle();
} }
function removeBookmark() {
var uri = ioService.newURI(window.content.location.href, null, null);
if (bmService.isBookmarked(uri)) {
bmService.getBookmarkIdsForURI(uri, {}).forEach(function(id) {
bmService.removeItem(id);
});
display.prettyPrint("Removed bookmark",
{timeout: 3000, fade: 250});
} else {
display.prettyPrint("No bookmark found for this page",
{timeout: 3000, fade: 250});
}
}
function tagBookmark() {
var uri = ioService.newURI(window.content.location.href, null, null);
if (bmService.isBookmarked(uri)) {
var tags = tagService.getTagsForURI(uri, {});
prompt.read("tags:", function(newTags) {
tagService.untagURI(uri, tags);
tagService.tagURI(uri, newTags.split(',').map(String.trim));
display.prettyPrint("Retagged bookmark",
{timeout: 3000, fade: 250});
}, null, null, tags.join(', '));
} else {
display.prettyPrint("No bookmark found for this page",
{timeout: 3000, fade: 250});
}
}
var self = {
addBookmark: addBookmark,
removeBookmark: removeBookmark,
tagBookmark: tagBookmark
}; };
return self; return self;
@ -598,7 +651,19 @@ var oni =
ext.add('oni-add-bookmark', function(ev, arg) { oni.addBookmark(arg); }, ext.add('oni-add-bookmark', function(ev, arg) { oni.addBookmark(arg); },
M({en: "oni - Add bookmark"})); M({en: "oni - Add bookmark"}));
ext.add('oni-rm-bookmark', function(ev, arg) { oni.removeBookmark(arg); },
M({en: "oni - Remove bookmark"}));
ext.add('oni-tag-bookmark', function(ev, arg) { oni.tagBookmark(arg); },
M({en: "oni - Tag bookmark"}));
key.setViewKey([':', 'a'], function(ev, arg) { key.setViewKey([':', 'a'], function(ev, arg) {
ext.exec("oni-add-bookmark", arg, ev); ext.exec("oni-add-bookmark", arg, ev);
}, "Bookmark this page"); }, "Bookmark this page");
key.setViewKey([':', 'K'], function(ev, arg) {
ext.exec("oni-rm-bookmark", arg, ev);
}, "Remove the bookmark for this page");
key.setViewKey([':', 'T'], function(ev, arg) {
ext.exec("oni-tag-bookmark", arg, ev);
}, "Change the tags for this page");