When is chrome.bookmarks.onChildrenReordered fired? - google-chrome-extension

In Chrome extensions, it seems that whenever a bookmark is moved (to the same folder or not), the chrome.bookmarks.onMoved event will get fired, but not chrome.bookmarks.onChildrenReordered. So when would the latter ever get fired?

Related

Is it possible for an extension to know what event listeners are registered by another extension?

I think the title says it all. Suppose I have an extension X which has event listeners for some event(s). Is it possible for another extension, say Y, to get the list of all those event listeners ?
you can get the list of event listeners on a specific element, as explained by this post:
How to find event listeners on a DOM node?
But Chrome extensions are made to be invisible, the page cannot detect that an extension inserted a content script inside of it. Extensions are basically closed off, and no other page, or extension, can catch a glimpse of what goes on within another extension's content scripts. So You can try, but i severely doubt it to be possible.

Intercept (and perhaps cancel) page's mouse/keyboard event handlers

I'm trying to create a Chrome Extension that deals with selected text. Some website pages' otherwise selectable text content has click/mouse-up/down event handlers that navigate to a new page.
Is there a way from the background or content script to temporarily disable (and restore) the page's arbitrary event handlers without interfering with the native text selection?
Worst case I'm thinking of is to detach, clone the body html, allow the selection, and then restore the bound original. Seems like trouble.
Thanks!
Most HTML DOM events follow the capture-target-bubble event model. This means, for example, that if you click on a button, that the "click" event is first dispatched at the root, all the way down to the button, then back up. Event propagation can be stopped, which prevents the event listener at the next level from being notified of the event.
The earliest possibility of receiving the event is at the root, often window at the capture phase. To bind an event listener to the capture phase, use addEventListener with the third parameter set to true:
// in a content script, at run_at:document_start
window.addEventListener('click', function(event) {
event.stopImmediatePropagation();
}, true);
Many web pages use jQuery to manage DOM events, which binds the event listeners at the bubbling phase, so the previous method will work on most sites. If the page does not use jQuery, then you have to bind your event listener at document_start to make sure that your event listener is triggered before every other event listener.

Annoying delay before i can click a button after open an XPage

when I open an XPage with a lot of ssjs in it. I have always the problem, that when I click a button (with ssjs in it) directly after opening the page, nothing happens. When I wait 1 or 2 seconds, every thing works as expected. It seems, that not everything is loaded fast enough.
Is there an event to see if the document is completly loaded? I tried the jquery and dojo onready events and as well the onClientLoad event. But all these events trigert directly after the page is open (but not finish loaded).
You can add the following onClientLoad event as a client side javascript:
XSP.addOnLoad(new function() {
// this will run when everything is ready...
});
That's also why you see a lag for button events. All event handlers are binded via the same mechanism as above.
It was my fault. I just recognized that there are more than 30 panels there all have an empty onClientLoad method in it. After I removed the events, my page need to load 500ms insted of 2,5s and also the XSP.addOnLoad event to see if the page is ready now works as expected.
Thanks #Serdar Basegmez for your help.

Wait for chrome.tabs.update tab to finish loading

I'm trying to work on a chrome extension and am trying to clean up some of my code by relying on the sendMessage. However the callback function activates before the page has finished loading so in the case of a new tab, nobody receives and in the case of an existing tab the page that is being moved from is getting the message (but that isn't what I want). I've looked for other people asking about that problem with new tabs and there wasn't a clear answer, the best suggestion I've seen is to create a global variable and create a listener for tab loads and compare it against this global variable.
So the question is, is there a way to wait in the callback function until the page has loaded, or do I create an array of JS objects that contain the tab I'm waiting on and the information I want to send to that tab.
For reference here is the relevant code in the background javascript file.
chrome.tabs.sendMessage(tab.id, {info: "info"}, function(response)
{
//This line isn't used when I am navigating without changing tabs
chrome.tabs.create({url: response.info.linkUrl}, function(tab1)
{
chrome.tabs.update(tab1.id, {url: response.info.linkUrl}, function(tab2)
{
chrome.tabs.sendMessage(tab2.id, {info: "More Info"});
});
});
});
Otherwise I am able to confirm that all of my tab side code works, once my sendMessage was delayed enough for me to see that with my own eyes. My code is able to consistently make it past validation on the page being navigated away from, confirmed by checking document.url.
You can try injecting a second content script instead of a message.
It will execute in the same context as your other script.
Something along the lines of
chrome.tabs.executeScript(tab2.id,
{code: 'showInfo("More Info);', runAt: 'document_idle'}
);
where showInfo does the same as your message handler.
It's a bit of a hack and I'm not 100% sure the load order will be correct.
Other possible solutions are more complex.
For example, you can make the content script report back that it is ready and have a handler for that, for instance you can register a listener for onMessage in the background that waits for a message from that specific tab.id, sends "More Info" and then deregisters or disables itself.
Or, you could potentially switch to programmatic injection of your content script, which would let you control load order.

YUI browser history manager back button

In am using YUI browser history manager for keeping track of Ajax navigations. I am registering history object like:
YAHOO.util.History.register("state",init, onStateChange);
Here onStateChange is getting called when I do
YAHOO.util.History.navigate("state",urlhash);
and when I press back button.
Is there any way to know if onStateChange is called on back button or by calling navigate?
If this event can be called by an external system sometimes (in this case the browser back button) and by your own code sometimes, you can differentiate the caller by making it a requirement to do something special when your own code calls the method. Wrapping the call to navigate() in your own helper method can help make sure you stay consitent with this.
In the YUI docs I see that register() can take an optional 4th parameter (among others) which is an arbitrary object that will get passed into your onStateChange handler function. One of the properties of this object could be a flag indicating that it was called from your code versus initiated by the browser, and in your navigate() wrapper always set that flag. Remember to set it back in your handler.
Note: I am not as familiar with YUI as ExtJS, maybe some guru that knows the API better can help, but this is a general strategy that can work. This answer makes an inelegant assumption that a call to navigate() will make that handler fire reliably before any other navigation has a chance to occur, which is probably a safe bet in the single-threaded javascript world.

Resources