forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseExtensionPage.ts
More file actions
48 lines (42 loc) · 1.47 KB
/
useExtensionPage.ts
File metadata and controls
48 lines (42 loc) · 1.47 KB
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
48
import { computed, ref } from 'vue';
import { SettingsTab } from '../utils/enums';
import { getStringTab } from '../utils/extension-tabs';
export const QUERY_PARAMS_DASHBOARD = 'dashboard.html';
export const QUERY_PARAMS_TAB = 'tab';
export const QUERY_PARAMS_TAB_LIMITS = 'limits';
export const QUERY_PARAMS_BLOCK = 'block.html';
export const QUERY_PARAMS_BLOCK_DOMAIN = 'domain';
export function useExtensionPage() {
const urlObj = ref(new URL(location.href));
const isLimitPage = computed(
() =>
urlObj.value.hostname == __APP_ID__ &&
urlObj.value.pathname.includes(QUERY_PARAMS_DASHBOARD) &&
urlObj.value.searchParams.get(QUERY_PARAMS_TAB) == QUERY_PARAMS_TAB_LIMITS,
);
const isBlockPage = computed(
() =>
urlObj.value.hostname == __APP_ID__ &&
urlObj.value.pathname.includes(QUERY_PARAMS_BLOCK) &&
urlObj.value.searchParams.get(QUERY_PARAMS_BLOCK_DOMAIN)?.includes('youtube.com'),
);
function updateTab(tab: SettingsTab) {
let targetTab = getStringTab(tab);
const currentTab = urlObj.value.searchParams.get(QUERY_PARAMS_TAB);
if (window.history.replaceState && currentTab) {
const sourceUrl = `tab=${currentTab}`;
const targetUrl = `tab=${targetTab}`;
window.history.replaceState(
location.href,
document.title,
location.href.replace(sourceUrl, targetUrl),
);
urlObj.value = new URL(location.href);
}
}
return {
isLimitPage,
isBlockPage,
updateTab,
};
}