Google Chrome Plugin: How to detect URL of selected tab
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:
Grab the current URL of the selected tab (Ex: www.google.com)
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="开发者_Python百科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"
]
}
chrome.tabs.getSelected(null, function(tab) {
alert(tab.url);
});
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));
精彩评论