Skip to content

Commit

Permalink
fix: context menu
Browse files Browse the repository at this point in the history
Use same popout window
  • Loading branch information
Sudo-Ivan committed Sep 22, 2024
1 parent d99995f commit 3d0aff2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
59 changes: 42 additions & 17 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let popupWindowId = null;

chrome.runtime.onInstalled.addListener(() => {
const defaultWordlist = {
"bypassing censorship is the best": "veggies are good for you"
Expand All @@ -24,7 +26,7 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
action: info.menuItemId,
message: info.selectionText
}, (response) => {
openPopupWithResults(tab.id, response);
openOrUpdatePopupWithResults(response, info.selectionText);
});
});
}
Expand Down Expand Up @@ -59,22 +61,45 @@ 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
});
function openOrUpdatePopupWithResults(results, originalMessage) {
if (popupWindowId === null) {
chrome.windows.create({
url: chrome.runtime.getURL("html/popup.html"),
type: "popup",
width: 400,
height: 600
}, (popupWindow) => {
popupWindowId = popupWindow.id;
chrome.tabs.onUpdated.addListener(function listener(updatedTabId, info) {
if (info.status === 'complete' && updatedTabId === popupWindow.tabs[0].id) {
chrome.tabs.onUpdated.removeListener(listener);
sendResultsToPopup(popupWindow.tabs[0].id, results, originalMessage);
}
});
});
} else {
chrome.windows.get(popupWindowId, { populate: true }, (window) => {
if (chrome.runtime.lastError) {
popupWindowId = null;
openOrUpdatePopupWithResults(results, originalMessage);
} else {
chrome.windows.update(popupWindowId, { focused: true });
sendResultsToPopup(window.tabs[0].id, results, originalMessage);
}
});
}
}

function sendResultsToPopup(tabId, results, originalMessage) {
chrome.tabs.sendMessage(tabId, {
action: "showResults",
results: results,
message: originalMessage
});
}
}

chrome.windows.onRemoved.addListener((windowId) => {
if (windowId === popupWindowId) {
popupWindowId = null;
}
});
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.3",
"version": "1.3.1",
"author": {
"name": "Sudo-Ivan"
},
Expand Down

0 comments on commit 3d0aff2

Please sign in to comment.