Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.tabs.executeScript return value to background.js from injected file [duplicate]

I made a chrome extension which run after user click on the icon (browser action). After user click on the icon the file background.js is running. It will check the tabs and I inject a js file in the tab.

File background.js

chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.query({'url':"URL TO SEARCH"}, function(tabs) {
        //console.log(tabs[0].id);
        chrome.tabs.executeScript(tabs[0].id, {file: "do.js"}, function (test){
            console.log(test);
        });
    });
});

The file do.js do some stuff (it works no issue) and i would like to return a value at the end of do.js but I'm stuck in code as I don't find the solution.

do.js

if ( Test1) {
    do something;
    return ok; //how to do that ????
}else{
    do someting;
    return not ok; //how to do ???
}

My question what is the code to add to do.js to return a simple text value. I have read this question, but i don't understand the answer.

Below the manifest.json

"background": {
    "scripts": ["background.js"]
},
"permissions": [
    "tabs", "http://*/*", "https://*/*","file:///*"
],
"browser_action": {
  "default_title": "Mute Hangout",
  "default_icon": "icon16.png"
},
"manifest_version": 2

Thank you

like image 686
St3ph Avatar asked Apr 21 '14 10:04

St3ph


1 Answers

The callback of chrome.tabs.executeScript will be exectued when injected script is exectued. And the last statement will be passed to the callback as the result parameter.

var result;

if (Test1) {
    do something
    result = "ok";
}else{
    do something
    result = "not ok";
}

// pls make sure that the result you want to pass to executeScript callback need to be the last statement of the injecting script
result
like image 176
Chickenrice Avatar answered Oct 15 '22 06:10

Chickenrice