-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
33 lines (30 loc) · 974 Bytes
/
background.js
File metadata and controls
33 lines (30 loc) · 974 Bytes
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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "sendWhatsApp",
title: "Send WhatsApp to '%s'",
contexts: ["selection"]
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const rawNumber = info.selectionText.trim();
const formattedNumber = formatNumber(rawNumber);
if (!formattedNumber) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => alert("Invalid phone number!")
});
return;
}
chrome.storage.sync.get("template", (data) => {
const message = data.template ? encodeURIComponent(data.template) : "";
const url = `https://wa.me/${formattedNumber}?text=${message}`;
chrome.tabs.create({ url });
});
});
function formatNumber(number) {
number = number.replace(/\D/g, "");
if (number.length === 10 || number.startsWith("0")) {
number = "880" + number.replace(/^0+/, "");
}
return number.length >= 10 ? number : null;
}