-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
79 lines (69 loc) · 2.2 KB
/
popup.js
File metadata and controls
79 lines (69 loc) · 2.2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const DEFAULTS = { enabled: true }
const getSettings = async () => {
const obj = await chrome.storage.local.get('settings')
return { ...DEFAULTS, ...(obj.settings || {}) }
}
document.addEventListener('DOMContentLoaded', async () => {
let state = await getSettings()
const enabledEl = document.getElementById('enabled')
const statusText = document.getElementById('status-text')
const refreshBtn = document.getElementById('refresh')
const optionsBtn = document.getElementById('open-options')
const updateUI = () => {
enabledEl.checked = state.enabled
statusText.textContent = state.enabled ? 'Activo' : 'Inactivo'
statusText.style.color = state.enabled ? '#01a373' : '#999'
}
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0]
if (activeTab && activeTab.url) {
try {
const url = new URL(activeTab.url)
if (url.hostname.includes('marca.com')) {
refreshBtn.style.display = 'flex'
}
} catch (e) {}
}
})
if (enabledEl) {
updateUI()
enabledEl.addEventListener('change', async () => {
state.enabled = enabledEl.checked
updateUI()
const current = await getSettings()
current.enabled = state.enabled
await chrome.storage.local.set({ settings: current })
reloadCurrentTabIfMarca()
})
}
if (refreshBtn) {
refreshBtn.addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) chrome.tabs.reload(tabs[0].id)
window.close()
})
})
}
if (optionsBtn) {
optionsBtn.addEventListener('click', () => {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage()
} else {
window.open(chrome.runtime.getURL('options.html'))
}
})
}
})
const reloadCurrentTabIfMarca = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0]
if (activeTab && activeTab.url) {
try {
const currentUrl = new URL(activeTab.url)
if (currentUrl.hostname.includes('marca.com')) {
chrome.tabs.reload(activeTab.id)
}
} catch (_e) {}
}
})
}