post message from window to background

465 views
Skip to first unread message

Tom Lee

unread,
Jul 28, 2013, 10:38:39 AM7/28/13
to chromium-...@chromium.org

Hi All,

 

I am having trouble receiving messages in my background page from a popup window I created.

 

Basically I create a popup up window to prompt the user to set up options. I create the window in the background script after the document is loaded. At the same time I add event listener to listen for messages.

 

// Background script

chrome.windows.create(

                 {url: "http://localhost:8081/mypage.php”, type: "popup", width: 600, height: 400}

                 );

 

    window.addEventListener("message", function(e) {

                alert('message received');

    })

 

In the window in the popup I have a script block where I am trying to post option information  back to the background. The problem is that I am not receiving the posted message

 

// popup

window.parent.postMessage({ test: true },'*');

 

 

Does anybody know what I am doing wrong? Any suggestions would be greatly appreciated.

 

Thanks,
Tom

 

PhistucK

unread,
Jul 28, 2013, 3:53:44 PM7/28/13
to Tom Lee, Chromium-extensions
When you create a page (a tab, a popup) using chrome.windows.create, it is not like window.open, which keeps the parent and child relationship (I guess you tried window.open and it did not work like you intended).
You have to inject a script into it using chrome.tabs.executeScript (for example) and call chrome.runtime.sendMessage using that script, since normal web pages do not have access to Chrome extension APIs.
(Note that there is a way to send messages from web pages to extensions, but I think it is experimental and not supported in stable Chrome yet, others may correct me)


PhistucK


 

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/41fafeb0-4b69-4c89-bd5f-c9f0594103b3%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages