Last active
September 25, 2015 19:57
-
-
Save 958/976073 to your computer and use it in GitHub Desktop.
[keysnail]ライブブックマークを見る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PLUGIN_INFO = | |
<KeySnailPlugin> | |
<name>Live bookmark viewer</name> | |
<description lang="ja">ライブブックマークを見る</description> | |
<updateURL>https://gist.github.com/958/976073/raw/livebookmark.ks.js</updateURL> | |
<iconURL>https://sites.google.com/site/958site/Home/files/livebookmark.ks.png</iconURL> | |
<license>MIT</license> | |
<minVersion>1.8.0</minVersion> | |
<author>958</author> | |
<version>0.0.5</version> | |
<detail><![CDATA[ | |
=== 概要 === | |
ライブブックマークを閲覧します | |
* Firefox 13.0 以上でのみ動作します | |
]]></detail> | |
</KeySnailPlugin>; | |
let pOptions = plugins.setupOptions("live_bookmark", { | |
"folders_keymap": { | |
preset: { | |
"C-z" : "prompt-toggle-edit-mode", | |
"SPC" : "prompt-next-page", | |
"b" : "prompt-previous-page", | |
"j" : "prompt-next-completion", | |
"k" : "prompt-previous-completion", | |
"g" : "prompt-beginning-of-candidates", | |
"G" : "prompt-end-of-candidates", | |
"q" : "prompt-cancel", | |
// | |
"l" : "show-items", | |
"o" : "open-home", | |
}, | |
description: M({ | |
ja: "フォルダ一覧の操作用キーマップ", | |
en: "Local keymap for folder list" | |
}) | |
}, | |
"items_keymap": { | |
preset: { | |
"C-z" : "prompt-toggle-edit-mode", | |
"SPC" : "prompt-next-page", | |
"b" : "prompt-previous-page", | |
"j" : "prompt-next-completion", | |
"k" : "prompt-previous-completion", | |
"g" : "prompt-beginning-of-candidates", | |
"G" : "prompt-end-of-candidates", | |
"q" : "prompt-cancel", | |
// | |
'o' : 'open', | |
'h' : 'select-folder', | |
}, | |
description: M({ | |
ja: "リンク一覧の操作用キーマップ", | |
en: "Local keymap for items" | |
}) | |
}, | |
}, PLUGIN_INFO); | |
const FOLDER_ICON = "chrome://browser/skin/livemark-folder.png"; | |
const ITEM_ICON = "chrome://mozapps/skin/places/defaultFavicon.png"; | |
Components.utils.import("resource://gre/modules/Promise.jsm"); | |
let liveBookmarkFolders = []; | |
function LivemarkFolder(aFolder) { | |
this.folder = aFolder; | |
this.completed = false; | |
this.children = null; | |
getLivemark(aFolder) | |
.then((aLivemark) => this.getLivemark(aLivemark)); | |
} | |
LivemarkFolder.prototype = { | |
getLivemark: function(aLivemark) { | |
this.livemark = aLivemark; | |
this.children = aLivemark.getNodesForContainer(this.folder); | |
this.completed = true; | |
}, | |
nodeInserted: function() { }, | |
nodeRemoved: function() { }, | |
nodeAnnotationChanged: function() { }, | |
nodeTitleChanged: function() { }, | |
nodeHistoryDetailsChanged: function() { }, | |
nodeReplaced: function() { }, | |
nodeMoved: function() { }, | |
ontainerStateChanged: function () { }, | |
sortingChanged: function() { }, | |
batching: function() { }, | |
invalidateContainer: function(aNode) { | |
try { | |
if (this.livemark.status == this.livemark.STATUS_READY) { | |
util.message(this.folder.title + ' invalidateContainer'); | |
this.livemark.unregisterForUpdates(aNode, this); | |
this.children = this.livemark.getNodesForContainer(this.folder); | |
this.reloadCallback(); | |
} | |
}catch(e){util.message(e)} | |
}, | |
reload: function(aCallback, aForce) { | |
this.reloadCallback = aCallback; | |
this.livemark.registerForUpdates({}, this); | |
this.livemark.reload(!!aForce); | |
}, | |
toCollection: function() { | |
return [ | |
FOLDER_ICON, | |
this.folder.title, | |
this.children.filter((i) => i.accessCount == 0).length, | |
]; | |
}, | |
toChildrenCollection: function() { | |
return this.children.map((i) => [ITEM_ICON, i.title, i]); | |
}, | |
}; | |
//http://moz-addon.g.hatena.ne.jp/teramako/20120531/1338472968 | |
function isLivemark(node) { | |
return (node instanceof Ci.nsINavHistoryContainerResultNode) && | |
PlacesUtils.annotations.itemHasAnnotation(node.itemId, PlacesUtils.LMANNO_FEEDURI); | |
} | |
function getLivemark(aNode) { | |
if (!isLivemark(aNode)) | |
throw TypeError("aNode is not LivemarkContainer"); | |
return PlacesUtils.livemarks | |
.getLivemark({ id: aNode.itemId }); | |
} | |
function getLiveBookmarkFolders(id){ | |
let parent = PlacesUtils.getFolderContents(id).root; | |
for (let i = 0; i < parent.childCount; i++) { | |
let child = parent.getChild(i); | |
if (isLivemark(child)) | |
liveBookmarkFolders.push(new LivemarkFolder(child)); | |
else if (PlacesUtils.nodeIsFolder(child)) | |
arguments.callee(child.itemId); | |
} | |
} | |
function getLiveBookmarks(){ | |
return new Promise((resolve) => { | |
var id = PlacesUtils.bookmarks.placesRoot; | |
getLiveBookmarkFolders(id); | |
setTimeout(function() { | |
if (liveBookmarkFolders.every((i) => i.completed)) | |
resolve(); | |
else | |
setTimeout(arguments.callee, 100); | |
}, 100); | |
}); | |
} | |
function selectFolder(initialIndex) { | |
prompt.selector({ | |
message : "select live bookmark :", | |
collection : liveBookmarkFolders.map((i) => i.toCollection()), | |
initialIndex : initialIndex, | |
header : ['Title', 'Unread'], | |
flags : [ICON | IGNORE, 0, 0], | |
keymap : pOptions['folders_keymap'], | |
stylist : function(args, n, current) { | |
let style = ""; | |
if (args[2] > 0) style += 'font-weight: bold;'; | |
return style; | |
}, | |
actions : [ | |
[showDetail, 'Show items', 'show-items'], | |
[(i) => openUILinkIn(liveBookmarkFolders[i].livemark.siteURI.spec, 'tab'), 'Open home', 'open-home'], | |
/* | |
[function(i, rows){ | |
display.echoStatusBar("Reloading ..."); | |
liveBookmarkFolders[i].reload(function(){ | |
liveBookmarkFolders = []; | |
getLiveBookmarks().then(selectFolder); | |
display.echoStatusBar(""); | |
}, true); | |
}, 'Reload', 'reload'], | |
*/ | |
] | |
}); | |
} | |
function showDetail(bookmarkIndex) { | |
var folder = liveBookmarkFolders[bookmarkIndex]; | |
prompt.selector({ | |
message : "pattern:", | |
collection : folder.toChildrenCollection(), | |
flags : [ICON | IGNORE, 0, ICON | IGNORE], | |
keymap : pOptions['items_keymap'], | |
stylist : function(args, n, current) { | |
return (args[2].accessCount == 0) ? | |
"font-weight: bold;" : ""; | |
}, | |
actions : [ | |
[(i) => openUILinkIn(folder.children[i].uri, 'tab'), 'Open', 'open,c'], | |
[() => selectFolder(bookmarkIndex), 'Select live bookmark folders', 'select-folder'] | |
] | |
}); | |
} | |
if (window.PlacesUtils) { | |
PlacesUtils.livemarks.reloadLivemarks(); | |
plugins.withProvides(function (provide) { | |
if (!("mozIAsyncLivemarks" in Components.interfaces)) { | |
display.echoStatusBar("not suported."); | |
} else { | |
provide('live-bookmark-select-folder', function(ev, arg){ | |
liveBookmarkFolders = []; | |
getLiveBookmarks().then(selectFolder); | |
}, M({en: 'Live bookmark - Select folder', ja: 'Live bookmark - フォルダを選択'})); | |
provide('live-bookmark-reload-all', function(ev, arg){ | |
PlacesUtils.livemarks.reloadLivemarks(); | |
}, M({en: 'Live bookmark - Reload all', ja: 'Live bookmark - 更新'})); | |
} | |
}, PLUGIN_INFO); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment