Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

Get tab url from Chrome Extension in Javascript

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; });


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...