我使用以下代码(张贴在stackoverflow.com) unshorten url在谷歌应用程序脚本:
function ExpandURL(url) {
url = url.indexOf("https://") == 0 ? url : "https://" + url; // Added
var response = UrlFetchApp.fetch(url, { followRedirects: false });
var longurl = decodeURIComponent(response.getHeaders()['Location']);
return longurl;
}
但是当我使用它一段时间。URL:
function test() {
Logger.log( ExpandURL("OWLYURL"))
}
我在日志窗口中得到以下消息错误:
Exception: Address unavailable: OWLYURL
(请将OWLYURL替换为https://ow.ly/d9AV30jRJWJ(不加空格);因为我不能发布我的问题时,正文包含这样的纯URL)
同样的事情也发生在许多其他地方。我试过的所有url
有什么办法可以解决这个问题吗?
在您的问题中,使用https:// ow . ly /d9AV30jRJWJ
(由于避免了Stackoverflow的错误,因此包含空格)。但是,我想在这个例子中,它可能是http:// ow . ly /d9AV30jRJWJ
。因为当我访问https:// ow . ly /d9AV30jRJWJ
时,没有返回任何响应。另一方面,当我访问http:// ow . ly /d9AV30jRJWJ
时,返回博客页面。如果我的理解是正确的,那么下面的修改如何?
修改脚本:
function ExpandURL(url) {
url = /https?:///.test(url) ? url : "https://" + url; // Modified
var response = UrlFetchApp.fetch(url, { followRedirects: false });
var longurl = decodeURIComponent(response.getHeaders()['Location']);
return longurl;
}
function test() {
Logger.log(ExpandURL("http:// ow . ly /d9AV30jRJWJ")) // When you test this, please modify ` ow . ly ` to `ow.ly`.
}
- 当
test
运行时,返回https://blog.leadquizzes.com/ubersuggest-find-profitable-keywords-with-neil-patels-free-seo-tool?platform=hootsuite
。