jQuery 动态 URL 缩短器

发布于 2024-08-12 03:38:15 字数 472 浏览 5 评论 0原文

我正在寻找一个动态 URL 缩短器,就像 tweetdeck 的工作方式一样。我发现许多 jQuery 和通用 javascript 插件都会获取 url,并在按下按钮时通过缩短服务(例如 bit.ly)运行它。然而,我还没有找到一个可以即时执行此操作的人。我的第一个问题是这是否已经存在于某个地方?其次,如果没有,那么识别文本框中需要缩短的 URL 的最佳方法是什么?我的想法:

  1. 在该文本区域的 onKeyUp 上运行查找 http 的文本,
  2. 如果找到,则抓取整个 URL(如何确定结尾?可能是句点、逗号、空格等...)
  3. 确保 URL 不是已经是 bit.ly URL
  4. 验证 URL(发出请求并确保 http 响应没有错误,bit.ly 已经这样做了吗?)
  5. 如果有效,则将 url 发送到 bit.ly 的 API 并获取响应
  6. 替换长 URL 与短 URL 位于文本区域。

想法?

I'm looking for an on the fly URL shortener much like how tweetdeck works. I have found many jQuery and general javascript plugins that take a url and run it through a shortening service such as bit.ly when a button is pressed. However, I have not been able to find one that does it on the fly. My first question is does this already exist someplace? Secondly, if it doesn't, then what would be the best way to recognize a URL that needs to be shortened inside a textbox? My thoughts:

  1. On onKeyUp of that text area run through the text looking for http
  2. If found grab the whole URL (how do I determine the end? could be period, comma, space, etc...)
  3. Make sure the URL isn't already a bit.ly URL
  4. Validate the URL (make a request and make sure the http response is not an error, does bit.ly already do this?)
  5. If valid, send the url to bit.ly's API and get the response
  6. Replace the long URL with the short URL in the text area.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

咽泪装欢 2024-08-19 03:38:15

以下是如何使用 Bitly API 和 jQuery 获取缩短 URL 的示例:

function get_short_url(long_url, login, api_key, func)
{
    $.getJSON(
        "http://api.bitly.com/v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response)
        {
            func(response.data.url);
        }
    );
}

以下代码可用于获取短 URL:

/*
Sign up for Bitly account at
 https://bitly.com/a/sign_up

and upon completion visit
https://bitly.com/a/your_api_key/ 
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});

Here is an example how to get a shortened URL with Bitly API and jQuery:

function get_short_url(long_url, login, api_key, func)
{
    $.getJSON(
        "http://api.bitly.com/v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response)
        {
            func(response.data.url);
        }
    );
}

The following code could be used to get a short URL:

/*
Sign up for Bitly account at
 https://bitly.com/a/sign_up

and upon completion visit
https://bitly.com/a/your_api_key/ 
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});
秋日私语 2024-08-19 03:38:15

我猜 Bitly 的 API 略有变化。现在,您实际上只需要一个访问令牌来请求一个短 URL。

遵循最佳实践,我创建了以下纯 Javascript 代码段:

getShortUrl: function(url, callback)
{
   var accessToken = '___YOUR_ACCESS_TOKEN___';
   var url = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + accessToken + '&longUrl=' + encodeURIComponent(url);

    $.getJSON(
        url,
        {},
        function(response)
        {
            if(callback)
                callback(response.data.url);
        }
    );
},

I guess the API of Bitly has changed slightly. You now only really need an access token to request a short URL.

Following the best practices, I created the following Javascript-only snippet:

getShortUrl: function(url, callback)
{
   var accessToken = '___YOUR_ACCESS_TOKEN___';
   var url = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + accessToken + '&longUrl=' + encodeURIComponent(url);

    $.getJSON(
        url,
        {},
        function(response)
        {
            if(callback)
                callback(response.data.url);
        }
    );
},
北城孤痞 2024-08-19 03:38:15

动态比特将很难做到可靠且快速。

人们大多数时候不会输入http,甚至不会输入www。

正如你所说,最后将很难确定 url 是否包含空格,或者更糟糕的是,由于用户没有输入空格,所以会进入下一个句子。

如果人们需要怎么办事后更改网址,因为他们输入了 http://stakoverflow.com/ 而不是 https://stackoverflow.com/

我认为最好的解决方案是文本编辑器上的“插入缩短的网址”按钮,让人们可以这样做。或者,在发布帖子时在服务器端执行此操作。

The on the fly bit is going to be difficult to make reliable and speedy.

People won't type http most of the time or even www.

The end, like you said, is going to be hard to determine if the url contains a space or worse, runs into the next sentence because the user didn't put in a space.

And what if people need to change the url after the fact because they typed http://stakoverflow.com/ instead of https://stackoverflow.com/ ?

I think the best solution would be an "insert shortened url" button on your text editor that allowed people to do just that. Or, do it server-side when the post is made.

酒解孤独 2024-08-19 03:38:15

您还可以在服务器上使用 php 和curl 生成一个短网址,这样您就不必在网页中公开您的 API 密钥:

<?php
    //the long url posted by your webpage
    $url = strip_tags($_POST["url"]);

    $api_user = "your_bitly_user_name";
    $api_key = "your_bitly_api_key";

    //send it to the bitly shorten webservice
    $ch = curl_init ("http://api.bitly.com/v3/shorten?login=$api_user&apiKey=$api_key&longUrl=$url&format=json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //the response is a JSON object, send it to your webpage
    echo curl_exec($ch);    
?>

然后在您的网页中,代码应该类似于:

    //the long url that you want to shorten
    var longUrl = escape(window.location.href)

    $.ajax({
        url : "php/getShortUrl.php",//this is the php script above
        dataType : "json",
        type : "POST",
        data : {
            url : longUrl
        },
        success : function(data) {
            if(data.status_txt === "OK"){
                shortUrl = data.data.url;
            }
        },
        error : function(xhr, error, message) {
            //no success, fallback to the long url
            shortUrl = longUrl
        }
    });

请参阅 bitly API 了解更多详情

You could also generate a short url with php and curl on the server, this way you don't have to expose your API key in the webpage:

<?php
    //the long url posted by your webpage
    $url = strip_tags($_POST["url"]);

    $api_user = "your_bitly_user_name";
    $api_key = "your_bitly_api_key";

    //send it to the bitly shorten webservice
    $ch = curl_init ("http://api.bitly.com/v3/shorten?login=$api_user&apiKey=$api_key&longUrl=$url&format=json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //the response is a JSON object, send it to your webpage
    echo curl_exec($ch);    
?>

Then in your webpage the code should be something like:

    //the long url that you want to shorten
    var longUrl = escape(window.location.href)

    $.ajax({
        url : "php/getShortUrl.php",//this is the php script above
        dataType : "json",
        type : "POST",
        data : {
            url : longUrl
        },
        success : function(data) {
            if(data.status_txt === "OK"){
                shortUrl = data.data.url;
            }
        },
        error : function(xhr, error, message) {
            //no success, fallback to the long url
            shortUrl = longUrl
        }
    });

See the bitly API for more details

挽袖吟 2024-08-19 03:38:15

我在寻找类似的东西时发现了你的帖子,最终只是编写了一个 jQuery 插件,它提供了(至少部分)你正在寻找的东西。

我在 Bitbucket 上的 jQuery Url Shortener

这是一个非常简单的插件;我不需要缩短用户的网址,因此在缩短之前我没有进行任何长度检查或网址测试,尽管我并不反对添加这些类型的功能。

只是觉得你可能会发现它有用。

至于识别文本框中的网址,我建议使用 匹配 url 的正则表达式

I found your post while looking for something similar and eventually just wrote a jQuery plugin that provides (at least part of) what you're looking for.

My jQuery Url Shortener on Bitbucket

It's a very simple plugin; I didn't need to shorten the user's urls so I don't have any length checking or url testing before shortening it, though I am not averse to adding those types of features.

Just thought you might find it useful.

As for Recognising URLs in your textbox, I would suggest using a RegEx to match the url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文