QRCode type detection - Javascript
QRCode type detection
2021
//#region type detectionlet TYPE_TEXT = "TEXT";let TYPE_URL = "URL";let TYPE_PHONE = "PHONE NUMBER";let TYPE_WIFI = "WIFI";let TYPE_UPI = "UPI";function isUrl(decodedText) {var expression1 =/^((javascript:[\w-_]+(\([\w-_\s,.]*\))?)|(mailto:([\w\u00C0-\u1FFF\u2C00-\uD7FF-_]+\.)*[\w\u00C0-\u1FFF\u2C00-\uD7FF-_]+@([\w\u00C0-\u1FFF\u2C00-\uD7FF-_]+\.)*[\w\u00C0-\u1FFF\u2C00-\uD7FF-_]+)|(\w+:\/\/(([\w\u00C0-\u1FFF\u2C00-\uD7FF-]+\.)*([\w\u00C0-\u1FFF\u2C00-\uD7FF-]*\.?))(:\d+)?(((\/[^\s#$%^&*?]+)+|\/)(\?[\w\u00C0-\u1FFF\u2C00-\uD7FF:;&%_,.~+=-]+)?)?(#[\w\u00C0-\u1FFF\u2C00-\uD7FF-_]+)?))$/g;var regexExp1 = new RegExp(expression1);if (decodedText.match(regexExp1)) {return true;}var expression2 =/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;var regexExp2 = new RegExp(expression2);if (decodedText.match(regexExp2)) {return true;}return false;}function isPhoneNumber(decodedText) {var expression = /tel:[+]*[0-9]{3,}/g;var regexExp = new RegExp(expression);return decodedText.match(regexExp);}function isWifi(decodedText) {var expression = /WIFI:S:(.*);T:(.*);P:(.*);H:(.*);;/g;var regexExp = new RegExp(expression);return decodedText.match(regexExp);}function isUpi(decodedText) {try {var upiUri = new URL(decodedText);if (!upiUri || upiUri == null) {return false;}return upiUri.protocol === 'upi:';} catch (err) {return false;}}function detectType(decodedText) {if (isUrl(decodedText)) {return TYPE_URL;}if (isPhoneNumber(decodedText)) {return TYPE_PHONE;}if (isWifi(decodedText)) {return TYPE_WIFI;}if (isUpi(decodedText)) {return TYPE_UPI;}return TYPE_TEXT;}//#endregion
0 nhận xét:
Post a Comment