Message passing from a content script to an external script or another content script.

1,181 views
Skip to first unread message

Jared

unread,
May 6, 2016, 9:33:38 AM5/6/16
to Chromium-Extensions-Announce

I am working on a Chrome extension where I need to get a value from one part of the extension to the other. I have a background script the executes both scripts when the extension icon is clicked. One content script gets a value from the web page and needs to send it to external script. When the extension icon is clicked it executes the external script which listens for a message.

function message(){                                         
    console.log("Listening...");                        
    var token = "";                                     

    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
            console.log("Attempting to get message...");
            console.log(sender.tab ?                    
            "from a content script:" + sender.tab.url : 
            "from the extension");                      
    if (typeof request != 'undefined'){                 
            console.log("Printing token...");           
            token = request.token;                      

            console.log(token);                         

            received = true;                            
            sendResponse({greeting: "Got Token"});      
    }                                                   
    });                                                 
}                                                           
message();   

In the content script, when a button on the web page is clicked it should send a message to the external script. Here is the code in the content script.

chrome.runtime.sendMessage({token: token, URL: URL},function(response)   {
    console.log("Greeting 1");
    console.log(response.greeting);                                                                                                                                                                            
    });    

When I run the program I get the "Listening" printed out from the external script and I get the "Greeting 1" printed out from the content script. However, in the external script the listener doesn't seem to be working. I can breakpoint on it and step through the process but it just ends up skipping the block inside the listener and just ends at the end of the function doing nothing. Can anyone see what I might be doing wrong?

PhistucK

unread,
May 6, 2016, 1:15:20 PM5/6/16
to Jared, Chromium-Extensions-Announce
Do you see any exception?
"External script" sounds like a script you (or the website) load from a server, in regular webpage. If this is correct, then I would expect an exception there, since chrome.runtime.onMessage does not seem to be exposed in non-extension context/world.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" 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 https://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/f884b572-dea4-4e08-b8c9-1a1b33a05cd3%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Jared

unread,
May 6, 2016, 1:38:17 PM5/6/16
to Chromium-Extensions-Announce, jmcre...@peachtreebilling.com
The other script in its current configuration is another content script. 
For a little more background on what I am trying to do:
I am using a content script to extract a token out of the web page's local storage. I need the token to make calls to an API. I can get the token but when I try to use it in the content script I get an 401 unauthorized error for invalid token. So then I used message sending to the background page to get the token value there. But when I did that I still got the 401 unauthorized token invalid token error. However, when I use the token hard coded in a script that is not the background or content script it works perfectly. I cannot figure out why it works in one place but not the other. That is why I was trying to send the value of the token to the other script. 
I know it seems like an odd problem and I am new to Chrome extensions do may be doing some things backwards. Thanks for any help you can give me.  


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Jared

unread,
May 6, 2016, 3:45:20 PM5/6/16
to Chromium-Extensions-Announce, jmcre...@peachtreebilling.com
Thank everyone for the help but I found a solution to the problem without having to solve the messaging problem. Thanks again.
Reply all
Reply to author
Forward
0 new messages