Suppose I have a list of domains that I want to block with front end technology. I want to block any URL that is hosted by the domain. On top of my head I have the following methods:
假设我有一个我想用前端技术阻止的域列表。我想阻止域托管的任何URL。在我的头脑中,我有以下方法:
- regex
string.split("/")
location.hostname
But what if the URL is a shortened one? e.g. https://goo.gl/lWyZ5B
. All of the above methods will return undesired result: goo.gl
.
但是,如果URL是缩短的,该怎么办?例如https://goo.gl/lWyZ5B。所有上述方法都会返回不需要的结果:goo.gl。
This is actually a part of a Chrome extension I am planning. So maybe Chrome can fetch the shortened URL then redirect to a warning page if the result is on block list?
这实际上是我计划的Chrome扩展程序的一部分。那么,如果结果在阻止列表中,Chrome可以获取缩短的URL,然后重定向到警告页面?
2 个解决方案
#1
The only way to know what the real host that is behind a shortened URL is to actually attempt to fetch the URL and then see if you get a 302 redirect and see what that redirect URL is.
了解缩短URL背后的真实主机的唯一方法是实际尝试获取URL,然后查看是否获得302重定向并查看重定向URL是什么。
Since you generally can't load cross-origin URLs from within browser Javascript, you would probably need a server to do this for you and return the result to you.
由于您通常无法从浏览器Javascript中加载跨源URL,因此您可能需要服务器为您执行此操作并将结果返回给您。
One other option is that if the link shortener is a known one that you have API access to, then you can query that API to see what a given shortened link is a shortcut for. For example, bit.ly has this api http://dev.bitly.com/data_apis.html. The API will typically allow cross origin access so you can access it from a browser.
另一个选项是,如果链接缩短程序是您具有API访问权限的已知链接,则可以查询该API以查看给定缩短链接的快捷方式。例如,bit.ly有这个api http://dev.bitly.com/data_apis.html。 API通常允许跨源访问,因此您可以从浏览器访问它。
Since you can't confidently have API access to all possible link shorteners, this strategy is really only applicable to specific link shorteners that you prepare for. A general solution for any link shortener would need to use a server to access the URL directly to get the redirect.
由于您无法自信地获得所有可能的链接缩短程序的API访问权限,因此该策略实际上仅适用于您准备的特定链接缩短程序。任何链接缩短器的一般解决方案都需要使用服务器直接访问URL以获取重定向。
For example, here's a simple node.js program that fetches a URL and checks to see if it returns a redirect response and gets the redirect URL if so.
例如,这是一个简单的node.js程序,它获取URL并检查它是否返回重定向响应并获取重定向URL(如果是这样)。
var request = require("request");
request({url: "https://goo.gl/lWyZ5B", followRedirect: false}, function(error, response, body) {
console.log(response.statusCode);
if (response.statusCode >= 300 && response.statusCode < 400) {
console.log(response.headers.location);
}
});
This generates this output:
这会生成此输出:
301
https://www.youtube.com/watch?v=ZrU_tt4R3xY
This could be wrapped in a server that would take a URL as a request and return the resulting URL.
这可以包装在服务器中,该服务器将URL作为请求并返回结果URL。
#2
It seems you would have to do an http call and view the response in order to accomplish this.
看来你必须做一个http调用并查看响应才能完成此任务。
#1
The only way to know what the real host that is behind a shortened URL is to actually attempt to fetch the URL and then see if you get a 302 redirect and see what that redirect URL is.
了解缩短URL背后的真实主机的唯一方法是实际尝试获取URL,然后查看是否获得302重定向并查看重定向URL是什么。
Since you generally can't load cross-origin URLs from within browser Javascript, you would probably need a server to do this for you and return the result to you.
由于您通常无法从浏览器Javascript中加载跨源URL,因此您可能需要服务器为您执行此操作并将结果返回给您。
One other option is that if the link shortener is a known one that you have API access to, then you can query that API to see what a given shortened link is a shortcut for. For example, bit.ly has this api http://dev.bitly.com/data_apis.html. The API will typically allow cross origin access so you can access it from a browser.
另一个选项是,如果链接缩短程序是您具有API访问权限的已知链接,则可以查询该API以查看给定缩短链接的快捷方式。例如,bit.ly有这个api http://dev.bitly.com/data_apis.html。 API通常允许跨源访问,因此您可以从浏览器访问它。
Since you can't confidently have API access to all possible link shorteners, this strategy is really only applicable to specific link shorteners that you prepare for. A general solution for any link shortener would need to use a server to access the URL directly to get the redirect.
由于您无法自信地获得所有可能的链接缩短程序的API访问权限,因此该策略实际上仅适用于您准备的特定链接缩短程序。任何链接缩短器的一般解决方案都需要使用服务器直接访问URL以获取重定向。
For example, here's a simple node.js program that fetches a URL and checks to see if it returns a redirect response and gets the redirect URL if so.
例如,这是一个简单的node.js程序,它获取URL并检查它是否返回重定向响应并获取重定向URL(如果是这样)。
var request = require("request");
request({url: "https://goo.gl/lWyZ5B", followRedirect: false}, function(error, response, body) {
console.log(response.statusCode);
if (response.statusCode >= 300 && response.statusCode < 400) {
console.log(response.headers.location);
}
});
This generates this output:
这会生成此输出:
301
https://www.youtube.com/watch?v=ZrU_tt4R3xY
This could be wrapped in a server that would take a URL as a request and return the resulting URL.
这可以包装在服务器中,该服务器将URL作为请求并返回结果URL。
#2
It seems you would have to do an http call and view the response in order to accomplish this.
看来你必须做一个http调用并查看响应才能完成此任务。