objective-c - Chrome 地址栏如何确定它是 URL 还是搜索字符串?

标签 objective-c google-chrome url

- (BOOL) validateUrl: (NSString *) candidate {
    NSString *urlRegEx =
    @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    if( [urlTest evaluateWithObject:candidate]
            ||[candidate containsString:@".com"]
            ||[candidate containsString:@".net"]
            ||[candidate containsString:@".org"]
            ||[candidate containsString:@".cn"]
            ||[candidate containsString:@".jp"]
           )
    {
       return TRUE;
    }

    return  FALSE;
}

这是一长串URL域名,“.com”、“.net”、“.org”等等。 人们不需要在前面或地址栏中输入“http”。

那么 Chrome 地址栏如何确定它是一个 URL 还是一个搜索字符串呢?

If I input "a.fa", it's not an URL.
"a a.com",it's a search string.
"a.mobi/aaa", it's an URL.

最佳答案

正如 funroll 提到的那样,通过 Chromium 找到答案是可能的——但至少根据我的测试,这是正在发生的事情的基本思路。

如果输入到“多功能框”的字符串遵循以下格式,则该字符串被确定为 URL:

[协议(protocol)][子域].[子域].[域名].[tld]

其中子域(当然是可选的)和域名都只包含字母(对于 Chrome,这似乎包括重音字母)、数字、空格和连字符,并且 TLD/顶级域来自批准列表—.com、.net 等—除非指定了协议(protocol),在这种情况下,任何 TLD 都被视为有效。协议(protocol)也来自一个集合列表,但可以是几乎任何格式,冒号跟在任意数量的斜杠之后。如果协议(protocol)不是设置列表的一部分,则整个 URL 将被视为搜索。

如果上述 URL 格式(例如 stackoverflow.com/)中的字符串后有斜杠,则后面的任何内容都有效。

或者,如果斜杠出现在字符串的开头,Chrome 也将其视为一个 URL(使用 file:// 协议(protocol))。


有效 URL 示例(根据 Chrome):

  • stackoverflow.com
  • abc.stackoverflow.com
  • abc.abc.abc.abc.stackoverflow.com
  • stáckoverflow.com (this changes the URL, but is allowed—try it!)
  • stack-overflow.com
  • -stackoverflow.com (might not even be a legal domain name, but it works)
  • 4stackoverflow.com
  • stackoverflow.com
  • stackoverflow.com/not valid characters !@#$^æ
  • [http]://stackoverflow.com (the brackets aren't legal, but I can't include the link otherwise)
  • [http]:////stackoverflow.com
  • [http]:stackoverflow.com
  • [http]:stackoverflow.mynewtld

无效 URL 示例:

  • stack overflow.com
  • stackoverflow*.com
  • stack/overflow.com
  • stackoverflow.mynewtld

And, well, just about everything else.


我们只希望在某处有一个图书馆来做这一切。

关于objective-c - Chrome 地址栏如何确定它是 URL 还是搜索字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24765294/

相关文章:

javascript - 尝试实现 flexbox 来填充网页的剩余高度,但在 chrome 中不起作用

wordpress - 将 .html 添加到 URL 末尾

objective-c - NSView 中的放置位置

objective-c - 仅使用 NSBundle 插入 Xib 的两个按钮。仍然显示TableView

ios - ios 9.0 无法添加谷歌登录

javascript - 如果 Google chrome 开发者工具已经打开,如何以编程方式关闭它?

objective-c - 如何在 cocoa 中显示一个弹出窗口,通知 NSTextField 不能为空?

javascript - 解决 Google Chrome 29 中 Canvas 崩溃的问题

url - Logstash:为文档创建 url 友好的 _id

apache - 用于 Apache 重写的 Nginx 重写 URL 替换