我使用以下代码(张贴在stackoverflow.com上)来取消Google中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;
}
但是,当我将它用于ow.ly 示例 URL时:
function test() {
Logger.log( ExpandURL("OWLYURL"))
}
我在日志窗口中得到以下消息错误:
Exception: Address unavailable: OWLYURL
(请将OWLYURL替换为https://ow.ly /d9AV30jRJWJ (没有空格),因为当正文包含这样简单的URL时,我无法发布我的问题)
对于我尝试过的许多其他ow.ly URL,也会发生同样的情况。
有没有办法解决这个问题?
发布于 2022-05-21 05:33:36
在您的问题中,使用了https:// ow . ly /d9AV30jRJWJ
(由于避免了堆栈溢出的错误,所以包含了空格。)但是,我认为在这种情况下,可能是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
。https://stackoverflow.com/questions/72326845
复制相似问题