2 lines
9.2 KiB
JavaScript
2 lines
9.2 KiB
JavaScript
const e="undefined"!=typeof wx&&"function"==typeof wx.getSystemInfoSync&&Boolean(wx.getSystemInfoSync().fontSizeSetting),t="undefined"!=typeof uni&&"undefined"==typeof window,n=e||t,o="undefined"!=typeof uni,r=("undefined"!=typeof uni||"undefined"!=typeof window)&&!n,i=e?wx:o?uni:"undefined"!=typeof window?window:{},s=r&&window&&window.navigator&&window.navigator.userAgent||"",c=/Android/i.test(s),l=/(?:Windows Phone)/.test(s),a=/(?:SymbianOS)/.test(s),u=/OpenHarmony/i.test(s),d=r&&("ontouchstart"in window||navigator.maxTouchPoints>0)&&function(){if(r){if(/Tablet|Pad/i.test(s))return!0;if(180===window.orientation||0===window.orientation)return window.innerWidth>=768&&window.innerHeight>=1024;if(90===window.orientation||-90===window.orientation)return window.innerWidth>=1024&&window.innerHeight>=768}return!1}(),f=/iPhone/i.test(s)||/iPod/i.test(s),w=e,h=c||l||a||f||d||u,m=r&&!h,p=t&&!e;function g(){let e="";return m?e="pc":h?e="h5":w?e="wechat":p&&(e="app"),e}function b(t){const{count:n=1,sourceType:r=["album"],mediaType:s,dom:c}=t;return new Promise(((t,l)=>{o?e&&i.chooseMedia?i.chooseMedia({count:n,sourceType:r,mediaType:s,success:e=>{t({files:e})},error:e=>{l(e)}}):i.chooseVideo({count:n,sourceType:r,success:e=>{t({files:e})},error:e=>{l(e)}}):((null==c?void 0:c.click)&&c.click(),c.value="",null==c||c.addEventListener("change",(e=>{var n;(null===(n=e.target.files)||void 0===n?void 0:n.length)<=0||t({files:e.target.files[0]})}),!1))}))}function y(e){return b(Object.assign({mediaType:["image"]},e))}function S(e){return b(Object.assign({mediaType:["video"]},e))}const E=new Map;function v(e,t){E.set(e,t)}function T(e,t){if(!e)return Promise.reject(new Error(`getBoundingClientRect get error selector ${typeof e}.`));if(window||document){const t="object"==typeof e?e:document.querySelector(e);if(!t)return Promise.reject(new Error(`getBoundingClientRect can't find ${e} dom.`));const n=t.getBoundingClientRect();return Promise.resolve({id:t.id,top:n.top,left:n.left,right:n.right,bottom:n.bottom,width:n.width,height:n.height,x:n.x,y:n.y})}if(o){if(!t)return Promise.reject(new Error("getBoundingClientRect need instanceName in params."));const n=uni.createSelectorQuery().in(E.get(t));return new Promise(((t,o)=>{n.select(e).boundingClientRect((e=>{e&&t(e)})).exec(),setTimeout((()=>o(new Error(`getBoundingClientRect ${e} timeout.`))),500)}))}return Promise.reject(new Error("getBoundingClientRect occur error"))}function C(e){if(o)throw new Error("getBoundingClientRectSync can not use in uni-app.");if(window||document){const t="object"==typeof e?e:document.querySelector(e);if(!t)throw new Error(`getBoundingClientRectSync can't find ${e} dom.`);const n=t.getBoundingClientRect();return{id:t.id,top:n.top,left:n.left,right:n.right,bottom:n.bottom,width:n.width,height:n.height,x:n.x,y:n.y}}throw new Error("getBoundingClientRectSync occur error.")}function P(e,t){if(!e)return Promise.reject(new Error(`getScrollInfo get error selector ${typeof e}.`));if(!o&&window){const t="object"==typeof e?e:document.querySelector(e);return t?Promise.resolve({id:t.id,scrollTop:t.scrollTop,scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,scrollHeight:t.scrollHeight}):Promise.reject(new Error(`getScrollInfo can't find ${e} dom.`))}if(o){if(!t)return Promise.reject(new Error("getScrollInfo need instanceName in params."));const n=uni.createSelectorQuery().in(E.get(t));return new Promise(((t,o)=>{n.select(e).scrollOffset((e=>{e&&t(e)})).exec(),setTimeout((()=>o(new Error(`getScrollInfo ${e} timeout.`))),500)}))}return Promise.reject(new Error("getScrollInfo occur error"))}function x(e){if(o)throw new Error("getScrollInfoSync can not use in uni-app.");if(!o&&window){const t="object"==typeof e?e:document.querySelector(e);if(!t)throw new Error(`getScrollInfoSync can't find ${e} dom.`);return{id:t.id,scrollTop:t.scrollTop,scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,scrollHeight:t.scrollHeight}}throw new Error("getScrollInfoSync() occur error.")}function R(e,t){if(o){if(!t)return Promise.reject(new Error("getFields need instanceName in params."));const n=uni.createSelectorQuery().in(E.get(t));return new Promise(((t,o)=>{n.select(e).fields({rect:!0,size:!0,scrollOffset:!0},(e=>{e&&t(e)})).exec(),setTimeout((()=>o(new Error(`get ${e} fields timeout.`))),500)}))}return Promise.reject(new Error("getFields occur error"))}class L{constructor(){if(L.instance)return L.instance;L.instance=this,this.eventCallback=null,this.listener=null,this.button=0,this.init()}init(){r&&(this.eventCallback=e=>{this.listener&&this.listener(e)},document.addEventListener("mousedown",this.eventCallback))}listen(e){if(r){const{domRefs:t,ignoreDomRefs:n,handler:o,button:r=0}=e;this.button=r,this.listener=e=>{const r=Array.isArray(n)?n:[n];for(const t of r)if(t&&t.contains(e.target))return;const i=Array.isArray(t)?t:[t];for(const t of i)if(t&&t.contains(e.target)&&this.button===e.button)return;o(e),this.remove()}}}remove(){this.listener=null,this.button=-1}}function k(e){const{element:t,onLongPress:n,options:o={}}=e;if(!t||!n)throw new Error("addLongPressEventListener missing arguments");const{duration:r=800,eventDelegation:i}=o;let s;function c(){clearTimeout(s),a()}function l(){clearTimeout(s),a()}function a(){t.removeEventListener("pointerup",c),t.removeEventListener("pointermove",l)}t.addEventListener("pointerdown",(e=>{const o=e.target,a=(null==i?void 0:i.subSelector)?o.closest(i.subSelector)||void 0:o,u=e.currentTarget;s=setTimeout((()=>n(e,a,u)),r),t.addEventListener("pointerup",c),t.addEventListener("pointermove",l)}))}const I={PROTOCOLS:["javascript:","vbscript:","file:","about:","ftp:","ws:","wss:","mailto:","tel:","sms:","gopher:","data:","blob:"],HTML_TAGS:["<script","<img","<iframe","<object","<embed","<video","<audio","<link","<meta","<style","<form","<input","<svg","<xml","<xmp","<xsl","<base"],EVENT_HANDLERS:["onclick","onload","onerror","onmouseover","onmouseout","onfocus","onblur","onsubmit","onreset","onselect","onchange","onkeyup","onkeydown","onkeypress","ondblclick","oncontextmenu","ondrag","ondrop","onmouseenter","onmouseleave","onanimation","onafterprint","onbeforeprint","onbeforeunload","onhashchange","onmessage","onoffline","ononline","onpagehide","onpageshow","onpopstate","onresize","onstorage","onunload"],JS_FUNCTIONS:["eval(","setTimeout(","setInterval(","Function(","constructor","prototype","__proto__","window.","document.","location.","alert(","confirm(","prompt(","fetch(","XMLHttpRequest"],CSS_PATTERNS:["expression(","url(","@import","behavior:","-moz-binding"],ENCODING_SEQUENCES:["&#","\\","fromCharCode","String.fromCharCode","unescape(","escape(","encodeURI(","decodeURI(","atob(","btoa("],SPECIAL_MARKERS:["\x3c!--","--\x3e","*/>","]]>","<!ENTITY","<!DOCTYPE"],DATA_MARKERS:["base64,","0x","%0","U+","\\u","\\x"],OTHER_PATTERNS:["null","undefined","NaN","true","false","instanceof"]};function j(e,t={}){const{maxLength:n=2083,allowedProtocols:o=["http:","https:","mailto:"],allowCredentials:r=!1,allowDataUrl:i=!1}=t;if(!e||"string"!=typeof e)return"about:blank";if(e.length>n)return"about:blank";try{let t=e.replace(/[\u0000-\u001F\u007F-\u009F\u2000-\u200D\uFEFF]/g,"");t=t.normalize("NFKC"),t=t.replace(/\s+/g," ").trim();let n=t;const s=new Set;let c=0;for(;c<5;)try{const e=decodeURIComponent(n);if(s.has(e))break;s.add(e),n=e,c++}catch(e){break}const l=function(){const e=[],t=Object.keys(I);for(let n=0;n<t.length;n++){const o=I[t[n]];for(let t=0;t<o.length;t++)e.push(o[t])}return e}();for(const e of s){const t=e.toLowerCase();for(let e=0;e<l.length;e++)if(-1!==t.indexOf(l[e]))return"about:blank"}const a=function(e){const t=/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/,n=e.match(t);if(!n)throw new Error("Invalid URL");const o=(n[4]||"").split("@"),r=o[o.length-1];let i="",s="";if(o.length>1){const e=o[0].split(":");i=e[0]||"",s=e[1]||""}const c=r.split(":"),l=c[0],a=c[1]||"";return{href:e,protocol:(n[1]||"").toLowerCase(),host:r,hostname:l,port:a,pathname:n[5]||"",search:n[6]||"",hash:n[8]||"",username:i,password:s}}(t),u=a.protocol.replace(":","").toLowerCase()+":";let d=!1;for(let e=0;e<o.length;e++)if(o[e]===u){d=!0;break}if(!d)return"about:blank";if(!r&&(a.username||a.password))return"about:blank";if(!i&&"data:"===u)return"about:blank";if(!/^[\w\-.]+(:\d+)?$/.test(a.host))return"about:blank";const f=a.pathname+a.search+a.hash;return/^[\w\-./=?#&%+@,:()[\]]+$/.test(f)?t:"about:blank"}catch(e){return"about:blank"}}function A(e){const t=function(e){const t=/(?:https?:\/\/|mailto:|www\.)(?:localhost(?::\d{1,5})?|[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b)(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)/gi,n=[],o=e.split(t),r=e.match(t)||[];let i=0;return o.forEach((e=>{if(e&&n.push({type:"text",text:e}),r[i]){const e=r[i];let t=e;e.toLowerCase().startsWith("www.")&&(t="https://"+e),n.push({type:"url",text:e,url:t}),i++}})),n}(e);return t.map((e=>{if("url"===e.type){const t=j(e.url);return"about:blank"===t?{type:"text",text:e.text}:Object.assign(Object.assign({},e),{url:t})}return e}))}console.log("UniversalAPI.VERSION:2.4.0");const O=new L;export{i as TUIGlobal,k as addLongPressListener,y as chooseImage,S as chooseVideo,T as getBoundingClientRect,C as getBoundingClientRectSync,R as getFields,g as getPlatform,P as getScrollInfo,x as getScrollInfoSync,f as isIOS,O as outsideClick,A as parseTextAndValidateUrls,j as sanitizeUrl,v as setInstanceMapping};
|