// Keep query strings intact on both raw and WeChat-encoded page options. function readTarget(value) { if (typeof value !== 'string' || !value || value.length > 8192) return ''; let target = value; if (!target.startsWith('https://')) { try { target = decodeURIComponent(value); } catch (_) { return ''; } } if (/[\\\s\u0000-\u001f]/.test(target)) return ''; const matched = /^https:\/\/([^/?#]+)([^?#]*)(?:[?#]|$)/.exec(target); if (!matched) return ''; if (matched[1] === 'www.yuban.co') return target; if (matched[1] === 'app.fmode.cn' && /^\/dev\/pobingfeng\/owner\/nav\/(?:products|cases|home)\/?$/.test(matched[2])) return target; return ''; } function queryKey(part) { try { return decodeURIComponent(part.split('=')[0]); } catch (_) { return ''; } } const identityKeys = new Set(['token','sessiontoken','accesstoken','access_token','refreshtoken','refresh_token','authorization','userid','mobile','openid','unionid']); function isIdentity(key) { return identityKeys.has(String(key).toLowerCase()); } function withQuery(target, values = {}, clean = false) { // Existing payment callbacks own their OAuth code and state. const payment = /^https:\/\/www\.yuban\.co\/user-profile\/(?:account-recharge|membership-renewal)\/?(?:[?#]|$)/.test(target); const omit = key => clean && (isIdentity(key) || (!payment && ['code','state'].includes(key))); const hashAt = target.indexOf('#'); const url = hashAt < 0 ? target : target.slice(0, hashAt); let hash = hashAt < 0 ? '' : target.slice(hashAt); if (clean && hash.includes('?')) { const separator = hash.indexOf('?'); const params = hash.slice(separator+1).split('&').filter(part => part && !omit(queryKey(part))); hash = hash.slice(0,separator) + (params.length ? '?' + params.join('&') : ''); } const index = url.indexOf('?'); const base = index < 0 ? url : url.slice(0, index); let parts = index < 0 ? [] : url.slice(index + 1).split('&').filter(Boolean); if (clean) parts = parts.filter(part => !omit(queryKey(part))); const keys = new Set(parts.map(queryKey)); for (const [key,value] of Object.entries(values)) { if (omit(key) || keys.has(key) || value === '' || value == null) continue; parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(String(value))); } return base + (parts.length ? '?' + parts.join('&') : '') + hash; } function requestedTarget(options = {}) { const target = readTarget(options.path); if (!target) return ''; const values = {}; for (const [key,value] of Object.entries(options)) { if (!['path','url','skipAuth','__proto__','constructor','prototype'].includes(key) && typeof value === 'string') values[key] = value; } return withQuery(target, values, true); } module.exports = { readTarget, requestedTarget, withQuery };