11/**
2+ * 获取 URL 的基础路径(包括协议、域名和路径部分,但不包括文件名)。
23 *
3- * @param urlString {String} "https://example.com/path/to/page.html"
4- * @return
4+ * @param { string } urlString - 完整的 URL 字符串,例如 "https://example.com/path/to/page.html"。
5+ * @return { string } - 返回 URL 的基础路径,例如 "https://example.com/path/to/"。
56 */
67function urlBasePath ( urlString ) {
78 const url = new URL ( urlString ) ;
9+ // 获取基础路径(包括协议、域名和路径部分,但不包括文件名)
810 const basePath = `${ url . origin } ${ url . pathname . substring ( 0 , url . pathname . lastIndexOf ( "/" ) + 1 ) } ` ;
911 // console.log(basePath); // 输出: https://example.com/path/to/
1012 return basePath ;
1113}
1214
1315/**
16+ * 将 script 的 src 格式化为完整的 URL。
1417 *
15- * 把script的src格式化,统一为url的形式
18+ * 支持以下格式的 script src:
19+ * 1. 完整的 URL(以 "http://" 或 "https://" 开头)。
20+ * 2. CDN URL(以 "//" 开头)。
21+ * 3. 相对路径(以 "./" 开头)。
22+ * 4. 省略域名的路径(以 "/" 开头)。
23+ * 5. 其他相对路径。
1624 *
17- * @param scriptSrc {String}
18- * @return
25+ * @param { string } scriptSrc - script 的 src 属性值。
26+ * @return { string } - 返回格式化后的完整 URL。
1927 */
2028function formatScriptSrcToUrl ( scriptSrc ) {
2129
22- // 强制requestUrl是一个字符串
23- // 比如可能会是一个TrustedScriptURL
30+ // 强制将 scriptSrc 转换为字符串(例如,处理 TrustedScriptURL 类型)
2431 scriptSrc = scriptSrc + "" ;
2532
26- // 正常格式直接返回
33+ // 如果已经是完整的 URL,直接返回
2734 if ( scriptSrc . startsWith ( "http://" ) || scriptSrc . startsWith ( "https://" ) ) {
2835 return scriptSrc ;
2936 }
3037
31- // 兼容CDN URL
38+ // 处理 CDN URL(以 "//" 开头)
3239 // 示例:"//statics.moonshot.cn/kimi-chat/shared-K0TvIN461soURJCs7nh6uxcQiCM_.04bc3959.async.js"
3340 if ( scriptSrc . startsWith ( "//" ) ) {
3441 return "https:" + scriptSrc ;
3542 }
3643
37- // 相对路径: "./js/login/log-utils1.1.js"
44+ // 处理相对路径(以 "./" 开头)
45+ // 示例:"./js/login/log-utils1.1.js"
3846 if ( scriptSrc . startsWith ( "./" ) ) {
3947 return urlBasePath ( window . location . href ) + scriptSrc . substring ( 2 , scriptSrc . length ) ;
4048 }
4149
42- // 兼容省略域名的情况
43- // 数据样例 :"/logos/2024/moon/december-r4/december.js"
50+ // 处理省略域名的路径(以 "/" 开头)
51+ // 示例 :"/logos/2024/moon/december-r4/december.js"
4452 if ( scriptSrc . startsWith ( "/" ) ) {
4553 return window . location . origin + scriptSrc ;
4654 }
4755
48- // 相对路径
49- // "static/js/chunk-19a101ae.45e69b5c.js"
56+ // 处理其他相对路径
57+ // 示例: "static/js/chunk-19a101ae.45e69b5c.js"
5058 return window . location . origin + "/" + scriptSrc ;
5159}
5260
5361module . exports = {
5462 urlBasePath,
5563 formatScriptSrcToUrl,
56- }
64+ } ;
0 commit comments