add common tool

pull/375/head
jingjingxyk 3 years ago
parent 0299ecff1a
commit 3959082715

@ -0,0 +1,33 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
# Tab indentation (no size specified)
[Makefile]
indent_style = tab
# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

@ -16,4 +16,88 @@ function removeClass(el, className) {
}
}
export { addClass, removeClass };
function getCookie(name) {
let arr,
reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if ((arr = document.cookie.match(reg))) {
return decodeURIComponent(arr[2]);
} else {
return null;
}
//await cookieStore.get({name:name})
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
function setCookie(name, value, second, domain) {
var exp = new Date();
exp.setTime(exp.getTime() + second * 1000);
document.cookie =
name +
"=" +
encodeURIComponent(value) +
";expires=" +
exp.toGMTString() +
";path=/;domain=" +
domain +
";SameSite=None;Secure";
}
async function getCookies(domain) {
let cookies = await cookieStore.getAll({ domain: domain });
return cookies;
}
function encodeBase64(str) {
return btoa(encodeURIComponent(str));
}
function decodeBase64(encoded) {
return decodeURIComponent(atob(encoded));
}
function getParameterValue(name) {
let reg = new RegExp("[^?&]?" + encodeURI(name) + "=[^&]+");
let arr = location.search.match(reg);
if (arr != null) {
return decodeURI(arr[0].substring(arr[0].search("=") + 1));
}
return "";
}
function createJsonFile(content, filename) {
let blob = new Blob([JSON.stringify(content)], { type: "application/json" });
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = filename;
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 3000);
}
async function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function getMediaDevices() {
return await navigator.mediaDevices.enumerateDevices();
}
// new URLSearchParams
// new URL
// (new Date()).toISOString()
export {
addClass,
removeClass,
hasClass,
setCookie,
getCookie,
encodeBase64,
decodeBase64,
getParameterValue,
sleep,
getMediaDevices,
};

Loading…
Cancel
Save