Html 下一个选项卡上的Chrome扩展操作和返回上一个选项卡

Html 下一个选项卡上的Chrome扩展操作和返回上一个选项卡,html,google-chrome,google-chrome-extension,Html,Google Chrome,Google Chrome Extension,我想把标签改成下一个,在HTML上做一些动作(比如获取一些数据),然后回到“主”标签。始终打开两个选项卡。我可以更改选项卡(使用background.js),它应该是活动的,但当我尝试获取html时,它总是获取background.html。。。有没有办法从第二个选项卡获取html代码?我想我也可以做“返回上一个选项卡”,所以我真正需要的是:从第二个选项卡获取html!(如果可能,我可以在哪里对html执行操作?) manifest.json { "manifest_version": 2,

我想把标签改成下一个,在HTML上做一些动作(比如获取一些数据),然后回到“主”标签。始终打开两个选项卡。我可以更改选项卡(使用background.js),它应该是活动的,但当我尝试获取html时,它总是获取background.html。。。有没有办法从第二个选项卡获取html代码?我想我也可以做“返回上一个选项卡”,所以我真正需要的是:从第二个选项卡获取html!(如果可能,我可以在哪里对html执行操作?)

manifest.json

{
  "manifest_version": 2,

  "name": "name",
  "description": "desc.",
  "version": "1.0",

"background": {
  "scripts": ["background.js"]
},

"content_scripts" :[
{
  "matches":[
    "http://www.thecrims.com/*"
  ],
  "js":["contentscript.js"]
}
],

"permissions": [
  "http://www.thecrims.com/*",
  "https://www.thecrims.com/*",
  "background",
  "tabs"
],
"browser_action": {
  "default_icon": "icon.jpg"
}
}
从contentscript.js调用background.js

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response);
});
background.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {

console.log(sender.tab ?
            "from a content script:" + sender.tab.url :
            "from the extension");
if (request.greeting == "hello")
{
    chrome.windows.getLastFocused(
         // Without this, window.tabs is not populated.
         {populate: true},
         function (window)
         {
          var foundSelected = false;
          for (var i = 0; i < window.tabs.length; i++)
          {
           // Finding the selected tab.
           if (window.tabs[i].active)
           {
            foundSelected = true;
           }
           // Finding the next tab.
           else if (foundSelected)
           {
            // Selecting the next tab.
            chrome.tabs.update(window.tabs[i].id, {active: true, selected: true});

            //GET HTML FROM ^
            chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
                console.log(tabs[0]);//this give me BACKGROUND.HTML html... i want from tabs[0]!!
            });

            return;
           }
          }
         });
  sendResponse({farewell: "goodbye"});
}
});
chrome.runtime.onMessage.addListener(
功能(请求、发送方、发送响应){
console.log(sender.tab?
“来自内容脚本:”+sender.tab.url:
“从延期开始”);
if(request.greeting==“hello”)
{
chrome.windows.getLastFocused(
//如果没有此选项,则不会填充window.tabs。
{populate:true},
功能(窗口)
{
var foundSelected=false;
对于(变量i=0;i
我想您正在尝试从“开发工具”中为您打开的后台页面运行此功能

然后,活动窗口(由
tabs.query
查询)将恰好是具有单个选项卡的开发工具窗口


为什么还要运行
查询
?您已经在
窗口中有了选项卡。选项卡[i]
,请使用该选项卡而不是
选项卡[0]

。。。我正在运行
查询
,因为我真的不知道如何使用
窗口.tabs[i]
。假设我需要单击第二个选项卡上的按钮(在
窗口上。选项卡[i]
),我应该使用
executeScript
?类似于
chrome.tabs.executeScript(window.tab[i],代码:{},函数(结果){})?顺便问一下,是否可以在不更改选项卡的情况下对第二个选项卡(window.tab[i])执行操作?我的意思是不更改为
活动:真,选定:真