<%+cbi/valueheader%>
<%
local map = self.map
local ss_type = map:get("@server_subscribe[0]", "ss_type")
local xray_hy2_type = map:get("@server_subscribe[0]", "xray_hy2_type")
-%>
<script type="text/javascript">
//<![CDATA[
let ss_type = "<%=ss_type%>"
let xray_hy2_type = "<%=xray_hy2_type%>"

function padright(str, cnt, pad) {
	return str + Array(cnt + 1).join(pad);
}

function b64EncodeUnicode(str) {
	return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
		return String.fromCharCode('0x' + p1);
	}));
}

function b64encutf8safe(str) {
	return b64EncodeUnicode(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, '');
}

function b64DecodeUnicode(str) {
	return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
		return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
	}).join(''));
}

function b64decutf8safe(str) {
	var l;
	str = str.replace(/-/g, "+").replace(/_/g, "/");
	l = str.length;
	l = (4 - l % 4) % 4;
	if (l) str = padright(str, l, "=");
	return b64DecodeUnicode(str);
}

function b64encsafe(str) {
	return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, '')
}

function b64decsafe(str) {
	var l;
	str = str.replace(/-/g, "+").replace(/_/g, "/");
	l = str.length;
	l = (4 - l % 4) % 4;
	if (l) str = padright(str, l, "=");
	return atob(str);
}

function dictvalue(d, key) {
	var v = d[key];
	if (typeof (v) == 'undefined' || v == '') return '';
	return b64decsafe(v);
}

function export_ssr_url(btn, urlname, sid) {
	var s = document.getElementById(urlname + '-status');
	if (!s) return false;
	var v_server = document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0];
	var v_port = document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0];
	var v_protocol = document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol')[0];
	var v_method = document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method')[0];
	var v_obfs = document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs')[0];
	var v_password = document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0];
	var v_obfs_param = document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs_param')[0];
	var v_protocol_param = document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol_param')[0];
	var v_alias = document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0];
	var ssr_str = v_server.value + ":" + v_port.value + ":" + v_protocol.value + ":" + v_method.value + ":" + v_obfs.value + ":" + b64encsafe(v_password.value) + "/?obfsparam=" + b64encsafe(v_obfs_param.value) + "&protoparam=" + b64encsafe(v_protocol_param.value) + "&remarks=" + b64encutf8safe(v_alias.value);
	var textarea = document.createElement("textarea");
	textarea.textContent = "ssr://" + b64encsafe(ssr_str);
	textarea.style.position = "fixed";
	document.body.appendChild(textarea);
	textarea.select();
	try {
		document.execCommand("copy"); // Security exception may be thrown by some browsers.
		s.innerHTML = "<font style=\'color:green\'><%:Copy SSR to clipboard successfully.%></font>";
	} catch (ex) {
		s.innerHTML = "<font style=\'color:red\'><%:Unable to copy SSR to clipboard.%></font>";
	} finally {
		document.body.removeChild(textarea);
	}
	return false;
}

function import_ssr_url(btn, urlname, sid) {
		var s = document.getElementById(urlname + '-status');
		if (!s) return false;
		var ssrurl = prompt("<%:Paste sharing link here%>", "");
		if (ssrurl == null || ssrurl == "") {
			s.innerHTML = "<font style=\'color:red\'><%:User cancelled.%></font>";
			return false;
		}
		s.innerHTML = "";
		//var ssu = ssrurl.match(/ssr:\/\/([A-Za-z0-9_-]+)/i);
		ssrurl = ssrurl.replace(/&([a-zA-Z]+);/g, '&').replace(/\s*#\s*/, '#').trim();  //一些奇葩的链接用"&amp;"当做"&"，"#"前后带空格
		var ssu = ssrurl.split('://');
		//console.log(ssu.length);
		var event = document.createEvent("HTMLEvents");
		event.initEvent("change", true, true);
		switch (ssu[0]) {
		case "hysteria2":
		case "hy2":
			try {
				var url = new URL("http://" + ssu[1]);
				var params = url.searchParams;
			} catch(e) {
				alert(e);
				return false;
			}

			if (xray_hy2_type === "hysteria2") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = (ssu[0] === "hy2") ? "hysteria2" : ssu[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);

				if (params.get("protocol")) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_transport')[0].checked = true; // 设置 flag_transport 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_transport')[0].dispatchEvent(event); // 触发事件

				document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport_protocol')[0].value = params.get("protocol") || "udp";
				}

				if (params.get("pinSHA256")) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.pinsha256')[0].value = params.get("pinSHA256") || "";
				}
			} else {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "v2ray"
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = (ssu[0] === "hy2") ? "hysteria2" : ssu[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = url.hostname;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = url.port || "443";
			if (params.get("lazy") === "1") { 
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.lazy_mode')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.lazy_mode')[0].dispatchEvent(event);
			}
			if (params.get("mport")) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_port_hopping')[0].checked = true; // 设置 flag_port_hopping 为 true
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_port_hopping')[0].dispatchEvent(event); // 触发事件

			document.getElementsByName('cbid.shadowsocksr.' + sid + '.port_range')[0].value = params.get("mport") || "";
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.hy2_auth')[0].value = decodeURIComponent(url.username);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.hy2_auth')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.uplink_capacity')[0].value =
				(params.get("upmbps") && params.get("upmbps").match(/\d+/)) ? params.get("upmbps").match(/\d+/)[0] : "";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.downlink_capacity')[0].value =
				(params.get("downmbps") && params.get("downmbps").match(/\d+/)) ? params.get("downmbps").match(/\d+/)[0] : "";
			if (params.get("obfs") && params.get("obfs") !== "none") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_obfs')[0].checked = true; // 设置 flag_obfs 为 true
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.flag_obfs')[0].dispatchEvent(event); // 触发事件

			document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs_type')[0].value = params.get("obfs");
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.salamander')[0].value = params.get("obfs-password") || params.get("obfs_password");
			}
			if (params.get("security") === "tls" || params.get("sni") || params.get("alpn")) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true; // 设置 flag_obfs 为 true
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event); // 触发事件
				if (params.get("sni")) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = params.get("sni") || "";
				}
				if (params.get("alpn")) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_alpn')[0].value = params.get("alpn") || "";
				}
			}
			if (params.get("insecure") === "1") { 
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].dispatchEvent(event);
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";

			s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		case "ss":
			var url0 = (ssu[1] || "");
			var param = "";

			// 先分离 #（alias）
			var hashIndex = url0.indexOf("#");
			if (hashIndex >= 0) {
				param = url0.substring(hashIndex + 1);
				url0 = url0.substring(0, hashIndex);
			}

			// 再分离 ? 或 /?（参数）
			var queryIndex = (url0 = url0.replace('/?', '?')).indexOf("?");
			var query = "";
			if (queryIndex >= 0) {
				query = url0.substring(queryIndex + 1);
				url0 = url0.substring(0, queryIndex);
			}

			var params = Object.fromEntries(new URLSearchParams(query));

			if ( ! params.type) {
				// 普通 SS 导入逻辑
				// 判断是否 SIP002 格式（即含 @）
				if (url0.indexOf("@") !== -1) {
					// === SIP002 格式 ===
					var sipIndex = url0.indexOf("@");
					// 先 URL 解码 base64 再解码
					var userInfoB64 = decodeURIComponent(url0.substring(0, sipIndex));
					var userInfo = b64decsafe(userInfoB64);
					var userInfoSplitIndex = userInfo.indexOf(":");
					if(userInfoSplitIndex < 0) {
						// 格式错误
						s.innerHTML = "<font style='color:red'><%:Userinfo format error.%></font>";
						break;
					}
					var method = userInfo.substring(0, userInfoSplitIndex);
					var password = userInfo.substring(userInfoSplitIndex + 1);
					var serverPart = url0.substring(sipIndex + 1);
					var serverInfo = serverPart.split(":");

					var server = serverInfo[0];
					var port = serverInfo[1];

					var plugin = "", pluginOpts = "";
					if (params.plugin) {
						var pluginParams = decodeURIComponent(params.plugin).split(";");
						plugin = pluginParams.shift();
						pluginOpts = pluginParams.join(";");
					}
				} else {
					// === Base64 SS2022 / 普通格式 的整体编码格式 ===
					// 先 URL 解码整个字符串
					var decodedUrl0 = decodeURIComponent(url0);
					var sstr = b64decsafe(decodedUrl0);
					if (!sstr) {
						s.innerHTML = "<font style='color:red'><%:Base64 sstr failed.%></font>";
						break;
					}

					// 支持 SS2022 / 普通格式
					var regex2022 = /^([^:]+):([^:]+):([^@]+)@([^:]+):(\d+)$/;
					var regexNormal = /^([^:]+):([^@]+)@([^:]+):(\d+)$/;

					var m2022 = sstr.match(regex2022);
					var mNormal = sstr.match(regexNormal);

					if (m2022) {
						var method = m2022[1];
						var password = m2022[2] + ":" + m2022[3];
						var server = m2022[4];
						var port = m2022[5];
					} else if (mNormal) {
						var method = mNormal[1];
						var password = mNormal[2];
						var server = mNormal[3];
						var port = mNormal[4];
					} else {
						s.innerHTML = "<font style='color:red'><%:SS URL base64 sstr format not recognized.%></font>";
						break;
					}

					var plugin = "", pluginOpts = "";
					if (params["shadow-tls"]) {
						try {
							var decoded_tls = JSON.parse(atob(decodeURIComponent(params["shadow-tls"])));
							plugin = "shadow-tls";
							var versionFlag = "";
							if (decoded_tls.version && !isNaN(decoded_tls.version)) {
                				versionFlag = "v" + decoded_tls.version + "=1;";
							}
							pluginOpts = versionFlag + "host=" + (decoded_tls.host || "") + ";passwd=" + (decoded_tls.password || "");
						} catch (e) {
							console.log("shadow-tls decode failed:", e);
						}
					}
				}

				// 判断密码是否经过url编码
				const isURLEncodedPassword = function(pwd) {
					if (!/%[0-9A-Fa-f]{2}/.test(pwd)) return false;
					try {
						const decoded = decodeURIComponent(pwd.replace(/\+/g, "%20"));
						const reencoded = encodeURIComponent(decoded);
						return reencoded === pwd;
					} catch (e) {
						return false;
					}
				}
				if (isURLEncodedPassword(password)) {
					password = decodeURIComponent(password); // 解码URL编码
				} else {
					password = password; // 保持原始值
				}

				// === 填充配置项 ===
				var has_ss_type = (ss_type === "ss-rust") ? "ss-rust" : "ss-libev";

				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.has_ss_type')[0].value = has_ss_type;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.has_ss_type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = server;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = port;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = password || "";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method_ss')[0].value = method;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method_ss')[0].dispatchEvent(event);

				if (plugin && plugin !== "none") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_plugin')[0].checked = true; // 设置 enable_plugin 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_plugin')[0].dispatchEvent(event); // 触发事件
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin')[0].value = plugin;
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin')[0].dispatchEvent(event);
					if (plugin !== undefined) {
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.plugin_opts')[0].value = pluginOpts || "";
					}
				} else {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_plugin')[0].checked = false;
				}

				if (param != undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = decodeURIComponent(param);
				}
				s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
				return false;
			} else {			
				try {
					// Xray SS 导入逻辑
					// 拆分 @，判断是否是 base64 userinfo 的格式
					var parts = url0.split("@");
					if (parts.length > 1) {
						// @ 前是 base64(method:password)，后面是 server:port?params
						var userinfo = b64decsafe(parts[0]);
						var sepIndex = userinfo.indexOf(":");
						if (sepIndex > -1) {
							method = userinfo.slice(0, sepIndex);
							password = userinfo.slice(sepIndex + 1);  //一些链接用明文uuid做密码
						}
					}
					var url = new URL("http://" + url0 + (param ? "#" + encodeURIComponent(param) : ""));

				} catch(e) {
					alert(e);
					return false;
				}
				// Check if the elements exist before trying to modify them
				function setElementValue(name, value) {
					const element = document.getElementsByName(name)[0];
					if (element) {
						if (typeof value === 'boolean') {
							element.checked = value;
						} else {
							element.value = value;
						}
					}
				}
				function dispatchEventIfExists(name, event) {
					const element = document.getElementsByName(name)[0];
					if (element) {
						element.dispatchEvent(event);
					}
				}
				setElementValue('cbid.shadowsocksr.' + sid + '.alias', url.hash ? decodeURIComponent(url.hash.slice(1)) : "");
				setElementValue('cbid.shadowsocksr.' + sid + '.type', "v2ray");
				dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.type', event);
				setElementValue('cbid.shadowsocksr.' + sid + '.v2ray_protocol', "shadowsocks");
				dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.v2ray_protocol', event);
				setElementValue('cbid.shadowsocksr.' + sid + '.server', url.hostname);
				setElementValue('cbid.shadowsocksr.' + sid + '.server_port', url.port || "80");
				setElementValue('cbid.shadowsocksr.' + sid + '.password', password || url.username);
				setElementValue('cbid.shadowsocksr.' + sid + '.transport', 
					params.type === "http" ? "h2" : 
					(["xhttp", "splithttp"].includes(params.type) ? "xhttp" :
					(["tcp", "raw"].includes(params.type) ? "raw" : 
					(params.type || "raw")))
				);
				dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.transport', event);
				setElementValue('cbid.shadowsocksr.' + sid + '.encrypt_method_ss', method || params.encryption || "none");
				if ([ "tls", "xtls", "reality" ].includes(params.security)) {
					setElementValue('cbid.shadowsocksr.' + sid + '.' + params.security, true);
					dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.' + params.security, event);

					if (params.security === "tls") {
						if (params.ech && params.ech.trim() !== "") {
							setElementValue('cbid.shadowsocksr.' + sid + '.enable_ech', true); // 设置 enable_ech 为 true
							dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_ech', event); // 触发事件
							setElementValue('cbid.shadowsocksr.' + sid + '.ech_config', params.ech || "");
						}
						if (params.allowInsecure === "1") {
							setElementValue('cbid.shadowsocksr.' + sid + '.insecure', true); // 设置 insecure 为 true
							dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.insecure', event); // 触发事件
						}
					}
					if (params.security === "reality") {
						setElementValue('cbid.shadowsocksr.' + sid + '.reality_publickey', params.pbk ? decodeURIComponent(params.pbk) : "");
						setElementValue('cbid.shadowsocksr.' + sid + '.reality_shortid', params.sid || "");
						setElementValue('cbid.shadowsocksr.' + sid + '.reality_spiderx', params.spx ? decodeURIComponent(params.spx) : "");
						if (params.pqv && params.pqv.trim() !== "") {
							setElementValue('cbid.shadowsocksr.' + sid + '.enable_mldsa65verify', true); // 设置 enable_mldsa65verify 为 true
							dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_mldsa65verify', event); // 触发事件
							setElementValue('cbid.shadowsocksr.' + sid + '.reality_mldsa65verify', params.pqv || "");
						}
					}
					setElementValue('cbid.shadowsocksr.' + sid + '.tls_flow', params.flow || "none");
					dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.tls_flow', event);

					setElementValue('cbid.shadowsocksr.' + sid + '.tls_alpn', params.alpn || "");
					setElementValue('cbid.shadowsocksr.' + sid + '.fingerprint', params.fp || "");
					setElementValue('cbid.shadowsocksr.' + sid + '.tls_host', params.sni || "");
				}
				switch (params.type) {
				case "ws":
					if (params.security !== "tls") {
						setElementValue('cbid.shadowsocksr.' + sid + '.ws_host', params.host ? decodeURIComponent(params.host) : "");
					}
					setElementValue('cbid.shadowsocksr.' + sid + '.ws_path', params.path ? decodeURIComponent(params.path) : "/");
					break;
				case "httpupgrade":
					if (params.security !== "tls") {
						setElementValue('cbid.shadowsocksr.' + sid + '.httpupgrade_host', params.host ? decodeURIComponent(params.host) : "");
					}
					setElementValue('cbid.shadowsocksr.' + sid + '.httpupgrade_path', params.path ? decodeURIComponent(params.path) : "/");
					break;
				case "xhttp":
				case "splithttp":
					if (params.security !== "tls") {
						setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_host', params.host ? decodeURIComponent(params.host) : "");
					}
					setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_mode', params.mode || "auto");
					setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_path', params.path ? decodeURIComponent(params.path) : "/");
					if (params.extra && params.extra.trim() !== "") {
						setElementValue('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra', true); // 设置 enable_xhttp_extra 为 true
						dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra', event); // 触发事件
						setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_extra', params.extra || "");
					}
					break;
				case "kcp":
					setElementValue('cbid.shadowsocksr.' + sid + '.kcp_guise', params.headerType || "none");
					setElementValue('cbid.shadowsocksr.' + sid + '.seed', params.seed || "");
					break;
				case "http":
				/* this is non-standard, bullshit */
				case "h2":
					setElementValue('cbid.shadowsocksr.' + sid + '.h2_host', params.host ? decodeURIComponent(params.host) : "");
					setElementValue('cbid.shadowsocksr.' + sid + '.h2_path', params.path ? decodeURIComponent(params.path) : "");
					break;
				case "quic":
					setElementValue('cbid.shadowsocksr.' + sid + '.quic_guise', params.headerType || "none");
					setElementValue('cbid.shadowsocksr.' + sid + '.quic_security', params.quicSecurity || "none");
					setElementValue('cbid.shadowsocksr.' + sid + '.quic_key', params.key || "");
					break;
				case "grpc":
					setElementValue('cbid.shadowsocksr.' + sid + '.serviceName', params.serviceName || "");
					setElementValue('cbid.shadowsocksr.' + sid + '.grpc_mode', params.mode || "gun");
					break;
				case "tcp":
				case "raw":
					setElementValue('cbid.shadowsocksr.' + sid + '.tcp_guise', params.headerType || "none");
					dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.tcp_guise', event);
					if (params.headerType === "http") {
						setElementValue('cbid.shadowsocksr.' + sid + '.http_host', params.host ? decodeURIComponent(params.host) : "");
						setElementValue('cbid.shadowsocksr.' + sid + '.http_path', params.path ? decodeURIComponent(params.path) : "");
					}
					break;
				}
				s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
				return false;
			}
		case "ssr":
			var sstr = b64decsafe((ssu[1] || "").replace(/#.*/, "").trim());
			var ploc = sstr.indexOf("/?");
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = url0.match(/^(.+):([^:]+):([^:]*):([^:]+):([^:]*):([^:]+)/);
			if (!ssm || ssm.length < 7) return false;
			var pdict = {};
			if (param.length > 2) {
				var a = param.split('&');
				for (var i = 0; i < a.length; i++) {
					var b = a[i].split('=');
					pdict[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
				}
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = ssm[1];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = ssm[2];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol')[0].value = ssm[3];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.encrypt_method')[0].value = ssm[4];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs')[0].value = ssm[5];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = b64decsafe(ssm[6]);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.obfs_param')[0].value = dictvalue(pdict, 'obfsparam');
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.protocol_param')[0].value = dictvalue(pdict, 'protoparam');
			var rem = pdict['remarks'];
			if (typeof (rem) != 'undefined' && rem != '' && rem.length > 0) document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = b64decutf8safe(rem);
			s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		case "trojan":
			try {
				var url = new URL("http://" + ssu[1]);
				var params = url.searchParams;
			} catch(e) {
				alert(e);
				return false;
			}

			if (!params.get("type")) {
				// 普通 Trojan 导入逻辑
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "trojan";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = url.hostname;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = url.port || "80";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = decodeURIComponent(url.username);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = params.get("peer") || params.get("sni");
				if (params.get("allowInsecure") === "1") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].checked = true; // 设置 insecure 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].dispatchEvent(event); // 触发事件
				}
				if (params.get("tfo") === "1") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.fast_open')[0].checked = true; // 设置 fast_open 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.fast_open')[0].dispatchEvent(event); // 触发事件
				}

				s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
				return false;
			} else {
				// Xray Trojan 导入逻辑
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = url.hash ? decodeURIComponent(url.hash.slice(1)) : "";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "v2ray";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = "trojan";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = url.hostname;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = url.port || "80";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.password')[0].value = decodeURIComponent(url.username);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = params.get("sni");
				if (params.get("allowInsecure") === "1") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].checked = true; // 设置 insecure 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].dispatchEvent(event); // 触发事件
				}
				if (params.get("ech") && params.get("ech").trim() !== "") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_ech')[0].checked = true; // 设置 enable_ech 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_ech')[0].dispatchEvent(event); // 触发事件
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.ech_config')[0].value = params.get("ech");
				}
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].value = 
					params.get("type") == "http" ? "h2" : 
					(["xhttp", "splithttp"].includes(params.get("type")) ? "xhttp" : 
					(["tcp", "raw"].includes(params.get("type")) ? "raw" : 
					(params.get("type") || "raw")));
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].dispatchEvent(event);
				if (params.get("security") === "tls") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_alpn')[0].value = params.get("alpn") || "";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.fingerprint')[0].value = params.get("fp") || "";
				}
				switch (params.get("type")) {
				case "ws":
					if (params.get("security") !== "tls") {
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
					}
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "/";
					break;
				case "httpupgrade":
					if (params.get("security") !== "tls") {
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.httpupgrade_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
					}
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.httpupgrade_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "/";
					break;
				case "xhttp":
				case "splithttp":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_mode')[0].value = params.get("mode") || "auto";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "/";
					if (params.get("extra") && params.get("extra").trim() !== "") {
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra')[0].checked = true; // 设置 enable_xhttp_extra 为 true
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra')[0].dispatchEvent(event); // 触发事件
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_extra')[0].value = params.get("extra") || "";
					}
					break;
				case "kcp":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.kcp_guise')[0].value = params.get("headerType") || "none";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.seed')[0].value = params.get("seed") || "";
					break;
				case "http":
				/* this is non-standard, bullshit */
				case "h2":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "";
					break;
				case "quic":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_guise')[0].value = params.get("headerType") || "none";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_security')[0].value = params.get("quicSecurity") || "none";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_key')[0].value = params.get("key") || "";
					break;
				case "grpc":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.serviceName')[0].value = params.get("serviceName") || "";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.grpc_mode')[0].value = params.get("mode") || "gun";
					break;
				case "raw":
				case "tcp":
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].value = params.get("headerType") || "none";
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].dispatchEvent(event);
					if (params.get("headerType") === "http") {
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_host')[0].value = params.get("host") ? decodeURIComponent(params.get("host")) : "";
						document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_path')[0].value = params.get("path") ? decodeURIComponent(params.get("path")) : "";
					}
					break;
				}

				s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
				return false;
			}
		case "vmess":
			var sstr = b64DecodeUnicode((ssu[1] || "").replace(/#.*/, "").trim());
			var ploc = sstr.indexOf("/?");
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = "v2ray";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].value = "vmess";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.v2ray_protocol')[0].dispatchEvent(event);
			var url0, param = "";
			if (ploc > 0) {
				url0 = sstr.substr(0, ploc);
				param = sstr.substr(ploc + 2);
			}
			var ssm = JSON.parse(sstr);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = ssm.ps;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = ssm.add;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = ssm.port;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.alter_id')[0].value = ssm.aid || "0";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.vmess_id')[0].value = ssm.id;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].value = 
				(["xhttp", "splithttp"].includes(ssm.net) ? "xhttp" : 
				(["tcp", "raw"].includes(ssm.net) ? "raw" : 
				(ssm.net || "raw")));
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.transport')[0].dispatchEvent(event);
			if (ssm.net === "raw" || ssm.net === "tcp") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].value = ssm.type;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tcp_guise')[0].dispatchEvent(event);
				if (ssm.type === "http") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_host')[0].value = ssm.host;
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.http_path')[0].value = ssm.path;
				}
			}
			if (ssm.net == "ws") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.ws_path')[0].value = ssm.path;
			}
			if (ssm.net == "httpupgrade") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.httpupgrade_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.httpupgrade_path')[0].value = ssm.path;
			}
			if (ssm.net == "xhttp" || ssm.net == "splithttp") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_mode')[0].value = ssm.mode || "auto";
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_path')[0].value = ssm.path;
				if (ssm.extra !== "" && ssm.extra !== undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra')[0].checked = true; // 设置 enable_xhttp_extra 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra')[0].dispatchEvent(event); // 触发事件
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.xhttp_extra')[0].value = ssm.extra;
				}
			}
			if (ssm.net == "h2") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_host')[0].value = ssm.host;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.h2_path')[0].value = ssm.path;
			}
			if (ssm.net == "quic") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_security')[0].value = ssm.securty;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.quic_key')[0].value = ssm.key;
			}
			if (ssm.net == "kcp") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.kcp_guise')[0].value = ssm.type;
			}
			if (ssm.tls == "tls") {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls')[0].dispatchEvent(event);
				if (ssm.fp !== "" && ssm.fp !== undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.fingerprint')[0].value = ssm.fp;
				}
				if (ssm.alpn !== "" && ssm.alpn !== undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_alpn')[0].value = ssm.alpn;
				}
				if (ssm.host !== "" && ssm.host !== undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.tls_host')[0].value = ssm.sni || ssm.host;
				}
				if (ssm.ech !== "" && ssm.ech !== undefined) {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_ech')[0].checked = true; // 设置 enable_ech 为 true
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.enable_ech')[0].dispatchEvent(event); // 触发事件
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.ech_config')[0].value = ssm.ech;
				}
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].checked =
					!!(ssm.allowInsecure ?? ssm.allowlnsecure ?? ssm['skip-cert-verify']); // 设置 insecure 为 true
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].dispatchEvent(event); // 触发事件
			}
			if (ssm.mux !== undefined) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.mux')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.mux')[0].dispatchEvent(event);
			}
			s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		case "vless":
			try {
				var url = new URL("http://" + ssu[1]);
				var params = url.searchParams;
			} catch(e) {
				alert(e);
				return false;
			}
			// Check if the elements exist before trying to modify them
			function setElementValue(name, value) {
				const element = document.getElementsByName(name)[0];
				if (element) {
					if (element.type === "checkbox" || element.type === "radio") {
						element.checked = value === true;
					} else {
						element.value = value;
					}
				}
			}
			function dispatchEventIfExists(name, event) {
				const element = document.getElementsByName(name)[0];
				if (element) {
					element.dispatchEvent(event);
				}
			}
			var server = url.hostname;
			/* 如果带 []，再去掉 [] */
			if (server[0] === "[" && server[server.length - 1] === "]") {
				server = server.slice(1, -1);
			}
			setElementValue('cbid.shadowsocksr.' + sid + '.alias', url.hash ? decodeURIComponent(url.hash.slice(1)) : "");
			setElementValue('cbid.shadowsocksr.' + sid + '.type', "v2ray");
			dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.type', event);
			setElementValue('cbid.shadowsocksr.' + sid + '.v2ray_protocol', "vless");
			dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.v2ray_protocol', event);
			setElementValue('cbid.shadowsocksr.' + sid + '.server', server);
			setElementValue('cbid.shadowsocksr.' + sid + '.server_port', url.port || "80");
			setElementValue('cbid.shadowsocksr.' + sid + '.vmess_id', url.username);
			setElementValue('cbid.shadowsocksr.' + sid + '.transport', 
				params.get("type") === "http" ? "h2" : 
				(["xhttp", "splithttp"].includes(params.get("type")) ? "xhttp" :
				(["tcp", "raw"].includes(params.get("type")) ? "raw" : 
				(params.get("type") || "raw")))
			);
			dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.transport', event);
			setElementValue('cbid.shadowsocksr.' + sid + '.vless_encryption', params.get("encryption") || "none");
			setElementValue('cbid.shadowsocksr.' + sid + '.tls_flow', params.get("flow") || "none");
			dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.tls_flow', event);
			if ([ "tls", "xtls", "reality" ].includes(params.get("security"))) {
				setElementValue('cbid.shadowsocksr.' + sid + '.' + params.get("security"), true);
				dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.' + params.get("security"), event);

				if (params.get("allowInsecure") === "1") {
					setElementValue('cbid.shadowsocksr.' + sid + '.insecure', true); // 设置 insecure 为 true
					dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.insecure', event); // 触发事件
				}

				if (params.get("security") === "tls") {
					if (params.get("ech") && params.get("ech").trim() !== "") {
						setElementValue('cbid.shadowsocksr.' + sid + '.enable_ech', true); // 设置 enable_ech 为 true
						dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_ech', event); // 触发事件
						setElementValue('cbid.shadowsocksr.' + sid + '.ech_config', params.get("ech") || "");
					}
				}
				if (params.get("security") === "reality") {
					setElementValue('cbid.shadowsocksr.' + sid + '.reality_publickey', params.get("pbk") ? decodeURIComponent(params.get("pbk")) : "");
					setElementValue('cbid.shadowsocksr.' + sid + '.reality_shortid', params.get("sid") || "");
					setElementValue('cbid.shadowsocksr.' + sid + '.reality_spiderx', params.get("spx") ? decodeURIComponent(params.get("spx")) : "");
					if (params.get("pqv") && params.get("pqv").trim() !== "") {
						setElementValue('cbid.shadowsocksr.' + sid + '.enable_mldsa65verify', true); // 设置 enable_mldsa65verify 为 true
						dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_mldsa65verify', event); // 触发事件
						setElementValue('cbid.shadowsocksr.' + sid + '.reality_mldsa65verify', params.get("pqv") || "");
					}
				}

				setElementValue('cbid.shadowsocksr.' + sid + '.tls_alpn', params.get("alpn") || "");
				setElementValue('cbid.shadowsocksr.' + sid + '.fingerprint', params.get("fp") || "");
				setElementValue('cbid.shadowsocksr.' + sid + '.tls_host', params.get("sni") || "");
			}
			switch (params.get("type")) {
			case "ws":
				if (params.get("security") !== "tls") {
					setElementValue('cbid.shadowsocksr.' + sid + '.ws_host', params.get("host") ? decodeURIComponent(params.get("host")) : "");
				}
				setElementValue('cbid.shadowsocksr.' + sid + '.ws_path', params.get("path") ? decodeURIComponent(params.get("path")) : "/");
				break;
			case "httpupgrade":
				if (params.get("security") !== "tls") {
					setElementValue('cbid.shadowsocksr.' + sid + '.httpupgrade_host', params.get("host") ? decodeURIComponent(params.get("host")) : "");
				}
				setElementValue('cbid.shadowsocksr.' + sid + '.httpupgrade_path', params.get("path") ? decodeURIComponent(params.get("path")) : "/");
				break;
			case "xhttp":
			case "splithttp":
				if (params.get("security") !== "tls") {
					setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_host', params.get("host") ? decodeURIComponent(params.get("host")) : "");
				}
				setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_mode', params.get("mode") || "auto");
				setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_path', params.get("path") ? decodeURIComponent(params.get("path")) : "/");
				if (params.get("extra") && params.get("extra").trim() !== "") {
					setElementValue('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra', true); // 设置 enable_xhttp_extra 为 true
					dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.enable_xhttp_extra', event); // 触发事件
					setElementValue('cbid.shadowsocksr.' + sid + '.xhttp_extra', params.get("extra") || "");
				}
				break;
			case "kcp":
				setElementValue('cbid.shadowsocksr.' + sid + '.kcp_guise', params.get("headerType") || "none");
				setElementValue('cbid.shadowsocksr.' + sid + '.seed', params.get("seed") || "");
				break;
			case "http":
			/* this is non-standard, bullshit */
			case "h2":
				setElementValue('cbid.shadowsocksr.' + sid + '.h2_host', params.get("host") ? decodeURIComponent(params.get("host")) : "");
				setElementValue('cbid.shadowsocksr.' + sid + '.h2_path', params.get("path") ? decodeURIComponent(params.get("path")) : "");
				break;
			case "quic":
				setElementValue('cbid.shadowsocksr.' + sid + '.quic_guise', params.get("headerType") || "none");
				setElementValue('cbid.shadowsocksr.' + sid + '.quic_security', params.get("quicSecurity") || "none");
				setElementValue('cbid.shadowsocksr.' + sid + '.quic_key', params.get("key") || "");
				break;
			case "grpc":
				setElementValue('cbid.shadowsocksr.' + sid + '.serviceName', params.get("serviceName") || "");
				setElementValue('cbid.shadowsocksr.' + sid + '.grpc_mode', params.get("mode") || "gun");
				break;
			case "tcp":
			case "raw":
				setElementValue('cbid.shadowsocksr.' + sid + '.tcp_guise', params.get("headerType") || "none");
				dispatchEventIfExists('cbid.shadowsocksr.' + sid + '.tcp_guise', event);
				if (params.get("headerType") === "http") {
					setElementValue('cbid.shadowsocksr.' + sid + '.http_host', params.get("host") ? decodeURIComponent(params.get("host")) : "");
					setElementValue('cbid.shadowsocksr.' + sid + '.http_path', params.get("path") ? decodeURIComponent(params.get("path")) : "");
				}
				break;
			}
			s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		case "tuic":
			var url0 = (ssu[1] || "");
			var param = "";

			// 先分离 #（alias）
			var hashIndex = url0.indexOf("#");
			if (hashIndex >= 0) {
				param = url0.substring(hashIndex + 1);
				url0 = url0.substring(0, hashIndex);
			}

			// 再分离 ? 或 /?（参数）
			var queryIndex = (url0 = url0.replace('/?', '?')).indexOf("?");
			var query = "";
			if (queryIndex >= 0) {
				query = url0.substring(queryIndex + 1);
				url0 = url0.substring(0, queryIndex);
			}

			var params = Object.fromEntries(new URLSearchParams(query));

			var sipIndex = url0.indexOf("@");
			var userInfo = decodeURIComponent(url0.substring(0, sipIndex));      //  -- 如有Url编码进行解码，格式：uuid:password
			var hostPart = url0.substring(sipIndex + 1);     // 格式：hostname:port
			var userInfoSplitIndex = userInfo.indexOf(":");
			if(userInfoSplitIndex < 0) {
				// 格式错误
				s.innerHTML = "<font style='color:red'><%:Userinfo format error.%></font>";
				break;
			}

			var method = userInfo.substring(0, userInfoSplitIndex);
			var password = userInfo.substring(userInfoSplitIndex + 1);

			var url = new URL("http://" + hostPart); // 用 URL 提取 host 与 port

			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].value = ssu[0];
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.type')[0].dispatchEvent(event);
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server')[0].value = url.hostname;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.server_port')[0].value = url.port;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_uuid')[0].value = method;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_ip')[0].value = params.ip || "";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_passwd')[0].value = password;
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.udp_relay_mode')[0].value = params.udp_relay_mode || "native";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.congestion_control')[0].value = params.congestion_control || "cubic";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_alpn')[0].value = params.alpn || "";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.heartbeat')[0].value = params.heartbeat || "3";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.timeout')[0].value = params.timeout || "8";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.gc_interval')[0].value = params.gc_interval || "3";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.gc_lifetime')[0].value = params.gc_lifetime || "15";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.send_window')[0].value = params.send_window || "20971520";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.receive_window')[0].value = params.receive_window || "10485760";
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_max_package_size')[0].value = params.max_packet_size || "1500";
			if (params["disable_sni"] === "1" || params["disable_sni"] === "true") { 
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.disable_sni')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.disable_sni')[0].dispatchEvent(event);
			}
			if (params["zero_rtt_handshake"] === "1" || params["zero_rtt_handshake"] === "true") { 
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.zero_rtt_handshake')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.zero_rtt_handshake')[0].dispatchEvent(event);
			}
			if (params["dual_stack"] === "1" || params["dual_stack"] === "true") { 
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_dual_stack')[0].checked = true;
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.tuic_dual_stack')[0].dispatchEvent(event);
				if (params.ipstack_prefer && params.ipstack_prefer.trim() !== "") {
					document.getElementsByName('cbid.shadowsocksr.' + sid + '.ipstack_prefer')[0].value = params.ipstack_prefer || "";
				}
			}
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].checked =
				!!(params.allowInsecure ?? params.allowlnsecure ?? params.insecure); // 设置 insecure 为 true
			document.getElementsByName('cbid.shadowsocksr.' + sid + '.insecure')[0].dispatchEvent(event); // 触发事件
			if (param != undefined) {
				document.getElementsByName('cbid.shadowsocksr.' + sid + '.alias')[0].value = decodeURIComponent(param);
			}
			s.innerHTML = "<font style=\'color:green\'><%:Import configuration information successfully.%></font>";
			return false;
		default:
			s.innerHTML = "<font style=\'color:red\'><%:Invalid format.%></font>";
			return false;
		}
	}
	//]]>
</script>
<input type="button" class="btn cbi-button cbi-button-apply" value="<%:Import%>" onclick="return import_ssr_url(this, '<%=self.option%>', '<%=self.value%>')" />
<span id="<%=self.option%>-status"></span>
<%+cbi/valuefooter%>
