3

I am trying to attempt my first Google Chrome Extension and have a question. My end goal is to be able to select a button which will perform the following:

  1. Grab the current URL of the selected tab (Ex: www.google.com)

  2. Open a new tab using the URL from step 1 and appending a query string to the end (Ex: www.google.com?filter=0)

Currently, I was able to figure out how to open a create a new tab which loads a specified URL. What I am unsure of how to detect the URL from the selected tab and load that value in the new tab. Suggestions? Thanks in advance!!

Code below:

[popup.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>

[manifest.json]

{
  "name": "IGX Plugin",
  "version": "1.0",
  "description": "IGX Plugin",

  "browser_action": {
    "default_icon": "favicon.ico",
 "popup": "popup.html"
  },
  "permissions": [
    "tabs"
  ]


}
seanrco
  • 935
  • 5
  • 16
  • 32

2 Answers2

4
chrome.tabs.getSelected(null, function(tab) {
    alert(tab.url);
});
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
0

chrome.tabs.getSelected has been deprecated. so we should use tabs.query({active: true}... instead:

chrome.tabs.query({active: true}, tabs => alert(tabs[0].url));
Keith
  • 150,284
  • 78
  • 298
  • 434