Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 5 of 5

This is a discussion on Saving Bookmark Node Info To a Global Array within the Chrome Plugins section, part of the Google Chrome category: This has been solved with a hack (setting a timeout that waits for the callbacks to finish in the chrome). ...


  1. #1
    misfitplanet is offline Junior Member
    Join Date
    May 2011
    Posts
    3

    Default Saving Bookmark Node Info To a Global Array

    This has been solved with a hack (setting a timeout that waits for the callbacks to finish in the chrome). I didn't know this about JavaScript, that it will jump over code before it finishes other code when dealing with callbacks.. I only knew this to occur with threads in other languages. Maybe a thread is created in the Chrome? Please help me with a better answer if you know.

    I'm writing a bookmark manager extension. I've found a way to see the id, title and urls of bookmarks through console, but i need the info saved for further processing in a global array. I placed the global in the loop, but the call back function seems not ignore the global, erase it, or something. The data is not being stored in it. Is there a workaround for this? New to JavaScript.

    Code:
    function printBookmarks(bookmarks) {
      bookmarks.forEach(function(bookmark) {
        console.debug(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url);
        bookmarkEntries = bookmarkEntries.concat([bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url]);  // why are bookmarks not being added to global ??
        if (bookmark.children)
          printBookmarks(bookmark.children);
      });
    }
    
    function setBookmarkData()
    {
        bookmarkEntries = []
        chrome.bookmarks.getTree(function(bookmarks) {
        printBookmarks(bookmarks);
    });
        alert(bookmarkEntries) // comes up as empty array ???
    }
    If you'd like to take a peek at it I have the JavaScript running on a server now. http://75.72.25.11
    Last edited by misfitplanet; 05-06-2011 at 09:25 PM.

  2. #2
    devnull69 is offline Member
    Join Date
    Jul 2010
    Posts
    32

    Default

    Hm I don't trust .concat() here because sometimes it tends to create references instead of copies. And those references are lost after the callback finishes.
    Try this instead:
    Code:
    bookmarkEntries.push(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url);
    And initiate the array in the global scope rather than inside a function
    Code:
    var bookmarkEntries = [];
    function setBookmarkData() {
       chrome.bookmarks.getTree(function(bookmarks) {
          printBookmarks(bookmarks);
       });
       alert(bookmarkEntries) // comes up as empty array ???
    }
    Last edited by devnull69; 05-06-2011 at 10:25 AM.

  3. #3
    misfitplanet is offline Junior Member
    Join Date
    May 2011
    Posts
    3

    Default

    Made the changes, but still no data saved inside the global. This is really frustrating to see it there is the console, then pushing it to the global, yet the global comes up empty. Any other ideas?

    Code:
    var bookmarkEntries = []
    //also tried   bookmarkEntries without var, no avail
    
    function printBookmarks(bookmarks) {
      bookmarks.forEach(function(bookmark) {
        console.debug(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url);
        bookmarkEntries.push([bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url]);
        if (bookmark.children)
          printBookmarks(bookmark.children);
      });
    }
    
    function setBookmarks()
    {
        bookmarkEntries = []
        chrome.bookmarks.getTree(function(bookmarks) {
        printBookmarks(bookmarks);
    });
        alert(bookmarkEntries);
    }

  4. #4
    misfitplanet is offline Junior Member
    Join Date
    May 2011
    Posts
    3

    Default

    Here is minified extension code, code just to point out the problem. You could either place these to files in a folder and load load as an extension or download a ready-to-load example from my gmail docs.

    https://docs.google.com/leaf?id=0B6u...ZDkyM2Ji&hl=en

    Code:
    {
      "name": "Bookmarks Ext",
      "version": "0.1",
      "incognito": "split",
      "chrome_url_overrides": {
        "bookmarks": "index.html"
      },
      "permissions": [
      "bookmarks",
      "bookmarks.getChildren",
      "bookmarks.getTree",
      "bookmarks.id",
      "bookmarks.title",
      "bookmarks.url"
    ]
    }
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
        >
    <html lang="en">
    <head>
        <script type="text/javascript">
            
            function setBookmarkEntries()
            {
                chrome.bookmarks.getTree(function(bookmarks) {
                printBookmarks(bookmarks);});
            }
                
            function printBookmarks(bookmarks) {
                bookmarks.forEach(function(bookmark) {
                console.debug(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url);
                bookmarkEntries.push([bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url]);
                if (bookmark.children)
                    printBookmarks(bookmark.children);});
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            bookmarkEntries = []
            setBookmarkEntries()
            alert("Bookmark Entries: " + JSON.stringify(bookmarkEntries));
        </script>
    </body>
    </html>

  5. #5
    devnull69 is offline Member
    Join Date
    Jul 2010
    Posts
    32

    Default

    Quote Originally Posted by misfitplanet View Post
    [COLOR="red"]This has been solved with a hack (setting a timeout that waits for the callbacks to finish in the chrome). I didn't know this about JavaScript, that it will jump over code before it finishes other code when dealing with callbacks.
    Ok sorry, I did not see that in your post because of the unusual indentation you use.

    BUT: It is a general property of callbacks not only in Javascript. It is called "asynchronous execution" and yes, it will create threads in the background process. Most of the functions provided in the Chrome Extension System are being executed asynchronously. It is never good to use setTimeout to care for this though. Just put everything depending on the result of the callback INSIDE the callback.
    Code:
    function printBookmarks(bookmarks) {
       bookmarks.forEach(function(bookmark) {
          console.debug(bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url);
          bookmarkEntries = bookmarkEntries.concat([bookmark.id + ' - ' + bookmark.title + ' - ' + bookmark.url]);  // why are bookmarks not being added to global ??
          if (bookmark.children)       printBookmarks(bookmark.children);
       });
    }
    
    function setBookmarkData() {
       bookmarkEntries = [];
       chrome.bookmarks.getTree(function(bookmarks) {
          printBookmarks(bookmarks);
          alert(bookmarkEntries) // comes up as empty array ???
       });
    }
    Yes, it's as easy as that

Similar Threads

  1. Droid2 Global using Chrome
    By hattieruth2 in forum Chrome Tips & Tricks
    Replies: 0
    Last Post: 02-15-2011, 09:28 PM
  2. need help to return cross domain info
    By Alfred in forum Plugins Development
    Replies: 1
    Last Post: 04-23-2010, 07:54 PM
  3. Storing global values / creating a "Settings" system
    By DavidTurnbull in forum Plugins Development
    Replies: 5
    Last Post: 02-13-2010, 03:47 AM
  4. Access to search engines info
    By mindphaser in forum Plugins Development
    Replies: 4
    Last Post: 12-08-2009, 12:04 AM
  5. Chrome 2.0.177.1 Info
    By Stamga in forum Chrome Talk
    Replies: 3
    Last Post: 05-01-2009, 09:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •