Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a popup field to edit the default message #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wtf",
"version": "3.4.1",
"version": "3.4.2",
"description": "WTF Chrome Extension that allows you to send messages in bulk via WhatsApp™ Web",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@
"message": "Don't forget to add the area code for each contact.",
"description": "Prefix footer note popup log"
},
"messagePlaceholderPopup": {
"contactsPlaceholderPopup": {
"message": "Add the list of numbers for message sending, separated by commas or each number on a new line.",
"description": "Placeholder of the contacts field of the popup"
},
"messagePlaceholderPopup": {
"message": "Write a message.",
"description": "Placeholder of the message field of the popup"
},
"defaultMessage": {
Expand Down
6 changes: 5 additions & 1 deletion public/_locales/pt_BR/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@
"message": "Não se esqueça de adicionar o prefixo DDD da região de cada contato.",
"description": "Log do popup de nota de rodapé de prefixo"
},
"messagePlaceholderPopup": {
"contactsPlaceholderPopup": {
"message": "Adicione a lista de números para o envio das mensagens, separados por vírgula ou cada número em uma nova linha.",
"description": "Placeholder do campo de contatos do popup"
},
"messagePlaceholderPopup": {
"message": "Escreva sua mensagem.",
"description": "Placeholder do campo de mensagem do popup"
},
"defaultMessage": {
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "__MSG_appName__",
"version": "3.4.1",
"version": "3.4.2",
"action": {
"default_icon": {
"16": "icons/wtf16.png",
Expand Down
28 changes: 23 additions & 5 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { ChromeMessageTypes } from './types/ChromeMessageTypes';

const PopupMessageManager = new AsyncChromeMessageManager('popup');

class Popup extends Component<{}, { contacts: string, duplicatedContacts: number, status?: QueueStatus, confirmed: boolean }> {
class Popup extends Component<{}, { contacts: string, message: string, duplicatedContacts: number, status?: QueueStatus, confirmed: boolean }> {
constructor(props: {}) {
super(props);
this.state = {
contacts: '',
message: '',
duplicatedContacts: 0,
status: undefined,
confirmed: true,
Expand All @@ -32,6 +33,7 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number
messagesNotSentPopup = chrome.i18n.getMessage('messagesNotSentPopup');
prefixFooterNotePopup = chrome.i18n.getMessage('prefixFooterNotePopup');
messagePlaceholderPopup = chrome.i18n.getMessage('messagePlaceholderPopup');
contactsPlaceholderPopup = chrome.i18n.getMessage('contactsPlaceholderPopup');
cancelButtonLabel = chrome.i18n.getMessage('cancelButtonLabel');
okButtonLabel = chrome.i18n.getMessage('okButtonLabel');
optionsButtonLabel = chrome.i18n.getMessage('optionsButtonLabel');
Expand All @@ -48,6 +50,11 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number

this.updateStatus();
this.queueStatusListener = window.setInterval(this.updateStatus, 100);

chrome.storage.local.get(
{ message: this.defaultMessage },
data => { this.setState({ message: data.message }); }
);
}

updateStatus = () => {
Expand All @@ -60,7 +67,7 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number
clearInterval(this.queueStatusListener);
}

componentDidUpdate(prevProps: Readonly<{}>, prevState: Readonly<{ contacts: string, status?: QueueStatus, confirmed: boolean }>, snapshot?: any) {
componentDidUpdate(prevProps: Readonly<{}>, prevState: Readonly<{ contacts: string, message: string, status?: QueueStatus, confirmed: boolean }>, snapshot?: any) {
if (!prevState.status?.isProcessing && this.state.status?.isProcessing) {
this.setState({ confirmed: false });
}
Expand All @@ -70,6 +77,10 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number
this.setState({ contacts: event.target.value.replace(/[^\d\n\t,;]*/g, '') });
}

handleMessageChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
this.setState({ message: event.target.value });
}

parseContacts = (prefix: number) => {
const prefixToString = (prefix === 0 ? '' : prefix).toString();
const contactList = this.state.contacts.split(/[\n\t,;]/).filter(str => str.trim() !== '');
Expand All @@ -90,10 +101,10 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number

handleSubmit = (event: FormEvent<HTMLFormElement>) => {
const language = chrome.i18n.getUILanguage();
chrome.storage.local.get({ message: this.defaultMessage, attachment: null, buttons: [], delay: 0, prefix: language === 'pt_BR' ? 55 : 0 }, async data => {
chrome.storage.local.get({ attachment: null, buttons: [], delay: 0, prefix: language === 'pt_BR' ? 55 : 0 }, async data => {
let i = 0;
for (const contact of this.parseContacts(data.prefix)) {
PopupMessageManager.sendMessage(ChromeMessageTypes.SEND_MESSAGE, { contact, message: data.message, attachment: data.attachment, buttons: data.buttons, delay: data.delay });
PopupMessageManager.sendMessage(ChromeMessageTypes.SEND_MESSAGE, { contact, message: this.state.message, attachment: data.attachment, buttons: data.buttons, delay: data.delay });
}
});
event.preventDefault();
Expand Down Expand Up @@ -156,11 +167,18 @@ class Popup extends Component<{}, { contacts: string, duplicatedContacts: number
</Box>}
{this.state.confirmed && <form onSubmit={this.handleSubmit}>
<Box className="w-96 h-96" bodyClassName="p-4" footer={this.prefixFooterNotePopup}>
<ControlTextArea
className="flex-auto"
value={this.state.message}
onChange={this.handleMessageChange}
placeholder={this.messagePlaceholderPopup}
required
/>
<ControlTextArea
className="flex-auto"
value={this.state.contacts}
onChange={this.handleChange}
placeholder={this.messagePlaceholderPopup}
placeholder={this.contactsPlaceholderPopup}
required
/>
<div className="flex justify-between items-center">
Expand Down