Cannot send a response more than once per chrome.extension.onRequest listener per document

1,064 views
Skip to first unread message

Alon Bar-Lev

unread,
May 14, 2012, 7:58:53 AM5/14/12
to chromium-...@chromium.org
Hello,

I am getting the above message, don't know exactly why, and how to
overcome this. I could not find any relevant reference.

The scenario is as follows: extension1 needs to acquire information
from a content script of extension2.
Because extension1 cannot directly call the content of extension2 the
path is extension1::background->extension2::background->extension2::content.

The funny thing is that it seems to be working... but I don't like
getting errors.

I appreciate any help!
Alon Bar-Lev.

So what I am doing is the following:

extension2::content
---
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
var p = request.parameters;
handle(p);
sendResponse({parameters: p});
}
);
---

extension2::background
---
chrome.extension.onRequestExternal.addListener(
function(request, sender, sendResponse) {
chrome.tabs.sendRequest(
request.tabId,
request,
sendResponse
);
}
);
---

extension1::background
---
(function(details) {
chrome.extension.sendRequest(
extId,
{
tabId: details.tabId,
frameId: details.frameId,
parameters: details.parameters
},
function(response) {
handle(response);
}
);
})(details);
---

Matt Perry

unread,
May 15, 2012, 2:28:40 PM5/15/12
to Alon Bar-Lev, chromium-...@chromium.org
Where do the errors show up? In the page containing extension2::content, I assume? The error happens when you try to call sendResponse multiple times for the same message. This can happen if you have multiple onRequest listeners in a single document (e.g. if you have 2 content scripts injected into the same document, and both have an onRequest listener). The problem is that only one of their responses is actually sent.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Devin

unread,
May 15, 2012, 3:41:50 PM5/15/12
to chromium-...@chromium.org
onRequest/sendResponse is ONLY intended for one time messages. For long lived connections you need to use ports with connect/onConnec described here.

Alon Bar-Lev

unread,
May 15, 2012, 2:41:59 PM5/15/12
to Matt Perry, chromium-...@chromium.org
Hello Matt,

Thank you for your response.
Actually, I see this at the extension2::background page.
So I cannot have two listeners at background...
Strange limitation... I thought the concept of "addListener()" is to
allow publish subscriber model.

Thank you so much!
Alon.

Alon Bar-Lev

unread,
May 15, 2012, 3:48:34 PM5/15/12
to Devin, chromium-...@chromium.org
Hello Devin,

I had a problem with ports, as there it is difficult to correlate
between request and response. It made the code much more complicated.
So I preferred the sendRequese approach.

Thank you,
Alon.

> --
> You received this message because you are subscribed to the Google Groups
> "Chromium-extensions" group.

> To view this discussion on the web visit
> https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/-/nCaaDAVDZmYJ.

Abraham Williams

unread,
May 17, 2012, 6:18:23 PM5/17/12
to Alon Bar-Lev, Matt Perry, chromium-...@chromium.org
You can have as many listeners as you but but the API design is request:response. Once the request is satisfied with a response there is no longer an open request waiting for a response.

Abraham
--
Abraham Williams | abrah.am | abraham+
@abraham | github.com/abraham | blog.abrah.am
This email is: [ ] shareable [x] ask first [ ] private.



On Tue, May 15, 2012 at 11:41 AM, Alon Bar-Lev <alon....@gmail.com> wrote:
background

Reply all
Reply to author
Forward
0 new messages