有没有办法通过 javascript 缩短 goo.gl 的 url
我想在我的网页上添加一个按钮,当我单击该按钮时,它会尝试找到 goo.gl 并输入当前网址。
我如何通过javascript使用goo.gl服务?
仅供参考:
我在这里找到了一些东西 http://www.labnol.org /internet/create-google-short-url/11748/ 但如何导入这个函数呢? var auth_token = getUrlShorteningRequestParams(url);
function shortify(url)
{
var auth_token = getUrlShorteningRequestParams(url);
var urlEscaped = escape(url).replace(/\+/g,"%2B");
xmlhttp.open("POST", "http://goo.gl/api/url?
[email protected]&url=" + urlEscaped
+ "&auth_token=" + auth_token, false);
xmlhttp.onload = xmlhttpLoad;
xmlhttp.send(null);
}
仅供参考,再次
I want to add a button at my webpage, when I click the button it will try to locate goo.gl and input the current url.
How could I use goo.gl service by javascript?
FYI:
I found somthing at here http://www.labnol.org/internet/create-google-short-url/11748/
but how to import this function?
var auth_token = getUrlShorteningRequestParams(url);
function shortify(url)
{
var auth_token = getUrlShorteningRequestParams(url);
var urlEscaped = escape(url).replace(/\+/g,"%2B");
xmlhttp.open("POST", "http://goo.gl/api/url?
[email protected]&url=" + urlEscaped
+ "&auth_token=" + auth_token, false);
xmlhttp.onload = xmlhttpLoad;
xmlhttp.send(null);
}
FYI again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
goo.gl 的 API 需要向
http://goo.gl/api/shorten?url=URL
发出POST
请求,这意味着您无法在纯内容中执行此操作(在常规网页中)JavaScript。我开发了一个使用 JSONP 支持此功能的 服务。然而,它已成为自身成功的受害者,现在经常超出 App Engine 配额。
编辑:您发布的 JavaScript 代码来自 Chrome 扩展程序。它可以在特权浏览器扩展中完成;我写了一个类似的 Firefox 扩展。
PHP API 看起来也不错。但是,有一个不需要令牌的新 API (
/shorten
)。另外,它似乎没有与 JavaScript 一起使用的 JSONP 版本。goo.gl's API requires a
POST
request tohttp://goo.gl/api/shorten?url=URL
, which means you can't do it in pure content (in a regular web page) JavaScript.I developed a service that supports this using JSONP. However, it's become a victim of its own success, and now frequently exceeds the App Engine quota.
EDIT: The JavaScript code you've posted is from a Chrome extension. It can be done in privileged browser extensions; I wrote a similar Firefox extension.
The PHP API also seems fine. However, there is a new API (
/shorten
) that doesn't require the token. Also, it doesn't seem to have a JSONP version for use with JavaScript.您可以使用 bit.ly 服务在客户端创建短 URL:
jQuery on飞行网址缩短器
You can create short URLs on client side using bit.ly service:
jQuery on the fly URL shortener