开发者

Prevent event listener from been execute in Google Chrome Extension

开发者 https://www.devze.com 2023-04-12 04:34 出处:网络
I want to build a Google Chrome extension to sync all my bookmarks across Chrome so I have attached an event listener to onCreated;

I want to build a Google Chrome extension to sync all my bookmarks across Chrome so I have attached an event listener to onCreated;

chrome.bookmarks.onCreated.addListener(function (id, bookmark) {
    console.log('Hey bookmark created ' + id);
});

The idea is that when the user creates a new bookmark this onCreated event listener sends a JSON object to a server via an AJAX request stating that a bookmark has been created.

If the server then returns a JSON object which indicates开发者_如何转开发 that the new bookmark should be created on the browser I don't need the onCreated event listener to be executed.

Basically, the idea is that the event listener should only be triggered if a bookmark has been manually created by the user and not programmatically.

For example, the event listener should not be executed when if a bookmark is created using the following command;

chrome.bookmarks.create({
   parentId: '648',
   title: 'Google Folder'
});


If I understood correctly, you want to be able to pause chrome.bookmarks.onCreated while you are creating bookmarks programmatically. There is no clean way of doing it afaik, as Chrome API doesn't provide a way of removing event listeners, only adding.

I would try to make some global switch probably:

var listenerEnabled = true;
chrome.bookmarks.onCreated.addListener(function (id, bookmark) {
    if(listenerEnabled) {
        console.log('Hey bookmark created ' + id);
    }
});

function createBookmark() {
    listenerEnabled = false;
    chrome.bookmarks.create({parentId: '648',title: 'Google Folder'}, function() {
        //enable with delay in case listener fires late
        setTimeout(function() {
            listenerEnabled = true;
        }, 500);
    });
}

Another way would be marking bookmarks created programattically (by appending something to title for example), and then inside the listener you can detect them and remove this mark.


I'm not sure why you would want to prevent the user from manually adding a bookmark.

If you are wanting to keep the browser's bookmarks in sync with the server then you should only really need to ensure the following;

  1. All bookmarks on the server exist in the browser
  2. Any modifications to bookmarks on the server should be reflected in the browser (e.g. removal, hierarchy)
  3. Any bookmarks added to the browser are added to the server

Chrome already prevents users from creating a bookmark twice so you shouldn't need to perform this check but only notify your server which should add it to the database if it doesn't already exist there.

Bookmark syncing is relatively easy but you need to get your use cases and logic down on paper before you try and code this or it won't succeed.

Points 1 & 2 can be managed by periodically checking the server for updates. Point 3 you've already covered but the server needs to do the logic instead of the extension.


Although the above solution will work, Boris Smus at google groups found another way to do the same thing:

var myListener = function(...) {...};
chrome.bookmarks.onCreated.addListener(myListener);
// Remove temporarily
chrome.bookmarks.onCreated.removeListener(myListener);
// Add again...

This removes the listener when you need to programmatically create a bookmark/folder, and re-adds it when you are ready.

I just tested and used this solution in my code. Many thanks to Boris Smus, at

https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-extensions/fKYpINyqs1k

It would be preferable to able to do a programmatic create that does not trigger an event, or rather be able to do attach events only to user-specific actions. However, the solutions here will do for now.

0

精彩评论

暂无评论...
验证码 换一张
取 消