使用JavaScript从Chrome扩展程序获取标签网址 [英] Get tab url from Chrome Extension in Javascript

查看:91
本文介绍了使用JavaScript从Chrome扩展程序获取标签网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

/ p>

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

我的扩展是这样的:

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

schare.html:

 < style> 
body,html {
margin:0;
padding:0;
}
iframe {
width:520px;
height:200px;
保证金:0;
}
< / style>
< iframe id =iframesframeborder =0>< / iframe>
< script type =text / javascriptsrc =popup.js>< / script>

和popup.js:

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

但这是错误的网址。

解决方案

下面的代码可能不起作用,如果你有一个当前窗口中的选项卡数组,而不仅仅是一个。这里有一个修改后的版本:
$ b $ p $ ch codeh $ chrome.tabs.query({active:true,currentWindow:true},function(tabs){
var tab =(tabs.length === 0?tabs:tabs [0]);

var activeTabUrl = tab.url;
});


I tried looking around Stackoverflow but couldn't find anything specifically for this.

So basically, I have a share page for my website that goes like this:

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

My extension goes like this:

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

schare.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>

and popup.js:

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

But that's the wrong URL. How can I get the tab url in there without doing anything too fancy?

解决方案

The code below may not work if you have an array of tabs in the current window and not just one. Here's a modified version

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

这篇关于使用JavaScript从Chrome扩展程序获取标签网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆