如何获取打开Chrome扩展程序的标签页ID [英] How to get the tab id where the chrome extension is opened
问题描述
我要在新窗口中打开扩展名. Background.js
I am opening the extension in the new window. Background.js
chrome.browserAction.onClicked.addListener(function(msg, sender, response){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {tabId: tabs[0].id}, function(response) {});
// creating new window.
chrome.windows.create({ url: 'popup.html', width: 320, height: 480})
})
})
在打开新窗口之前,标签页ID将是活动的标签页ID,表示我在其中打开了chrome扩展程序(例如:google.com).我正在尝试访问 popup.js 中的标签ID.因为我想访问打开chrome扩展名的页面(google.com)的DOM.我怎样才能做到这一点?还是还有其他方法可以在 popup.js 中获取标签ID?
Before opening new window, the tab ID will be the active tab ID means where i opened the chrome extension(eg: google.com). I am trying to access that tab ID in popup.js. Because i want to access the DOM of the page(google.com) where chrome extension is opened. How can i do that? or is there any other way to get the tab ID in the popup.js?
在 popup.js
chrome.windows.getAll({populate:true}, function(tabs){
console.log(tabs)
});
我在这里获取popup.js中的所有窗口标签,但是在这里我不知道打开扩展名(google.com)的标签ID?
Here i am getting all the window tabs in popup.js, but here i do not know that tab ID where extension is opened(google.com)?
推荐答案
实际上非常简单
chrome.tabs.query({active: true}, function(tabs){})
它提供了所有活动的窗口选项卡并访问其他窗口DOM(google.com)
Its is giving what are all the active window tabs and accessing other window DOM(google.com)
chrome.tabs.query({active: true}, function(tabs){
chrome.tabs.executeScript(tabs[0].id,{ //tabs[0].id will give the tab id where extension is opened.
code: 'document' // any javascript statement
})
})
这篇关于如何获取打开Chrome扩展程序的标签页ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!