从chrome.tabs.executeScript返回值 [英] Return value from chrome.tabs.executeScript

查看:545
本文介绍了从chrome.tabs.executeScript返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在popup.js中,我使用以下代码显示当前选项卡的某个div ID内的所有文本 - 并显示在警报中。我想知道如何将div文本保存到popup.js中的变量中?

In popup.js i'm using the following code to display all the text within a certain div id of the current tab - and display in alert. I'm wondering how would it be possible to save the div text to a variable within popup.js?

chrome.tabs.executeScript(null,code:"alert(document.getElementById(\"DIVid\").innerText.split(' '))"});

以上工作正常,但是当我尝试这个时:

The above works fine, but when i try this:

var getText = chrome.tabs.executeScript(null,code:"document.getElementById(\"DIVid\").innerText.split(' ')"});

var getText = chrome.tabs.executeScript(null,code:"document.getElementById(\"DIVid\").innerText.split(' ')"},function(response){return response});

没有存储任何内容。我显然会以这种错误的方式去做。我究竟做错了什么?

Nothing is stored. I'm obviously going about this the wrong way. What am I doing wrong?

推荐答案

使用以下代码:

Use the following code,

var getText = Array();
chrome.tabs.executeScript(tabs[tab].id, {
    "code": "document.getElementById(\"_Your_ID_Here_\").innerText.split(' ')"
}, function (result) {
    for (i = 0; i < result[0].length; i++)
    getText [i] = result[0][i];
    console.log(getText);
});

由于 chrome.api

这篇关于从chrome.tabs.executeScript返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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