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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With