代码之家  ›  专栏  ›  技术社区  ›  seanrco

googlechrome插件:如何检测选中标签的URL

  •  3
  • seanrco  · 技术社区  · 14 年前

    我试图尝试我的第一个谷歌浏览器扩展和有一个问题。我的最终目标是能够选择一个执行以下操作的按钮:

    1. 使用步骤1中的URL打开一个新选项卡,并在末尾附加一个查询字符串(例如:www.google.com?过滤器(0)

    目前,我已经能够找到如何打开一个新的选项卡,创建一个新的选项卡来加载指定的URL。我不确定的是如何从所选选项卡检测URL并将该值加载到新选项卡中。建议?提前谢谢!!

    代码如下:

    [弹出窗口.html]

        <html>
    <head>
    
    <style>
    
    body {
      min-width:175px;
      overflow-x:hidden;
    }
    
    </style>
    
    
    <script>
    
     function createTab() {
      chrome.tabs.create({'url': 'http://www.google.com'});
     }
    
     function show_alert()
     {
     alert("I am an alert box!");
     }
    
    </script>
    </head>
    
    <body>
    
    <input type="button" onclick="createTab()" value="Create New Tab" />
    <hr/>
    <input type="button" onclick="show_alert()" value="Show alert box" />
    
    </body>
    </html>
    

    {
      "name": "IGX Plugin",
      "version": "1.0",
      "description": "IGX Plugin",
    
      "browser_action": {
        "default_icon": "favicon.ico",
     "popup": "popup.html"
      },
      "permissions": [
        "tabs"
      ]
    
    
    }
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   Alon Gubkin    14 年前
    chrome.tabs.getSelected(null, function(tab) {
        alert(tab.url);
    });
    
        2
  •  0
  •   Keith    8 年前

    chrome.tabs.getSelected has been deprecated . 所以我们应该使用 tabs.query({active: true}... 取而代之的是:

    chrome.tabs.query({active: true}, tabs => alert(tabs[0].url));