Skip to content

Commit

Permalink
update v1.3
Browse files Browse the repository at this point in the history
add: context menu items
  • Loading branch information
Sudo-Ivan committed Sep 22, 2024
1 parent 64f7355 commit 09da1ca
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 31 deletions.
103 changes: 74 additions & 29 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,80 @@
chrome.runtime.onInstalled.addListener(() => {
const defaultWordlist = {
"bypassing censorship is the best": "veggies are good for you"
};
chrome.storage.local.set({ wordlist: defaultWordlist });
const defaultWordlist = {
"bypassing censorship is the best": "veggies are good for you"
};
chrome.storage.local.set({ wordlist: defaultWordlist });

chrome.contextMenus.create({
id: "encrypt",
title: "Encrypt selected text",
contexts: ["selection"]
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "encrypt" || request.action === "decrypt") {
chrome.storage.local.get("wordlist", (data) => {
processMessage(data.wordlist || {}, request, sendResponse);
});
return true;
}

chrome.contextMenus.create({
id: "decrypt",
title: "Decrypt selected text",
contexts: ["selection"]
});
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "encrypt" || info.menuItemId === "decrypt") {
chrome.storage.local.get("wordlist", (data) => {
processMessage(data.wordlist || {}, {
action: info.menuItemId,
message: info.selectionText
}, (response) => {
openPopupWithResults(tab.id, response);
});
});
}
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "encrypt" || request.action === "decrypt") {
chrome.storage.local.get("wordlist", (data) => {
processMessage(data.wordlist || {}, request, sendResponse);
});
return true;
}
});

function processMessage(wordlist, request, sendResponse) {
let processedMessage = request.message;

function processMessage(wordlist, request, sendResponse) {
let processedMessage = request.message;

if (request.action === "encrypt") {
for (const [key, value] of Object.entries(wordlist)) {
processedMessage = processedMessage.replace(new RegExp(escapeRegExp(key), 'g'), value);
}
sendResponse({ encryptedMessage: processedMessage });
} else {
for (const [key, value] of Object.entries(wordlist)) {
processedMessage = processedMessage.replace(new RegExp(escapeRegExp(value), 'g'), key);
}
sendResponse({ decryptedMessage: processedMessage });
if (request.action === "encrypt") {
for (const [key, value] of Object.entries(wordlist)) {
processedMessage = processedMessage.replace(new RegExp(escapeRegExp(key), 'g'), value);
}
sendResponse({ encryptedMessage: processedMessage });
} else {
for (const [key, value] of Object.entries(wordlist)) {
processedMessage = processedMessage.replace(new RegExp(escapeRegExp(value), 'g'), key);
}
sendResponse({ decryptedMessage: processedMessage });
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function openPopupWithResults(tabId, results, originalMessage) {
chrome.windows.create({
url: chrome.runtime.getURL("html/popup.html"),
type: "popup",
width: 400,
height: 600
}, (popupWindow) => {
chrome.tabs.onUpdated.addListener(function listener(updatedTabId, info) {
if (info.status === 'complete' && updatedTabId === popupWindow.tabs[0].id) {
chrome.tabs.onUpdated.removeListener(listener);
chrome.tabs.sendMessage(popupWindow.tabs[0].id, {
action: "showResults",
results: results,
message: originalMessage
});
}
});
});
}
13 changes: 12 additions & 1 deletion js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function processText(action, inputElement, outputElement) {

const inputText = inputElement.value;

// If input is empty, clear the output and exit
if (!inputText.trim()) {
outputElement.value = '';
isProcessing = false;
Expand Down Expand Up @@ -56,4 +55,16 @@ document.addEventListener('DOMContentLoaded', function() {
window.open(chrome.runtime.getURL('options.html'));
}
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "showResults") {
if (request.results.encryptedMessage) {
outputText.value = request.results.encryptedMessage;
inputText.value = request.message || "";
} else if (request.results.decryptedMessage) {
inputText.value = request.results.decryptedMessage;
outputText.value = request.message || "";
}
}
});
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cryptic Chat",
"version": "1.2",
"version": "1.3",
"author": {
"name": "Sudo-Ivan"
},
Expand Down

0 comments on commit 09da1ca

Please sign in to comment.