-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblock.js
More file actions
116 lines (93 loc) · 3.36 KB
/
block.js
File metadata and controls
116 lines (93 loc) · 3.36 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
const MIN_CHARS = 5;
const textarea = document.getElementById("reason");
const counter = document.getElementById("counter");
const submitBtn = document.getElementById("submit");
// Get tab ID from URL params
const urlParams = new URLSearchParams(window.location.search);
const tabId = parseInt(urlParams.get("tabId"), 10);
function countNonWhitespace(str) {
return str.replace(/\s/g, "").length;
}
function updateCounter() {
const count = countNonWhitespace(textarea.value);
const isValid = count >= MIN_CHARS;
counter.textContent = `${count} / ${MIN_CHARS} characters`;
counter.classList.toggle("valid", isValid);
submitBtn.disabled = !isValid;
}
textarea.addEventListener("input", updateCounter);
submitBtn.addEventListener("click", () => {
const count = countNonWhitespace(textarea.value);
if (count < MIN_CHARS) return;
submitBtn.disabled = true;
submitBtn.textContent = "...";
chrome.runtime.sendMessage(
{ type: "ALLOW_ACCESS", tabId: tabId, reason: textarea.value.trim() },
(response) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
submitBtn.textContent = "Error";
submitBtn.disabled = false;
}
}
);
});
// Handle Enter key (Ctrl+Enter or Cmd+Enter to submit)
textarea.addEventListener("keydown", (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
if (!submitBtn.disabled) {
submitBtn.click();
}
}
});
// History functionality
const historyToggle = document.getElementById("historyToggle");
const historyList = document.getElementById("historyList");
function formatTime(timestamp) {
const date = new Date(timestamp);
const now = new Date();
const diff = now - date;
// Today
if (diff < 24 * 60 * 60 * 1000 && date.getDate() === now.getDate()) {
return "Today at " + date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
// Yesterday
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
if (date.getDate() === yesterday.getDate()) {
return "Yesterday at " + date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
// Older
return date.toLocaleDateString([], { month: "short", day: "numeric" }) +
" at " + date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
function escapeHtml(text) {
const div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
function renderHistory(log) {
if (!log || log.length === 0) {
historyList.innerHTML = '<p class="history-empty">No past reasons yet</p>';
return;
}
historyList.innerHTML = log.map(entry => `
<div class="history-item">
<div class="history-reason">${escapeHtml(entry.reason)}</div>
<div class="history-time">${formatTime(entry.timestamp)}</div>
</div>
`).join("");
}
historyToggle.addEventListener("click", () => {
const isOpen = historyToggle.classList.toggle("open");
historyList.classList.toggle("visible", isOpen);
if (isOpen) {
chrome.runtime.sendMessage({ type: "GET_REASON_LOG" }, (response) => {
if (response && response.log) {
renderHistory(response.log);
}
});
}
});
// Initial state
updateCounter();