jQuery 动态 URL 缩短器 [英] jQuery on the fly URL shortener

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

问题描述

我正在寻找一种与 tweetdeck 的工作方式非常相似的动态 URL 缩短器.我发现了许多 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. 如果找到,则获取整个 URL(我如何确定结尾?可以是句点、逗号、空格等...)
  3. 确保该 URL 不是一个 bit.ly URL
  4. 验证 URL(发出请求并确保 http 响应没有错误,bit.ly 是否已经这样做了?)
  5. 如果有效,将 url 发送到 bit.ly 的 API 并获得响应
  6. 用文本区域中的短网址替换长网址.

想法?

推荐答案

以下是如何使用 Bitly API 和 jQuery 获取缩短的 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);
});

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

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