2014-09-27 29 views
0

我希望将标签更改为下一个标签,在HTML上执行一些操作(如获取某些数据),然后返回到“主”标签。总是打开两个标签。我可以改变选项卡(使用background.js)它应该是活跃的,但当我尝试得到HTML它总是得到background.html ...有什么办法从第二个选项卡获取HTML代码?我想我也可以'回到以前的选项卡',所以我真的需要:从第二个标签得到的HTML! (如果可能的话,在那里我可以做HTML行动?)从contentscript.js在下一个标签上回到上一个标签的Chrome扩展程序操作

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" 
} 
} 

调用background.js

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

背景.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"}); 
} 
}); 

回答

1

我想你试图从开发工具中为你的背景页面打开运行这个。

然后,活动窗口(正如tabs.query所查询的)就是那个具有单个选项卡的开发工具窗口。

你为什么要运行query?您已有window.tabs[i]中的选项卡,请使用该选项卡代替tabs[0]

+0

嗯...我正在运行'query'因为我真的不知道如何使用'window.tabs [i]'。可以说我需要*第二个选项卡上的单击按钮*(在'window.tab [i]')我应该使用'executeScript'?像'chrome.tabs.executeScript(window.tab [i],代码:{},function(result){});'?顺便说一句。是否可以在第二个选项卡(window.tab [i])上执行操作而不更改选项卡?我的意思是没有改变为'积极:真实,选择:真' – Buzka91 2014-09-29 06:10:02

+0

对不起,我忘记标记答案:) ty帮助:) – Buzka91 2014-10-14 05:55:43

相关问题