Javascript 如何在我的页面操作弹出窗口中获取当前打开的选项卡的 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10413911/
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-08-24 01:12:17  来源:igfitidea点击:

How to get the currently opened tab's URL in my page action popup?

javascriptgoogle-chromegoogle-chrome-extension

提问by Jophin Joseph

I want to create an extension for automatically logging into my servers. So I created a background page to check the current URL and if it conforms to my URL regex, I'll display the page action icon. On click of the page action I'm opening a popup with some fields. I need to get the currently opened URL and fill it in one of the fields in the popup(like when we click the standard bookmark page action, the URL gets automatically filled in the popup that opens). How can I do something like this in chrome extensions? I tried Message Passing from the background page to the popup html but it is not working. Is it possible to send messages like that? Also I tried setting onload for the popup html file but i noticed that it is not triggering. Please suggest a suitable method to deal with this scenario.

我想创建一个自动登录到我的服务器的扩展。所以我创建了一个后台页面来检查当前的 URL,如果它符合我的 URL 正则表达式,我将显示页面操作图标。单击页面操作时,我将打开一个包含一些字段的弹出窗口。我需要获取当前打开的 URL 并将其填充到弹出窗口中的字段之一中(例如,当我们单击标准书签页面操作时,该 URL 会自动填充到打开的弹出窗口中)。我怎样才能在 chrome 扩展中做这样的事情?我尝试了从后台页面到弹出 html 的消息传递,但它不起作用。可以发送这样的消息吗?我还尝试为弹出的 html 文件设置 onload,但我注意到它没有触发。请提出一种合适的方法来处理这种情况。

回答by Rob W

Use chrome.tabs.querywith the following parameters:

使用chrome.tabs.query下列参数:

  • queryInfo object:
    • active: true- To get the active tab
    • lastFocusedWindow: true- To select the active window
  • callback function:
    This function receives one argument: An array of matched tabs. Since only one window can be active, and one tab within this window, the array has only one element. This element is an object with the Tabsignature.
  • 查询信息对象:
    • active: true- 获取活动标签
    • lastFocusedWindow: true- 选择活动窗口
  • 回调函数:
    该函数接收一个参数:一组匹配的选项卡。由于只有一个窗口可以处于活动状态,并且该窗口中有一个选项卡,因此该数组只有一个元素。这个元素是一个带有Tab签名的对象。

Code snippet:

代码片段:

// Do NOT forget that the method is ASYNCHRONOUS
chrome.tabs.query({
    active: true,               // Select active tabs
    lastFocusedWindow: true     // In the current window
}, function(array_of_Tabs) {
    // Since there can only be one active tab in one active window, 
    //  the array has only one element
    var tab = array_of_Tabs[0];
    // Example:
    var url = tab.url;
    // ... do something with url variable
});

The activeTabpermission is sufficient for this to work.

activeTab许可是足够的工作。

回答by dipole_moment

You can also use promises for a cleaner way of retrieving a tab:

您还可以使用 promise 以更简洁的方式检索选项卡:

  getCurrentTab().then(function(tab){
    // Do something w/tab
  });

  function getCurrentTab(){
    return new Promise(function(resolve, reject){
      chrome.tabs.query({
        active: true,               // Select active tabs
        lastFocusedWindow: true     // In the current window
      }, function(tabs) {
        resolve(tabs[0]);
      });
    });
  }