Get URL of tab where user pressed a key (from Hotkey callback)

27 views
Skip to first unread message

hotsopdotcom

unread,
May 16, 2012, 5:03:28 PM5/16/12
to mozilla-labs-jetpack
When the user presses a key in a tab, I want to get the URL of that
tab.

This works in my addon's main.js:

var showHotKey = Hotkey({
combo: "accel-shift-1",
onPress: function() {
console.log( "url of active tab is " + tabs.activeTab.url );
}
});

However, I wonder if there are cases where that could get the wrong
URL.

I tried putting that in a content script, but content scripts
apparently can't do a require("hotkeys").

Is there a better way to do this?

Hernan Rodriguez Colmeiro

unread,
May 17, 2012, 9:02:30 AM5/17/12
to mozilla-la...@googlegroups.com
contentScripts can't use require, so you should send a message to the
main script and add the listener to the keypress there. Here you can
see some examples of message passing between contentScripts and the
main script:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/content-scripts/using-postmessage.html

Hernán

hotsopdotcom

unread,
May 17, 2012, 3:07:08 PM5/17/12
to mozilla-labs-jetpack
The problem is that I can't use the AddOn SDK's Hotkey in the content
script. So, I can't use that to find out when the user pressed a
hotkey in the tab associated with that content script (unless I did
that using plain old Javascript rather than the SDK's library).

It looks to me like the hotkey library needs a contentScript setup of
some kind, or some way of finding out which tab triggered the hotkey.
But, I'm only learning as much about the SDK to write my plugin so
maybe there's something I'm missing.

Hernan Rodriguez Colmeiro

unread,
May 17, 2012, 8:12:08 PM5/17/12
to mozilla-la...@googlegroups.com
On Thu, May 17, 2012 at 4:07 PM, hotsopdotcom <hotsop...@yahoo.com> wrote:
> The problem is that I can't use the AddOn SDK's Hotkey in the content
> script. So, I can't use that to find out when the user pressed a
> hotkey in the tab associated with that content script (unless I did
> that using plain old Javascript rather than the SDK's library).
>

But you can create a listener on main.js using the SDK Hotkey API and
send a message to the content script when the user presses the key,
right? Maybe your case is more complicated, but I think that should
work.

Hernán

hotsopdotcom

unread,
May 20, 2012, 4:57:08 PM5/20/12
to mozilla-labs-jetpack
The problem is that I'll have multiple instances of the content
script, AFAIK.

My content script is loaded with this:

pageMod.PageMod({
include: "*",
contentScriptFile: data.url("myfile.js"),
onAttach: function( worker ) {}
...

AFAIK, that will generate multiple instances of the content script,
one for every tab.

OTOH, Hotkey is a global, in the sense that it isn't linked to a
specific content script or tab. Thus, when the Hotkey callback is
called, I won't know which of the possibly-multiple "worker" objects
should be called.

I might be wrong about that since I just want to know enough to write
my extension, but if I'm right then Hotkey would seem to need some way
to tell which tab the key was pressed in.

BTW: my extension uses the method in my first message, and in the
meantime it got experimentally approved. So, doing it that way might
be the approved way.

Paul Morris

unread,
May 20, 2012, 6:02:11 PM5/20/12
to mozilla-la...@googlegroups.com
Hello, To go back to your initial message:

hotsopdotcom wrote:
> When the user presses a key in a tab, I want to get the URL of that tab.
>
> This works in my addon's main.js:
>
> var showHotKey = Hotkey({
> combo: "accel-shift-1",
> onPress: function() {
> console.log( "url of active tab is " + tabs.activeTab.url );
> }
> });
>
> However, I wonder if there are cases where that could get the wrong URL.

AFAIK, I can't think of any way that a tab that wasn't the active tab
could be the source of a key press. That is, I think a key press will
always be coming from the active tab. So I think your code above should
work fine for getting the URL you want.

-Paul


Reply all
Reply to author
Forward
0 new messages