chrome.tabs.executeScript():如何获取内容脚本的结果? [英] chrome.tabs.executeScript(): How to get result of content script?

查看:1437
本文介绍了chrome.tabs.executeScript():如何获取内容脚本的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 chrome.tabs.executeScript 的文档( MDN ),回调函数接受一个array任何结果结果集来自脚本的执行。你究竟如何使用它来获得结果?我所有的尝试都以 undefined 传递给回调。



我试过在我的内容脚本结束了,它抛出了 Uncaught SyntaxError:非法返回语句。我尝试使用可选的代码对象参数 {code:returnHello;} 没有成功。

我觉得我不明白每个注入框架中脚本的结果是什么意思,在文档中。

解决方案

chrome.tabs.executeScript()返回一个

脚本的结果是最后评估的语句的值,可以是函数返回的值(即IIFE,使用 return 语句) 。通常,这将与控制台显示的执行结果(不是 console.log())相同,但结果是)如果您执行了代码/脚本( F12 )(例如,用于脚本 var foo ='我的结果'; foo; 结果数组将包含字符串我的结果作为一个元素)。如果您的代码很短,您可以尝试从控制台执行。



以下是一些示例代码,摘自我的另一个答案
$ b chrome.browserAction .onClicked.addListener(function(tab){
console.log('Injecting content script(s)');
//在Firefox上document.body.textContent可能更合适
chrome .tabs.executeScript(tab.id,{
code:'document.body.innerText;'
//如果您有些更复杂的事情,您可以使用IIFE:
//代码:'(function(){return document.body.innerText;})();'
//如果你的代码很复杂,你应该将它存储在
//单独的.js文件中,你注入文件:属性。
},receiveText);
});

//tabs.executeScript()返回执行脚本
//在结果数组中的结果,每帧中有一个条目,其中脚本
//被注入。
函数receiveText(resultsArray){
console.log(resultsArray [0]);
}

这将注入一个内容脚本来获得。当浏览器操作按钮被点击时,< body> 的innerText 您需要 activeTab 权限。



作为这些内容的一个例子,您可以打开网页控制台( F12 )并输入 document.body.innerText; (function(){return document.body。 innerText;})(); 看看会返回什么。


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.

解决方案

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.

这篇关于chrome.tabs.executeScript():如何获取内容脚本的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆