Firefox 书签探索未超过 Javascript 的第一级

发布于 2024-11-14 05:58:51 字数 2006 浏览 1 评论 0原文

我已经编写了一些代码来探索我的 Firefox 书签,但我只获得了第一级书签(即我没有获得文件夹中的链接)。

例如

  • 搜索引擎/

    • yahoo.com
  • google.com

在此示例中,我只能访问 Search_engines 和 google.com 而不是 yahoo.com

我的函数是递归的我不知道为什么会这样发生。

我的代码:

function browse_bookmark_node(bookmark_node, array)
{
    // We explore the bookmarks with this function     
   // iterate over the immediate children of this folder
   for (var i = 0; i < bookmark_node.childCount; i ++) {
       var node = bookmark_node.getChild(i);
       if (node.type ==0) {
          // the node is a link so we add it to the array
          array.push(node.title); 
       } else if (node.type ==6) {
          // the node is a folder so we explore it
          browse_bookmark_node(node, array);
       }
   }
}

function wrapper_browse_bookmark_node(bookmark_node) {
   // We use this function to wrapp the function browse_bookmark_node and keep track of the links
   var array = [];
   browse_bookmark_node(bookmark_node, array);
   return array;
}

// All the code following is used to access firefox bookmarks and works fine
var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();

var bookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                                 .getService(Components.interfaces.nsINavBookmarksService);
var bookmarksMenuFolder = bookmarksService.bookmarksMenuFolder;

query.setFolders([bookmarksMenuFolder], 1);

var result = historyService.executeQuery(query, options);
var rootNode = result.root;
rootNode.containerOpen = true;

// The function call to explore the bookmarks
var links_array = wrapper_browse_bookmark_node(rootNode);

// close a container after using it!
rootNode.containerOpen = false;

I've written some code to explore my Firefox bookmarks but I only get the first level of bookmarks (i.e. I don't get the links in the folders).

e.g.

  • Search_engines /

    • yahoo.com
  • google.com

In this example I have only access to Search_engines and google.com not yahoo.com

My function being recursive I don't know why this happens.

my code :

function browse_bookmark_node(bookmark_node, array)
{
    // We explore the bookmarks with this function     
   // iterate over the immediate children of this folder
   for (var i = 0; i < bookmark_node.childCount; i ++) {
       var node = bookmark_node.getChild(i);
       if (node.type ==0) {
          // the node is a link so we add it to the array
          array.push(node.title); 
       } else if (node.type ==6) {
          // the node is a folder so we explore it
          browse_bookmark_node(node, array);
       }
   }
}

function wrapper_browse_bookmark_node(bookmark_node) {
   // We use this function to wrapp the function browse_bookmark_node and keep track of the links
   var array = [];
   browse_bookmark_node(bookmark_node, array);
   return array;
}

// All the code following is used to access firefox bookmarks and works fine
var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();

var bookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                                 .getService(Components.interfaces.nsINavBookmarksService);
var bookmarksMenuFolder = bookmarksService.bookmarksMenuFolder;

query.setFolders([bookmarksMenuFolder], 1);

var result = historyService.executeQuery(query, options);
var rootNode = result.root;
rootNode.containerOpen = true;

// The function call to explore the bookmarks
var links_array = wrapper_browse_bookmark_node(rootNode);

// close a container after using it!
rootNode.containerOpen = false;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-11-21 05:58:51

一个明显的错误是使用 toolbarFolder 作为起点 - 这只是书签工具栏。如果您想要所有书签(即书签菜单、书签工具栏和未排序的书签),您需要更改查询参数:

query.setFolders([
    bookmarksService.bookmarksMenuFolder,
    bookmarksService.toolbarFolder,
    bookmarksService.unfiledBookmarksFolder
], 3);

另一个问题是在 nsINavHistoryResultNode 对象上获取 childCount 属性 - 那里没有这样的属性。在访问 nsINavHistoryContainerResultNode 的属性之前,您需要显式或隐式地(通过instanceof)调用QueryInterface。所以我会写:

} else if (node.type == 6 && node instanceof Components.interfaces.nsINavHistoryContainerResultNode) {
    var oldOpen = node.containerOpen;
    node.containerOpen = true;
    browse_bookmark_node(node, array);
    node.containerOpen = oldOpen;
}

One obvious mistake is using toolbarFolder as a starting point - that's only the bookmarks toolbar. If you want all bookmarks (meaning bookmarks menu, bookmarks toolbar and unsorted bookmarks) you need to change query parameters:

query.setFolders([
    bookmarksService.bookmarksMenuFolder,
    bookmarksService.toolbarFolder,
    bookmarksService.unfiledBookmarksFolder
], 3);

The other issue is getting childCount property on a nsINavHistoryResultNode object - there is no such property. Before you can access properties of nsINavHistoryContainerResultNode you need to call QueryInterface, either explicitly or implicitly (via instanceof). So I would write:

} else if (node.type == 6 && node instanceof Components.interfaces.nsINavHistoryContainerResultNode) {
    var oldOpen = node.containerOpen;
    node.containerOpen = true;
    browse_bookmark_node(node, array);
    node.containerOpen = oldOpen;
}
羁〃客ぐ 2024-11-21 05:58:51

只是为了节省想要获取所有书签的人的时间,不要这样做
query.setFolders([
bookmarksService.bookmarksMenuFolder,
bookmarksService.toolbar文件夹,
bookmarksService.unfiledBookmarksFolder
], 3);

正如 Wladimir 所建议的,因为它不会为您带来所有书签,而是对文件夹进行相交,只为您提供放置在所有这些文件夹中的书签,如 Mozilla 文档中所写

您可以设置多个文件夹,结果将是所有文件夹的交集。

https://developer.mozilla.org/en/Querying_Places

Just to save time to anyone who want to get all the bookmarks, do not do
query.setFolders([
bookmarksService.bookmarksMenuFolder,
bookmarksService.toolbarFolder,
bookmarksService.unfiledBookmarksFolder
], 3);

as suggested by Wladimir since it WON'T bring you all of the bookmarks but instead do an intersect of the folders and only give u the bookmarks which are placed at all of those folders, as written at Mozilla doc's

You can set multiple folders and the result will be the intersection of all the folders.

( https://developer.mozilla.org/en/Querying_Places )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文