Files
SocialMedia-Cleanup-Tool/popup.js
T

78 lines
1.9 KiB
JavaScript

const statusEl = document.getElementById("status");
function setStatus(text) {
statusEl.textContent = text;
}
async function getFacebookTab() {
const tabs = await chrome.tabs.query({
active: true,
currentWindow: true,
});
const tab = tabs[0];
if (!tab?.id) throw new Error("No active tab");
if (!/^https:\/\/(www\.)?facebook\.com\//i.test(tab.url || "")) {
throw new Error("Open a facebook.com Activity Log tab first");
}
return tab;
}
async function ping(tabId) {
return chrome.tabs.sendMessage(tabId, { type: "FB_CLEANER_PING" });
}
async function injectContent(tabId) {
if (!chrome.scripting?.executeScript) {
throw new Error(
"Reload this extension on chrome://extensions, then refresh Facebook"
);
}
await chrome.scripting.executeScript({
target: { tabId },
files: ["content.js"],
});
if (chrome.scripting.insertCSS) {
await chrome.scripting.insertCSS({
target: { tabId },
files: ["panel.css"],
});
}
// Give the content script a moment to register listeners
await new Promise((r) => setTimeout(r, 150));
}
async function ensureContent(tabId) {
try {
await ping(tabId);
} catch {
await injectContent(tabId);
await ping(tabId);
}
}
async function send(type) {
const tab = await getFacebookTab();
await ensureContent(tab.id);
return chrome.tabs.sendMessage(tab.id, { type });
}
document.getElementById("start").addEventListener("click", async () => {
try {
setStatus("Starting…");
const res = await send("FB_CLEANER_START");
setStatus(res?.ok ? "Deletion started on the page" : res?.error || "Failed");
} catch (e) {
setStatus(e.message || String(e));
}
});
document.getElementById("stop").addEventListener("click", async () => {
try {
setStatus("Stopping…");
const res = await send("FB_CLEANER_STOP");
setStatus(res?.ok ? "Stop requested" : res?.error || "Failed");
} catch (e) {
setStatus(e.message || String(e));
}
});