如何调用Firefox书签对话框?

3
2个回答

4

这篇文章已经过时,适用于Firefox 4。这个功能现在已经被实现在PlacesUIUtils模块中,方法是showBookmarkDialog()。你可以像这样调用它:

Components.utils.import("resource://gre/modules/Services.jsm");
var uri = Services.io.newURI("http://example.com/", null, null);

Components.utils.import("resource:///modules/PlacesUIUtils.jsm");
PlacesUIUtils.showBookmarkDialog({
  action: "add",
  type: "bookmark",
  uri: uri,
  title: "Example bookmark"
}, window);

这是一个内部模块,因此并没有真正的文档,API 可能会在未来再次更改。您可以看到它如何被使用的示例 在源代码中。顺便说一句,如果您实际想要打开的是书签列表而不是“添加书签”对话框,则可以这样做:
Components.utils.import("resource://gre/modules/Services.jsm");

var organizer = Services.wm.getMostRecentWindow("Places:Organizer");
if (!organizer)
{
  // No currently open places window, so open one with the specified mode.
  openDialog("chrome://browser/content/places/places.xul", 
             "", "chrome,toolbar=yes,dialog=no,resizable", "AllBookmarks");
}
else
{
  organizer.PlacesOrganizer.selectLeftPaneQuery("AllBookmarks");
  organizer.focus();
}

(这段代码大部分来自于PlacesCommandHook实现)


@R-U-Bn:这个问题与Add-on SDK无关,因此代码不使用任何SDK模块。我不得不回滚您的更改。 - Wladimir Palant
我知道了。抱歉,那我猜我会把它放在另一个答案中,因为问题本身并没有说不应该是SDK,对吧?(只是为了其他通过谷歌碰巧到这里的人) - e-motiv
当然你可以自由选择,但我认为这个问题明显是指经典插件。 - Wladimir Palant

0

针对SDK开发人员(谷歌碰撞):

const utils     = require('sdk/window/utils');
const window    = utils.getMostRecentBrowserWindow();

let { Cu } = require('chrome');
Cu.import("resource:///modules/PlacesUIUtils.jsm");

Cu.import("resource://gre/modules/Services.jsm");    

//Adding bookmark    
var uri = Services.io.newURI("http://example.com/", null, null);
PlacesUIUtils.showBookmarkDialog({
    action: "add",
    type:   "bookmark",
    title:  "Predefined title",
    uri:    uri    
}, window);

//Editing existing one
PlacesUIUtils.showBookmarkDialog({ 
    action: "edit",
    type:   "bookmark",
    itemId: 575
}, window);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接