Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function in background from popup

Is there a way to call a function in the background script from the popup? I can't explain it much further than that question. It's not an error I'm having with what I'm trying to do but rather something I completely don't know how to do. I want to make it possible to click a button in the popup page that'll call a function defined in the background page.

like image 666
Anonymous Avatar asked Mar 26 '11 15:03

Anonymous


2 Answers

It is indeed possible, using Message Passing.

popup.js

$("#button").click(function(){
    chrome.runtime.sendMessage({ msg: "startFunc" });
});

background.js

var func = function(){
    alert("Success!");
};

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        if(request.msg == "startFunc") func();
    }
);
like image 162
mattsven Avatar answered Oct 31 '22 12:10

mattsven


Try this

 var bgPage = chrome.extension.getBackgroundPage();
 var dat =  bgPage.paste(); // Here paste() is a function that returns value.
like image 28
Exception Avatar answered Oct 31 '22 13:10

Exception