Results 1 to 8 of 8

This is a discussion on Communication within the Plugins Development section, part of the Chrome Plugins category: One things I find myself having to reference a lot is communication between different scripts. And through enough testing and ...


  1. #1
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default Communication

    One things I find myself having to reference a lot is communication between different scripts. And through enough testing and stuff I've figured out how to communicate between everything. So here is a list of code snippets of how to do so. In all of these examples, I will be setting the message variable in the receiver to "hello". Each sample will have two parts -> and <-, for most of these, the two must be used together!

    Script -> Created window:
    Code:
    var mywindow = window.open("url","_blank");
    function sayHello(){
    	mywindow.message = "hello";
    }
    Script <- Created window:
    Code:
    function sayHello(){
    	window.opener.message = "hello";
    }
    Content script -> Toolstrip/Background Page:
    Code:
    myport = chrome.extension.connect();
    myport.onMessage.addListener(recv);
    myport.postMessage("hello");
    
    function recv(msg){
    	message = msg;
    }
    Content script <- Toolstrip/Background Page:
    Code:
    var myport;
    chrome.self.onConnect.addListener(listen);
    function listen(port){
    	myport = port;
    	myport.onMessage.addListener(recv);
    	myport.postMessage("hello");
    }
    
    function recv(msg){
    	message = msg;
    }
    Toolstrip -> Background Page:
    Code:
    var is_toolstrip = true;
    var v = chrome.self.getViews();
    for(var i in v){
    	if(v[i].is_background_page){
    		v[i].message = "hello";
    	}
    }
    Toolstrip <- Background Page:
    Code:
    var is_background_page = true;
    var v = chrome.self.getViews();
    for(var i in v){
    	if(v[i].is_toolstrip){
    		v[i].message = "hello";
    	}
    }
    Content script -> Matched page:
    Code:
    contentWindow.message = "hello";
    function sayHello(){
    	message = "hello";
    }
    contentWindow.sayHello = sayHello;
    Content script <- Matched page:
    Code:
    sayHello();
    //This function was injected by the content script,
    //and by the method of injection, is run in the content
    //script's scope rather than the page's.
    Extension's content script <-> Different extension's content script:
    Code:
    var myinterval,myid=chrome.extension.id_;
    var comm = contentWindow.document.getElementById("INTEREXTENSIONCOMMUNICATOR");
    if(typeof(comm) == "undefined"){
    	comm = document.createElement("span");
    	comm.setAttribute("display","none");
    	comm.setAttribute("id","INTEREXTENSIONCOMMUNICATOR");
    	contentWindow.document.body.appendChild(comm);
    }e
    myinterval = setInterval(function(){
    	if(comm.value.substr(0,myid.length+1) != myid+":"){
    		var pos = comm.value.indexOf(":");
    		var extid = comm.value.substr(0,pos);
    		console.log("Recieved message from extension "+extid);
    		message = comm.value.substr(pos+1);
    		comm.value = "";
    	}
    },100);
    
    function send(msg){
    	comm.value = myid+":"+msg;
    }
    
    send("hello");
    I think that's everything! You can of course link these together to communicate between further away things (like a different extensions toolstrip from your background page, or a background page's opened window from your content script).
    If there's anything I forgot please mention it and I'll post the examples, but I don't think there is.
    It'll be helpful for me even to not have to sift through my old code to remember how to do all this.. ^^;
    Last edited by Waha; 07-18-2009 at 02:46 AM.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  2. #2
    Kyrax is offline Senior Member
    Join Date
    Apr 2009
    Location
    Qc, Canada
    Posts
    495

    Default

    Wow Waha !
    Thanks

    This is incredibly useful. Didn't know about a lot of stuff here.
    Thanks a lot for taking the time to figure these out.

    Great work.

  3. #3
    OpenNingia is offline Senior Member
    Join Date
    Jul 2009
    Posts
    123

    Default

    Communication between background page and content script seems broken on 3.0.193.0.

    Didn't they fix that? Is a problem on my code?

    Content Script
    Code:
    function onMessageReceived(data)
    {
        console.log("Received message from Extension: " + data.message);
        }
    }
    
    var port = chrome.extension.connect();
    port.onMessage.addListener(onMessageReceived); 
    port.postMessage({message: "Hello!", values: [1,2,3]});
    Background Page
    Code:
    chrome.self.onConnect.addListener(onPortConnect);
    function onPortConnect(port) 
    {
          port.onMessage.addListener(onMessageReceived)
          port.postMessage({message: "Greetings, tab " + port.tab.id, values: [true,false,null]});
    }; 
    
    function onMessageReceived(data)
    {
        console.log("The content script said: " + data.message + " with values: " + data.values);
    }
    What is happening is that the background page receives the onConnect event, also receive a onMessage event, but when it post a message back to the content, the message is not received by the script.

    Do this work for you?

  4. #4
    OpenNingia is offline Senior Member
    Join Date
    Jul 2009
    Posts
    123

  5. #5
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    Yeah it's broken this version. Your code however looks correct.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  6. #6
    OpenNingia is offline Senior Member
    Join Date
    Jul 2009
    Posts
    123

    Default

    Ohhray

    Communication issues were fixed upstream! Next dev version should work

    http://code.google.com/p/chromium/is...etail?id=16228

  7. #7
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    That's great news.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  8. #8
    Mesafina is offline Junior Member
    Join Date
    Oct 2009
    Posts
    2

    Default

    I am having some trouble with getting a function to be accessable from the matched page.

    I have an extension that automatically adds a menu to a specific page and from that menu I need to be able to access objects and functions in a content script. However, when I try to use contentWindow.somefunction = somefunction in order to allow a link I added to the DOM to access somefunction, it says that contentWindow is undefined. I have tried using just window, as well as many combinations of window, contentWindow, and document, and nothing seems to work.

    Does anyone know how to make a function or an object and all it's properties and methods accessable to the webpage that is currently running, or at least to any DOM elements that have been added to said page?

    Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •