| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- // 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 ''; }
- }
- function withQuery(target, values = {}, removeIdentity = false) {
- const hashAt = target.indexOf('#');
- const url = hashAt < 0 ? target : target.slice(0, hashAt);
- const hash = hashAt < 0 ? '' : target.slice(hashAt);
- 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 (removeIdentity) parts = parts.filter(part => !['token','userId','mobile'].includes(queryKey(part)));
- const keys = new Set(parts.map(queryKey));
- for (const [key,value] of Object.entries(values)) {
- if (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','token','userId','mobile','__proto__','constructor','prototype'].includes(key) && typeof value === 'string') values[key] = value;
- }
- return withQuery(target, values, true);
- }
- module.exports = { readTarget, requestedTarget, withQuery };
|