本文介绍了Firefox书签探索不会通过Javascript的第一级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如




  • Search_engines /




    • yahoo.com


  • google.com



  • 在这个例子中,我只能访问Search_engines和google.com而不是yahoo.com

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

    我的代码:

    $ p $ function browse_bookmark_node(bookmark_node,array)
    {
    //我们用这个函数探索书签
    //遍历这个文件夹的直接子元素
    for(var i = 0; i< bookmark_node.childCount; i ++){
    var node = bookmark_node.getChild(i);
    if(node.type == 0){
    //该节点是一个链接,所以我们将它添加到数组
    array.push(node.title);
    } else if(node.type == 6){
    //该节点是一个文件夹,所以我们来探索它
    browse_bookmark_node(node,array);



    $ b函数wrapper_browse_bookmark_node(bookmark_node){
    //我们使用这个函数来包装函数browse_bookmark_node并跟踪链接
    var array = [];
    browse_bookmark_node(bookmark_node,array);
    返回数组;
    }

    //以下所有代码用于访问firefox书签并且正常工作
    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;

    //探索书签的函数调用
    var links_array = wrapper_browse_bookmark_node(rootNode);

    //使用它后关闭容器!
    rootNode.containerOpen = false;


    解决方案一个明显的错误是使用 toolbarFolder 作为起点 - 这只是书签工具栏。如果你想要所有的书签(意味着书签菜单,书签工具栏和未分类的书签),你需要改变查询参数:

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

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

     } 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;
    }


    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;
    
    解决方案

    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;
    }
    

    这篇关于Firefox书签探索不会通过Javascript的第一级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 07:08