Google Chrome Plugin: Determining the URL of the selected tab

I am trying to complete my first Google Chrome extension and ask a question. My ultimate goal is to choose a button that will do the following:

  • Take the current URL of the selected tab (for example: www.google.com)

  • Open a new tab using the URL from step 1, and add the query line at the end (for example: www.google.com?filter=0)

Currently, I have been able to figure out how to open a new tab that loads the specified URL. I am not sure how to determine the URL from the selected tab and load this value in a 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"
  ]


}
+3
2
chrome.tabs.getSelected(null, function(tab) {
    alert(tab.url);
});
+4

chrome.tabs.getSelected . tabs.query({active: true}... tabs.query({active: true}...:

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

Source: https://habr.com/ru/post/1764095/


All Articles