2011-05-08 56 views
1

我试图向(我自己的)谷歌浏览器扩展程序添加“共享”功能,但是我偶然发现了变量和网址的问题(对于JavaScript我毫无用处)。将变量插入网址

我的代码如下,如果有人可以指导我去哪里我会非常感激。

<script> 
    chrome.tabs.getSelected(null, function(tab) { 
     document.getElementById('longLink').value = tab.url; 
     }); 
     var shareURL = document.getElementById('longLink') 
</script> 
<a href="https://twitter.com/?status=" + shareURL + "&choe=UTF-8" target="_blank">Tweet This</a> 

我也试着

<a href="https://twitter.com/?status=" + encodeURIComponent(shareURL); + "&choe=UTF-8" target="_blank">Tweet This</a> 

最后,这个方法我试过

<script> 
    function tweet() { 
    var twitter='http://twitter.com/?status='+encodeURIComponent(tab.url); 
    chrome.tabs.create({url: twitter}); 
    } 
</script> 
<a onClick="tweet()" href="" target="_blank">Tweet</a> 
+1

然后呢?问题似乎不完整。发生了什么错误,发生了什么,预期的是什么? – 2011-05-08 18:13:07

+0

它加载没有URL的网页,它应该打开带有链接的URL。 https://twitter.com?status=example.com然而它加载“状态?=”(没有链接) – itsdaniel0 2011-05-08 18:15:20

回答

0
// Takes a url, a GET parameter name and value and returns 
// the given URL but with the given parameter at the end of 
// the query portion. 
function urlWithParameter(url, name, value) { 
    // Find the fragment since the query ends where the fragment starts. 
    var fragmentStart = url.indexOf('#'); 
    if (fragmentStart < 0) { fragmentStart = url.length; } 
    var urlBeforeFragment = url.substring(0, fragmentStart); 
    // If there is no query (no '?' in URL) then start the parameter with 
    // a '?' to create a query. Otherwise separate the parameter from 
    // the existing query with a '&'. 
    // We use encodeURIComponent which assumes UTF-8 to escapes special URL 
    // characters like '#', '&', '?', '%', and '='. 
    // It assumes UTF-8. Any replacement that does not assume UTF-8 must escape 
    // at least the code-points listed above. 
    return urlBeforeFragment + (urlBeforeFragment.indexOf('?') < 0 ? '?' : '&') 
     + encodeURIComponent(name) + '=' + encodeURIComponent(value) 
     + url.substring(fragmentStart); 
} 

可因此用于

<script>/* the function above */</script> 
<a onclick="this.href = urlWithParameter('http://twitter.com/', 'status', tab.url)" href="#">...</a> 

做你想做的事,同时仍然尊重链接target隐含<base target="...">,仍然显示悬停的有用的网址。

或者,如果你只需要操作一个参数,那么您可以使用您尽早解决这样

<a onclick="this.href = 'http://twitter.com/?status=' + encodeURIComponent(tab.url)" href="http://twitter.com/">...</a> 

编辑:为了得到它与异步铬存取工作,请尝试以下操作:

<script> 
function redirectWithSelectedTabUrl(link) { 
    chrome.tabs.getSelected(null, function (tab) { 
    window.location.href = tab.url 
     ? link.href + "?status=" + encodeURIComponent(tab.url) 
     : link.href; 
    }; 
    return false; 
} 
</script> 

<a href="http://twitter.com/" onclick="return redirectWithSelectedTabUrl(this)">...</a> 

这很简单,适用于各种浏览器,但它会忽略target,并且可能不会沿引用标头发送。

+0

@MikeSamuel好吧,不要说谎,这段代码已经完全失去了我。我不明白其中的任何一点,正如我在JavaScript中无用的问题所述,但从我的理解来看,这是实现我想做的唯一方法。 – itsdaniel0 2011-05-08 18:25:40

+0

@ itsdaniel0,编辑后使其用法更清晰。 – 2011-05-08 18:36:23

+0

@MikeSamuel谢谢,效果很好!现在我刚刚遇到结束为“空”(或根本没有)的状态。 – itsdaniel0 2011-05-08 19:58:14