2010-09-10 48 views
3

我试图尝试使用我的第一个Google Chrome浏览器扩展程序并提出问题。我的最终目标是能够选择一个按钮,将执行以下操作:Google Chrome插件:如何检测选定标签的网址

  1. 抓住所选选项卡的当前网址(例如:www.google.com)

  2. 打开一个新标签使用步骤1中的URL,并追加一个查询字符串结尾(例如:www.google.com?filter=0)

目前,我能弄清楚如何打开创建一个新标签加载指定的URL。我不确定如何检测选定选项卡中的URL并在新选项卡中加载该值。建议?提前致谢!!下面

代码:

[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

可能重复:http://stackoverflow.com/questions/1979583/how-can-i-get-the-url-for-a-google-chrome-tab – 2010-09-10 02:54:29

回答

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

谢谢你的工作完美!只需要在我现有的功能中嵌套... – OtoNoOto 2010-09-10 18:18:13

0

chrome.tabs.getSelectedhas been deprecated。所以我们应该用tabs.query({active: true}...代替:

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