-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
149 lines (134 loc) · 4.55 KB
/
popup.js
File metadata and controls
149 lines (134 loc) · 4.55 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Lapsed - popup script
(() => {
const ui = mapElements(['domainInput', 'checkBtn', 'recentList', 'clearBtn']);
const STATUS_UNKNOWN_LABEL = 'Status unknown';
const LEGACY_STATUS_MAP = {
available: 'status-available',
registered: 'status-taken',
expired: 'status-dropping',
error: 'status-error',
pending: 'status-checking'
};
init();
function init() {
bindEvents();
loadHistory();
ui.domainInput.focus();
}
function bindEvents() {
ui.checkBtn.addEventListener('click', handleManualCheck);
ui.domainInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') handleManualCheck();
});
ui.clearBtn.addEventListener('click', handleClearHistory);
}
function handleManualCheck() {
const parsed = parseDomainInput(ui.domainInput.value);
if (!parsed) return;
addToHistory(parsed);
openLapsed(parsed);
}
function addToHistory(domain) {
const entry = {
domain,
url: 'https://' + domain,
ts: Date.now(),
status: 'status-checking',
statusLabel: 'Checking'
};
chrome.storage.local.get(['failureHistory'], (data) => {
const history = data.failureHistory || [];
const filtered = history.filter(item => item.domain !== domain);
filtered.unshift(entry);
chrome.storage.local.set({ failureHistory: filtered.slice(0, 10) });
});
}
function handleClearHistory() {
chrome.storage.local.remove('failureHistory', () => {
ui.recentList.innerHTML = '<div class="recent-empty">No recent lookups yet</div>';
});
}
function parseDomainInput(value) {
const trimmed = (value || '').trim();
if (!trimmed) return null;
const normalized = trimmed
.replace(/^https?:\/\//i, '')
.replace(/\/.*$/, '')
.toLowerCase();
return normalized || null;
}
function openLapsed(domain, originalUrl) {
const url = chrome.runtime.getURL(
`lapsed.html?domain=${encodeURIComponent(domain)}&originalUrl=${encodeURIComponent(originalUrl || 'https://' + domain)}`
);
chrome.tabs.create({ url });
window.close();
}
function loadHistory() {
chrome.storage.local.get(['failureHistory'], (data) => {
const history = (data.failureHistory || []).slice(0, 10);
if (!history.length) {
ui.recentList.innerHTML = '<div class="recent-empty">No recent lookups yet</div>';
ui.clearBtn.style.display = 'none';
return;
}
ui.clearBtn.style.display = '';
ui.recentList.innerHTML = history.map(renderHistoryItem).join('');
ui.recentList.querySelectorAll('.recent-item').forEach((el) => {
el.addEventListener('click', () => openLapsed(el.dataset.domain, el.dataset.url));
});
});
}
function renderHistoryItem(item) {
const ago = timeAgo(item.ts);
const statusKey = normalizeStatusKey(item.status);
const statusClass = statusKey || 'status-error';
const statusLabel = item.statusLabel
|| (statusKey ? humanizeStatus(statusKey) : STATUS_UNKNOWN_LABEL);
const safeUrl = item.url || '';
const tooltip = statusLabel ? `title="${escapeAttr(statusLabel)}" aria-label="${escapeAttr(statusLabel)}"` : '';
return `
<div class="recent-item" data-domain="${item.domain}" data-url="${safeUrl}" ${tooltip}>
<span class="recent-domain">${item.domain}</span>
<span class="recent-meta">
<span class="recent-time">${ago}</span>
<span class="recent-status ${statusClass}"></span>
</span>
</div>
`;
}
function timeAgo(ts) {
const diff = Date.now() - ts;
const minutes = Math.floor(diff / 60000);
if (minutes < 1) return 'just now';
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h ago`;
return `${Math.floor(hours / 24)}d ago`;
}
function mapElements(ids) {
return ids.reduce((acc, id) => {
acc[id] = document.getElementById(id);
return acc;
}, {});
}
function normalizeStatusKey(raw) {
const key = typeof raw === 'string' ? raw.trim().toLowerCase() : '';
if (!key) return '';
if (key.startsWith('status-')) return key;
return LEGACY_STATUS_MAP[key] || '';
}
function humanizeStatus(key) {
if (!key) return '';
return key.replace(/^status-/, '')
.split('-')
.map(part => part ? part[0].toUpperCase() + part.slice(1) : part)
.join(' ');
}
function escapeAttr(text) {
if (!text) return '';
return text.replace(/["&<>]/g, ch =>
({ '"': '"', '&': '&', '<': '<', '>': '>' }[ch])
);
}
})();