Sam*_*Sam 1 google-chrome google-chrome-extension
我正在新窗口中打开扩展程序。 背景.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})
})
})
Run Code Online (Sandbox Code Playgroud)
在打开新窗口之前,标签 ID 将是活动标签 ID,表示我打开 chrome 扩展程序的位置(例如:google.com)。我正在尝试在popup.js 中访问该选项卡 ID 。因为我想访问打开 chrome 扩展程序的页面(google.com)的 DOM。我怎样才能做到这一点?或者有没有其他方法可以在popup.js 中获取标签 ID ?
在popup.js 中尝试其他方式
chrome.windows.getAll({populate:true}, function(tabs){
console.log(tabs)
});
Run Code Online (Sandbox Code Playgroud)
在这里,我获得了 popup.js 中的所有窗口选项卡,但在这里我不知道打开扩展程序的选项卡 ID(google.com)?
其实很简单
chrome.tabs.query({active: true}, function(tabs){})
Run Code Online (Sandbox Code Playgroud)
它提供了所有活动窗口选项卡并访问其他窗口 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
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4052 次 |
| 最近记录: |