- Deleted the `app.js` file and migrated its functionality to a modular structure with multiple JavaScript files for better organization and maintainability. - Updated `index.html` to reference the new `main.js` module, ensuring proper loading of the application. - Introduced new utility modules for API requests, authentication, calendar handling, and DOM manipulation to enhance code clarity and separation of concerns. - Enhanced CSS styles for improved layout and theming consistency across the application. - Added comprehensive comments and documentation to new modules to facilitate future development and understanding.
82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
/**
|
|
* Telegram init data and access checks.
|
|
*/
|
|
|
|
/**
|
|
* Get tgWebAppData value from hash when it contains unencoded & and =.
|
|
* Value runs from tgWebAppData= until next &tgWebApp or end.
|
|
* @param {string} hash - location.hash without #
|
|
* @returns {string}
|
|
*/
|
|
export function getTgWebAppDataFromHash(hash) {
|
|
const idx = hash.indexOf("tgWebAppData=");
|
|
if (idx === -1) return "";
|
|
const start = idx + "tgWebAppData=".length;
|
|
let end = hash.indexOf("&tgWebApp", start);
|
|
if (end === -1) end = hash.length;
|
|
const raw = hash.substring(start, end);
|
|
try {
|
|
return decodeURIComponent(raw);
|
|
} catch (e) {
|
|
return raw;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Telegram init data string (from SDK, hash or query).
|
|
* @returns {string}
|
|
*/
|
|
export function getInitData() {
|
|
const fromSdk =
|
|
(window.Telegram && window.Telegram.WebApp && window.Telegram.WebApp.initData) || "";
|
|
if (fromSdk) return fromSdk;
|
|
const hash = window.location.hash ? window.location.hash.slice(1) : "";
|
|
if (hash) {
|
|
const fromHash = getTgWebAppDataFromHash(hash);
|
|
if (fromHash) return fromHash;
|
|
try {
|
|
const hashParams = new URLSearchParams(hash);
|
|
const tgFromHash = hashParams.get("tgWebAppData");
|
|
if (tgFromHash) return decodeURIComponent(tgFromHash);
|
|
} catch (e) {
|
|
/* ignore */
|
|
}
|
|
}
|
|
const q = window.location.search
|
|
? new URLSearchParams(window.location.search).get("tgWebAppData")
|
|
: null;
|
|
if (q) {
|
|
try {
|
|
return decodeURIComponent(q);
|
|
} catch (e) {
|
|
return q;
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/**
|
|
* @returns {boolean} True if host is localhost or 127.0.0.1
|
|
*/
|
|
export function isLocalhost() {
|
|
const h = window.location.hostname;
|
|
return h === "localhost" || h === "127.0.0.1" || h === "";
|
|
}
|
|
|
|
/**
|
|
* True when hash has tg WebApp params but no init data (e.g. version without data).
|
|
* @returns {boolean}
|
|
*/
|
|
export function hasTelegramHashButNoInitData() {
|
|
const hash = window.location.hash ? window.location.hash.slice(1) : "";
|
|
if (!hash) return false;
|
|
try {
|
|
const keys = Array.from(new URLSearchParams(hash).keys());
|
|
const hasVersion = keys.indexOf("tgWebAppVersion") !== -1;
|
|
const hasData = keys.indexOf("tgWebAppData") !== -1 || !!getTgWebAppDataFromHash(hash);
|
|
return hasVersion && !hasData;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|