简体   繁体   English

Chrome 扩展:从弹出窗口获取当前选项卡

[英]Chrome extension: Get current tab from popup

I'm writing a Chrome extension and, in one part of it, I need to get the current tab's title and URL when a button on the popup page is clicked.我正在编写一个 Chrome 扩展程序,其中一部分需要在单击弹出页面上的按钮时获取当前选项卡的标题和 URL。

I've worked with Chrome's message passing system before and, after much effort, managed to get it to work, on many occasions.我之前曾使用过 Chrome 的消息传递系统,经过大量努力后,我在很多情况下都设法让它工作。 However, I've never had to use them with popup pages and, from what I've read, it's much more difficult to do.但是,我从来没有在弹出页面中使用过它们,而且根据我的阅读,这样做要困难得多。

The timeline I've managed to figure out so far is this:到目前为止我设法弄清楚的时间表是这样的:

  1. popup.html / popup.js : Button is clicked popup.html / popup.js :单击按钮
  2. popup.html / popup.js : Request / message is sent to the content script popup.html / popup.js : 请求/消息发送到内容脚本
  3. contentScript.js : Request / message is received from the popup page contentScript.js : 从弹出页面接收请求/消息
  4. contentScript.js : The title and URL of the current tab are stored in a variable contentScript.js : 当前tab的title和URL保存在一个变量中
  5. contentScript.js : The 2 variables are sent as a stringified response contentScript.js :这 2 个变量作为字符串化响应发送
  6. popup.html / popup.js : The 2 variables are parsed from the response popup.html / popup.js :从响应中解析出 2 个变量

Usually I'd be able to figure this out but, I've read a few things that have thrown a spanner in the works, such as:通常我能够弄清楚这一点,但是,我已经阅读了一些在工作中引发麻烦的事情,例如:

  • chrome.tabs.getSelected can only be used in a background page / script. chrome.tabs.getSelected只能在后台页面/脚本中使用。 Does this mean that content scripts don't need to be used at all?这是否意味着根本不需要使用内容脚本?
  • Messages cannot be sent directly from the content script to the popup page, they have to go via a background page消息不能直接从内容脚本发送到弹出页面,它们必须通过后台页面发送到 go
  • A popup window has to be confirmed as existing before a message can be passed to it (although, I think I know how to do this)必须先确认弹出窗口 window 存在,然后才能将消息传递给它(尽管我想我知道该怎么做)

I already found Chrome's message passing system difficult enough but, this has totally confused me.我已经发现 Chrome 的消息传递系统已经够难了,但这让我很困惑。 Hence, this post.因此,这篇文章。

chrome.tabs.getSelected is deprecated. 不推荐使用chrome.tabs.getSelected You should use chrome.tabs.query instead. 您应该使用chrome.tabs.query

chrome.tabs.query requires two parameters: a query object and a callback function that takes the array of resulting tabs as a parameter. chrome.tabs.query需要两个参数:一个查询对象和一个回调函数,它将结果选项卡数组作为参数。

You can get the "current tab" by querying for all tabs which are currently active and are in the current window. 您可以通过查询当前处于活动状态且位于当前窗口中的所有选项卡来获取“当前选项卡”。

var query = { active: true, currentWindow: true };

Since the query will return a Tab array containing the current tab alone, be sure to take the first element in the callback 由于查询将返回仅包含当前选项卡的Tab数组,因此请务必获取回调中的第一个元素

function callback(tabs) {
  var currentTab = tabs[0]; // there will be only one in this array
  console.log(currentTab); // also has properties like currentTab.id
}

Et voilà: Etvoilà:

chrome.tabs.query(query, callback);

Another way is to send a message from the popup to the content script which then returns the URL window.location.href另一种方法是从弹出窗口向内容脚本发送消息,然后返回 URL window.location.href

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM