-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
163 lines (141 loc) · 4.42 KB
/
popup.js
File metadata and controls
163 lines (141 loc) · 4.42 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const $enabled = document.getElementById("enabled");
const $camera = document.getElementById("camera");
const $customLabel = document.getElementById("customLabel");
const $status = document.getElementById("status");
const $errorSection = document.getElementById("errorSection");
const $errorMsg = document.getElementById("errorMsg");
const $cameraSection = document.getElementById("cameraSection");
const $grantBtn = document.getElementById("grantBtn");
const $cameraNames = document.getElementById("cameraNames");
let cameras = [];
function renderCameraNames() {
$cameraNames.innerHTML = "";
cameras.forEach((cam) => {
const li = document.createElement("li");
li.textContent = cam.label;
li.addEventListener("click", () => {
navigator.clipboard.writeText(cam.label);
li.classList.add("copied");
li.textContent = "Copied!";
setTimeout(() => {
li.classList.remove("copied");
li.textContent = cam.label;
}, 1000);
});
$cameraNames.appendChild(li);
});
}
function showError(msg, showGrant) {
$errorSection.style.display = "";
$errorMsg.textContent = msg;
$grantBtn.style.display = showGrant ? "" : "none";
$cameraSection.style.display = "none";
}
async function getActiveTab() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
}
function sendToTab(tabId, message) {
return new Promise((resolve, reject) => {
chrome.tabs.sendMessage(tabId, message, (response) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
} else {
resolve(response);
}
});
});
}
async function loadCameras() {
const tab = await getActiveTab();
if (!tab) {
showError("No active tab found.", false);
return;
}
let result;
try {
result = await sendToTab(tab.id, { type: "getDevices" });
} catch (err) {
showError(
"Cannot reach this page. Open a regular website, reload it, and reopen this popup.",
false
);
return;
}
const devices = result.cameras || [];
const hasLabels = devices.some((d) => d.label);
if (!hasLabels) {
showError("Camera access needed on this site to see device names.", true);
return;
}
cameras = devices.filter((d) => d.label);
$errorSection.style.display = "none";
$cameraSection.style.display = "";
$camera.innerHTML = "";
cameras.forEach((cam) => {
const opt = document.createElement("option");
opt.value = cam.label;
opt.textContent = cam.label;
$camera.appendChild(opt);
});
renderCameraNames();
}
async function loadConfig() {
const result = await chrome.storage.local.get("camSpoofer");
const config = result.camSpoofer;
if (!config) return;
$enabled.checked = !!config.enabled;
$customLabel.value = config.customLabel || "";
if (config.selectedLabel) {
$camera.value = config.selectedLabel;
if (!$camera.value && cameras.length) {
$camera.value = cameras[0].label;
}
}
}
function save() {
const selectedLabel = $camera.value;
const config = {
enabled: $enabled.checked,
selectedLabel,
customLabel: $customLabel.value.trim(),
};
chrome.storage.local.set({ camSpoofer: config }, () => {
$status.textContent = "Saved — reload tabs to apply";
$status.className = "status saved";
setTimeout(() => {
$status.textContent = "";
$status.className = "status";
}, 2000);
});
}
$grantBtn.addEventListener("click", async () => {
const tab = await getActiveTab();
if (!tab) return;
try {
const result = await sendToTab(tab.id, { type: "requestCamera" });
const devices = result.cameras || [];
if (devices.some((d) => d.label)) {
cameras = devices.filter((d) => d.label);
$errorSection.style.display = "none";
$cameraSection.style.display = "";
$camera.innerHTML = "";
cameras.forEach((cam) => {
const opt = document.createElement("option");
opt.value = cam.label;
opt.textContent = cam.label;
$camera.appendChild(opt);
});
renderCameraNames();
await loadConfig();
} else {
showError("Permission denied. Allow camera in browser and retry.", false);
}
} catch (_) {
showError("Could not request camera. Reload the page and try again.", false);
}
});
$enabled.addEventListener("change", save);
$camera.addEventListener("change", save);
$customLabel.addEventListener("input", save);
loadCameras().then(loadConfig);