59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
window.cookies = {
|
|
Read: function (name) {
|
|
try {
|
|
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
|
|
if (match) return decodeURIComponent(match[2]);
|
|
return '';
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
},
|
|
Write: function (name, value, expiresIso) {
|
|
try {
|
|
var expires = '';
|
|
if (expiresIso) {
|
|
expires = '; expires=' + new Date(expiresIso).toUTCString();
|
|
}
|
|
document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/';
|
|
} catch (e) { }
|
|
},
|
|
Delete: function (cookie_name) {
|
|
document.cookie = cookie_name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
}
|
|
};
|
|
|
|
window.ajax = {
|
|
Post: async function (url, data) {
|
|
try {
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'include',
|
|
body: JSON.stringify(data)
|
|
});
|
|
const text = await res.text();
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
return { success: res.ok, message: text };
|
|
}
|
|
} catch (err) {
|
|
return { success: false, message: err?.toString() ?? 'network error' };
|
|
}
|
|
}
|
|
};
|
|
|
|
window.getBrowserLanguage = function () {
|
|
try {
|
|
return (navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage || '';
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
};
|
|
|
|
window.setHtmlLang = function (lang) {
|
|
try {
|
|
if (document && document.documentElement) document.documentElement.lang = lang || '';
|
|
} catch (e) { }
|
|
};
|