5

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

St3ph
  • 2,232
  • 17
  • 17
  • 1
    No idea why this is marked as a duplicate - the other question is when using executeScript with a 'code' string, this question is about 'file' mode. – Craig Francis Jan 01 '20 at 14:11

1 Answers1

14

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
Chickenrice
  • 5,727
  • 2
  • 22
  • 21
  • Thank you very much it works, I spent 2 hours on this point... which is so simple in fact... – St3ph Apr 21 '14 at 20:16