From 395908271538a5d979992f183388ec17e905d4a3 Mon Sep 17 00:00:00 2001 From: jingjingxyk Date: Wed, 15 Jun 2022 21:29:40 +0800 Subject: [PATCH] add common tool --- .editorconfig | 33 +++++++++ tools/chromium_extension/js/app/until.js | 86 +++++++++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..81ed7f2 --- /dev/null +++ b/.editorconfig @@ -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 \ No newline at end of file diff --git a/tools/chromium_extension/js/app/until.js b/tools/chromium_extension/js/app/until.js index 19b4ecd..73b3baa 100644 --- a/tools/chromium_extension/js/app/until.js +++ b/tools/chromium_extension/js/app/until.js @@ -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, +};