jQuery即时URL缩短 [英] jQuery on the fly URL shortener

查看:363
本文介绍了jQuery即时URL缩短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种即时网址缩短器,就像tweetdeck的工作原理一样。我找到了许多jQuery和一般的javascript插件,它们接受一个url并在按下按钮时通过缩短服务(如bit.ly)运行它。但是,我还没能找到一个在飞行中做到这一点。我的第一个问题是这已经存在于某个地方吗?其次,如果没有,那么识别需要在文本框中缩短的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. 在该文本区域的onKeyUp上运行查找http的文本

  2. 如果找到抓取整个网址(如何确定结尾?可以是句号,逗号,空格等...)

  3. 确保网址不是bit.ly网址

  4. 验证URL(发出请求并确保http响应不是错误,bit.ly已经这样做了吗?)

  5. 如果有效,将网址发送到bit.ly的API并获得回复

  6. 将长网址替换为文本区域中的短网址。

  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.

想法?

推荐答案

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

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

这篇关于jQuery即时URL缩短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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