0

我试着环顾 Stackoverflow,但找不到任何专门针对此的东西。

所以基本上,我的网站有一个共享页面,如下所示:

http://domain.com/share.php?link=http://sharing.url

我的扩展是这样的:

{
 ...
  "browser_action": {
   "default_icon": "icon.ico",
   "default_popup": "schare.html"
 }
...
}

共享.html:

<style>
body, html{
margin:0;
padding:0;
}
iframe{
    width:520px;
    height:200px;
    margin:0;
}
</style>
<iframe id="iframes" frameborder="0"></iframe>
<script type="text/javascript" src="popup.js"></script>

和popup.js:

document.getElementById("iframes").setAttribute("src", "http://domain.com/share.php?link="+location.href+"");

但这是错误的 URL。我怎样才能在不做任何太花哨的事情的情况下获得标签网址?

4

2 回答 2

1

如果您在当前窗口中有一组选项卡,而不仅仅是一个,则下面的代码可能不起作用。这是修改后的版本

chrome.tabs.query({active : true, currentWindow: true}, function (tabs) { var tab = (tabs.length === 0 ? tabs : tabs[0]);
var activeTabUrl = tab.url; });

于 2013-01-25T15:56:00.920 回答
0

You can get the currently active tab by calling chrome.tabs.query. The callback function will receive the reference to the tab as an argument.

So to get the URL of active tab, you can use this:

chrome.tabs.query({active : true, currentWindow: true}, function (tab) {
    var activeTabUrl = tab.url;
});

Note, that the getCurrent() method uses a callback, so don't try to use in a linear code.

于 2012-08-04T03:08:11.887 回答