javascript Chrome 扩展程序:从弹出窗口中获取当前标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13359421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:32:46  来源:igfitidea点击:

Chrome extension: Get current tab from popup

javascriptgoogle-chromemessage

提问by mythofechelon

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. However, I've never had to use them with popup pages and, from what I've read, it's much more difficult to do.

我之前使用过 Chrome 的消息传递系统,经过很多努力,我在很多情况下都设法让它工作。但是,我从来没有将它们与弹出页面一起使用,而且从我所读到的内容来看,要做到这一点要困难得多。

The timeline I've managed to figure out so far is this:

到目前为止,我设法弄清楚的时间表是这样的:

  1. popup.html/ popup.js:    Button is clicked
  2. popup.html/ popup.js:    Request / message is sent to the content script
  3. contentScript.js:           Request / message is received from the popup page
  4. contentScript.js:           The title and URL of the current tab are stored in a variable
  5. contentScript.js:           The 2 variables are sent as a stringified response
  6. popup.html/ popup.js:    The 2 variables are parsed from the response
  1. popup.html/ popup.js: 按钮被点击
  2. popup.html/ popup.js: 请求/消息发送到内容脚本
  3. contentScript.js:从弹出页面收到请求/消息
  4. contentScript.js: 当前标签的标题和 URL 存储在一个变量中
  5. contentScript.js:这两个变量作为字符串化响应发送
  6. 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.getSelectedcan only be used in a background page / script. 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
  • 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)
  • chrome.tabs.getSelected只能在后台页面/脚本中使用。这是否意味着根本不需要使用内容脚本?
  • 消息不能直接从内容脚本发送到弹出页面,它们必须通过后台页面
  • 必须先确认弹出窗口存在,然后才能将消息传递给它(虽然,我想我知道如何做到这一点)

I already found Chrome's message passing system difficult enough but, this has totally confused me. Hence, this post.

我已经发现 Chrome 的消息传递系统已经够难了,但是,这让我很困惑。因此,这篇文章。

回答by fny

chrome.tabs.getSelectedis deprecated. You should use chrome.tabs.queryinstead.

chrome.tabs.getSelected已弃用。你应该chrome.tabs.query改用。

chrome.tabs.queryrequires 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à:

等等:

chrome.tabs.query(query, callback);