Skip to content

Commit

Permalink
fix: configure browser action event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jan 4, 2022
1 parent f8d7cd4 commit 4a05858
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/action/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export default {
if (this.searchModeAction === 'url') {
if (!validateUrl(this.docUrl)) {
this.focusDocUrlInput();
showNotification({messageId: 'error_invalidUrl'});
showNotification({messageId: 'error_invalidPageUrl'});
return;
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/assets/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,18 @@
"description": "Title of the menu item."
},

"error_invalidUrl": {
"message": "The URL is not valid. It must point to a HTTP(S) or FTP page.",
"error_invalidPageUrl": {
"message": "The page URL is not valid. It must point to a HTTP(S) or FTP page.",
"description": "Error message."
},

"error_invalidSearchMode_url": {
"message": "Searching for page URLs is only supported from the browser toolbar popup. Visit the extension's options to select a different search mode.",
"description": "Error message."
},

"error_invalidSearchModeMobile_url": {
"message": "Searching for page URLs is only supported from the browser menu popup. Visit the extension's options to select a different search mode.",
"description": "Error message."
},

Expand Down
29 changes: 8 additions & 21 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
executeFile,
onComplete,
isAndroid,
isMobile,
getActiveTab,
getPlatform
} from 'utils/common';
Expand Down Expand Up @@ -303,7 +304,7 @@ async function initSearch(session, docs) {

async function searchDocument(session, doc, firstBatchItem = true) {
if (!validateUrl(doc.docUrl)) {
await showNotification({messageId: 'error_invalidUrl'});
await showNotification({messageId: 'error_invalidPageUrl'});
return;
}

Expand Down Expand Up @@ -579,14 +580,8 @@ async function setBrowserAction() {
'searchAllEnginesAction'
]);
const enEngines = await getEnabledEngines(options);
const hasListener = browser.browserAction.onClicked.hasListener(
onActionClick
);

if (enEngines.length === 1) {
if (!hasListener) {
browser.browserAction.onClicked.addListener(onActionClick);
}
browser.browserAction.setTitle({
title: getText(
'actionTitle_engine',
Expand All @@ -598,9 +593,6 @@ async function setBrowserAction() {
}

if (options.searchAllEnginesAction === 'main' && enEngines.length > 1) {
if (!hasListener) {
browser.browserAction.onClicked.addListener(onActionClick);
}
browser.browserAction.setTitle({
title: getText('actionTitle_allEngines')
});
Expand All @@ -610,14 +602,8 @@ async function setBrowserAction() {

browser.browserAction.setTitle({title: getText('extensionName')});
if (enEngines.length === 0) {
if (!hasListener) {
browser.browserAction.onClicked.addListener(onActionClick);
}
browser.browserAction.setPopup({popup: ''});
} else {
if (hasListener) {
browser.browserAction.onClicked.removeListener(onActionClick);
}
browser.browserAction.setPopup({popup: '/src/action/index.html'});
}
}
Expand Down Expand Up @@ -675,11 +661,12 @@ async function setPageAction(tabId) {
'searchAllEnginesAction'
]);
const enEngines = await getEnabledEngines(options);
const hasListener = browser.pageAction.onClicked.hasListener(onActionClick);
const hasListener =
browser.pageAction.onClicked.hasListener(onActionButtonClick);

if (enEngines.length === 1) {
if (!hasListener) {
browser.pageAction.onClicked.addListener(onActionClick);
browser.pageAction.onClicked.addListener(onActionButtonClick);
}
browser.pageAction.setTitle({
tabId,
Expand All @@ -694,7 +681,7 @@ async function setPageAction(tabId) {

if (options.searchAllEnginesAction === 'main' && enEngines.length > 1) {
if (!hasListener) {
browser.pageAction.onClicked.addListener(onActionClick);
browser.pageAction.onClicked.addListener(onActionButtonClick);
}
browser.pageAction.setTitle({
tabId,
Expand All @@ -707,12 +694,12 @@ async function setPageAction(tabId) {
browser.pageAction.setTitle({tabId, title: getText('extensionName')});
if (enEngines.length === 0) {
if (!hasListener) {
browser.pageAction.onClicked.addListener(onActionClick);
browser.pageAction.onClicked.addListener(onActionButtonClick);
}
browser.pageAction.setPopup({tabId, popup: ''});
} else {
if (hasListener) {
browser.pageAction.onClicked.removeListener(onActionClick);
browser.pageAction.onClicked.removeListener(onActionButtonClick);
}
browser.pageAction.setPopup({
tabId,
Expand Down

0 comments on commit 4a05858

Please sign in to comment.