/*** I've asked this question on StackOveflow, but the only answer
I've gotten so far isn't very helpful. I am re-posting here, as it
seems like this board is still pretty active ***/
My Chrome extension injects a content script into every page
("matches": ["http:///", "https:///"]). The content script will fire a
message (chrome.extension.sendRequest) to the background page, and the
background page registers an event handler:
chrome.extension.onRequest.addListener( function (request, sender,
callback) {…} );
I have noticed that those events are fired as I start typing in the
omnibox – even before I hit Enter to load the destination page.
Scenario: - Launch Chrome - Start typing in the omnibox. In fact, as
soon as the omnibox has focus, my
chrome.extension.onRequest.addListener() event handler
[background.html] is fired.
The weird thing is that the sender.tab.index value is -1, so it
doesn’t map to a “real” tab. Also, as I continue switching focus away
from, and back to the omnibox, the events keep firing. Each time,
sender.tab.index is -1, but
sender.tab.id is being incremented by 2
(43, 45, 47, …). This behavior is easily reproducible.
Then, when I actually choose a URL (say,
xyz.com) by hitting Enter in
the omnibox, and the page loads in the browser/tab, I get yet another
event. This time, however, sender.tab.index is non-negative – it
represents the actual browser tab, and my content script handler can
do its work.
Is this behavior – content script being injected into a “phantom” page
for omnibox actions – known, and is it documented anywhere? And what
is the proper way for my event handler to handle this? Let’s say my
content script handler needs to manipulate the web page. Should I be
checking sender.tab.index, and only doing the work if
sender.tab.index != -1?
Thanks!