-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
47 lines (40 loc) · 1020 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import mitt from "mitt";
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
async function getStore(key = "store") {
const items = await chrome.storage.local.get(key);
return items;
}
const getUrlInfo = (url) => {
const { pathname, hostname } = new URL(url);
return { pathname, hostname };
};
const getCurrentTabStatus = async () => {
const { status, url, title } = await getCurrentTab();
const { hostname, pathname } = getUrlInfo(url);
return { status, url, hostname, pathname, title };
};
const debounce = (fn, timeout) => {
let timer = null;
return function () {
const context = this;
const args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(context, args);
clearTimeout(timer);
}, timeout);
};
};
const emitter = mitt();
export {
getCurrentTab,
getStore,
getUrlInfo,
debounce,
getCurrentTabStatus,
emitter,
};