views:

261

answers:

2

I am trying to utilize the Google Bookmarks API for Chrome Extensions (this is not the Google Bookmarks API as in http://www.google.com/bookmarks/, but rather Chrome Bookmarks API).

Anyway, I just tried a simple example which I have the background.html and the manifest.json listed below. However, I am not getting the alert dialog box. I am not getting this problem if I switch to using tab events. What am I doing wrong?

manifest.json

{
    "name": "Google Bookmark Integration",
    "version": "1.0",
    "description": "Integrates Chrome bookmarks with Google Bookmarks.",
    "icons": { "128": "images/bookmark.ico" },
    "background_page": "background.html",
    "permissions": [
     "bookmarks",
     "http://*.google.com/bookmarks/*"
    ]
}

background.html

<script>
    chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
       console.log("Bookmark Created"); 
    });
</script>
+1  A: 

JavaScript is dynamically typed and function definitions shouldn't have type names. Instead of (string id, BookmarkTreeNode bookmark), you need to write just (id, bookmark). Your background.html should be:

<script>
    chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
       alert("Dialog Box");     
    });
</script>

In addition, apparently Chrome has limited support for alert() inside extensions. (It worked for me in this particular case, but I've now found other cases where it doesn't.) Try console.log() instead. The Chrome Extensions documentation page on "debugging" has instructions for how to open the Developer Tools / JavaScript console for the background.html page, which you'll need to do.

dmazzoni
Thank you for your response. Unfortunately, this does not fix the problem. In addition, if I use similar code, but for tabs (including type names), it works fine.
JasCav
Check again. I just tried it on the latest dev channel Chrome and it works great. Any chance you have a typo somewhere else? Did you check the inspector for javascript errors?
dmazzoni
@dmazzoni - I have exactly what you have, plus the manifest file, and I'm not getting an alert box when I create a new bookmark. How are you creating the bookmark? I'm trying to do so by clicking on the star next to the address bar.
JasCav
You're right, there is another problem - alert() is not supported in extensions. See the changes to my response, above.
dmazzoni
@dmazzoni - I really must be doing something screwy. I am debugging (per the instructions from the Chrome site), and when I add a bookmark, it's like the extension doesn't even see it. I'm getting no response from the listener whatsoever. I do appreciate your help - +1 for that. If this leads me to the answer, I'll accept...but, I'm still struggling with something that seems so very simple.
JasCav
A: 

To answer my own question, the problem here was that, not only do the permissions need to be set to "bookmarks" but they also need to be set to "tabs". Once I did this, the plug-in then recognized adding and removing bookmarks.

Very counterintuitive, but this is the solution.

JasCav
Interesting. I can't reproduce, but I may be using a more recent build. Maybe file a bug at http://crbug.com - be sure to mention your exact version of Chrome and what platform (Windows, Linux, etc.).
dmazzoni