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

匿名 (未验证) 提交于 2019-12-03 02:23:02

问题:

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?

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!