chrome.tabs.executeScript(): How to get result of content script?
Asked Answered
S

1

10

According to the documentation for chrome.tabs.executeScript (MDN), the callback function accepts an "array of any result" result set from the execution of the script(s). How exactly do you use this to get results? All of my attempts end up with undefined being passed to the callback.

I have tried returning a value at the end of my content script, which threw a Uncaught SyntaxError: Illegal return statement. I tried using the optional code object argument {code: "return "Hello";} with no success.

I feel like I am not understanding what is meant by "The result of the script in every injected frame", in the documentation.

Statampere answered 10/1, 2017 at 20:41 Comment(2)
Possible duplicate of about chrome.tabs.executeScript( id,details, callback)Fitly
@Fitly It may be a duplicate answer, but this question, I feel, has a better and more specific question title. The other is quite general.Statampere
A
17

chrome.tabs.executeScript() returns an Array with "the result of the script" from each tab/frame in which the script is run.

"The result of the script" is the value of the last evaluated statement, which can be the value returned by a function (i.e. an IIFE, using a return statement). Generally, this will be the same thing that the console would display as the results of the execution (not console.log(), but the results) if you executed the code/script from the Web Console (F12) (e.g. for the script var foo='my result';foo;, the results array will contain the string "my result" as an element). If your code is short, you can try executing it from the console.

Here is some example code taken from another answer of mine:

chrome.browserAction.onClicked.addListener(function(tab) {
    console.log('Injecting content script(s)');
    //On Firefox document.body.textContent is probably more appropriate
    chrome.tabs.executeScript(tab.id,{
        code: 'document.body.innerText;'
        //If you had something somewhat more complex you can use an IIFE:
        //code: '(function (){return document.body.innerText;})();'
        //If your code was complex, you should store it in a
        // separate .js file, which you inject with the file: property.
    },receiveText);
});

//tabs.executeScript() returns the results of the executed script
//  in an array of results, one entry per frame in which the script
//  was injected.
function receiveText(resultsArray){
    console.log(resultsArray[0]);
}

This will inject a content script to get the .innerText of the <body> when the browser action button is clicked. you will need the activeTab permission.

As an example of what these produce, you can open up the web page console (F12) and type in document.body.innerText; or (function (){return document.body.innerText;})(); to see what will be returned.

Affaire answered 10/1, 2017 at 20:59 Comment(5)
So is the documentation just not descriptive enough, or in the Javascript world am I supposed to just know that is what is meant by "The result of the script"?Statampere
@RayfenWindspear, IMO: The documentation is insufficient. It could be much clearer. Given how it is worded, I initially had to think a moment about what they might mean by "the result of the script" (and test to verify) when I first read the documentation. It is the most likely possible meaning, which you might have been able to guess at with a bit more familiarity with JavaScript.Affaire
I tried using IIFE, even a simple script like "a = 1; a;" but I don't get any results back when my script is a webpack module. If it is a simple (not bundled) script - it worksProlong
@BenBracha Did you make it work with webpack ? I have the same issue with webpack.Kudos
I think this is possible with the webpack WrapperPlugin. Use it like this: new WrapperPlugin({ test: "activeElementSelectorCalculator.js", footer: "window.result", }), And your content script like this: window.result = 1; The window.result will be added to the webpack module as a footer using the "WrapperPlugin" in webpack config, so it will be the "last value" of running this scriptProlong

© 2022 - 2024 — McMap. All rights reserved.