如何在我的页面操作弹出窗口中获取当前打开的选项卡的 URL? [英] How to get the currently opened tab's URL in my page action popup?

查看:18
本文介绍了如何在我的页面操作弹出窗口中获取当前打开的选项卡的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

使用 chrome.tabs.query 带有以下参数:

Use chrome.tabs.query with the following parameters:

  • queryInfo 对象:
    • active: true - 获取活动标签
    • lastFocusedWindow: true - 选择活动窗口
    • queryInfo object:
      • active: true - To get the active tab
      • lastFocusedWindow: true - To select the active window

      代码片段:

      // 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
      });
      

      activeTab 权限足以使其工作.

      这篇关于如何在我的页面操作弹出窗口中获取当前打开的选项卡的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆