Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about chrome.tabs.executeScript( id,details, callback)

This function has a callback like function(array of any result) {...};

But I don't know what is the result means.

For example,

chrome.tabs..executeScript(null,    {code:"var x = 10"},    function(){}); 

how to return the x to the callback?

like image 873
dusthand Avatar asked Oct 31 '12 19:10

dusthand


People also ask

What is executeScript ()?

executeScript() Injects a script into a target context. The script is run at document_idle by default. Note: This method is available in Manifest V3 or higher in Chrome and Firefox 101.

What is a Chrome tab?

You can open as many tabs as you want in Chrome. You can also view all your tabs and switch between them. When you open a new tab, a personalized New Tab page based on your browsing history appears.


1 Answers

The result of a script is the last expression being evaluated. So in your example you could use:

chrome.tabs.executeScript( null, {code:"var x = 10; x"},    function(results){ console.log(results); } ); 

This will log [10] to the extension's console.

results is actually an array of values because if the page has more than one frame you can specify that the script should be injected in each one of them and get the result of all injections. See the specification for executeScript. If you don't specify allFrames: true, then results will always be a single element array.

like image 122
rsanchez Avatar answered Nov 11 '22 17:11

rsanchez