(url: any)
| 64 | |
| 65 | // 根据浏览器 url.host 是获取获取主域名 |
| 66 | export function getMainDomain(url: any) { |
| 67 | try { |
| 68 | // 处理URL对象或字符串 |
| 69 | let hostname = ''; |
| 70 | |
| 71 | // 如果是URL字符串,提取hostname部分 |
| 72 | if (typeof url === 'string') { |
| 73 | // 移除协议部分 |
| 74 | const noProtocol = url.replace(/^(https?:\/\/)/, ''); |
| 75 | // 提取域名部分(移除路径和查询参数) |
| 76 | hostname = noProtocol.split('/')[0]; |
| 77 | } else if (url instanceof URL) { |
| 78 | hostname = url.hostname; |
| 79 | } else { |
| 80 | return ''; |
| 81 | } |
| 82 | |
| 83 | // 处理特殊情况: 将Twitter的旧域名和新域名统一处理 |
| 84 | if (hostname === 'twitter.com' || hostname === 'x.com' || |
| 85 | hostname === 'www.twitter.com' || hostname === 'www.x.com') { |
| 86 | return 'x.com'; |
| 87 | } |
| 88 | |
| 89 | // 移除可能的www前缀 |
| 90 | hostname = hostname.replace(/^www\./, ''); |
| 91 | |
| 92 | // 提取基本域名 |
| 93 | const parts = hostname.split('.'); |
| 94 | if (parts.length >= 2) { |
| 95 | // 对于常见的二级域名(如co.uk),需要特殊处理 |
| 96 | if (parts.length >= 3 && |
| 97 | ((parts[parts.length-2] === 'co' || parts[parts.length-2] === 'com') && |
| 98 | parts[parts.length-1].length === 2)) { |
| 99 | // 例如 example.co.uk 应该返回 example.co.uk |
| 100 | return parts.slice(-3).join('.'); |
| 101 | } else { |
| 102 | // 否则返回主域名和顶级域名 |
| 103 | return parts.slice(-2).join('.'); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return hostname; |
| 108 | } catch (error) { |
| 109 | console.error('getMainDomain error:', error); |
| 110 | return ''; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * 检查文本内容是否属于不应翻译的特殊内容 |
no outgoing calls
no test coverage detected