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

ESLint and TSC fixes #142

Merged
merged 3 commits into from
Aug 7, 2023
Merged
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
9 changes: 8 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"compilerOptions": {
"target": "ES6",
"target": "es2021",
"module": "es2021",
"checkJs": true,
"strict": true,
"noImplicitAny": false,
"forceConsistentCasingInFileNames": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": "./src",
"paths": {
"/*": ["./*"],
Expand Down
19 changes: 4 additions & 15 deletions src/background/modules/InstallUpgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @private
* @param {Object} emojiPickerSettings
* @returns {Promise}
* @returns {Promise<void>}
*/
async function upgradeEmojiSet(emojiPickerSettings) {
// change removed emoji sets to best existing one
Expand All @@ -29,7 +29,7 @@ async function upgradeEmojiSet(emojiPickerSettings) {
// eslint-disable-next-line no-case-declarations
const text = "No emoji set upgrade needed.";
console.log(text);
return Promise.reject(new Error(text));
throw new Error(text);
}

console.log("Doing emoji set upgrade.");
Expand All @@ -38,7 +38,6 @@ async function upgradeEmojiSet(emojiPickerSettings) {
});

console.info("Emoji set upgrade successful.", await browser.storage.sync.get());
return Promise.resolve();
}

/**
Expand All @@ -47,7 +46,7 @@ async function upgradeEmojiSet(emojiPickerSettings) {
* @see {@link https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onInstalled}
* @private
* @param {Object} details
* @returns {Promise}
* @returns {Promise<void>}
*/
async function handleInstalled(details) {
// only trigger for usual addon updates
Expand All @@ -62,14 +61,4 @@ async function handleInstalled(details) {
upgradeEmojiSet(oldData.emojiPicker).catch(() => {});
}

/**
* Inits module.
*
* @private
* @returns {void}
*/
function init() {
browser.runtime.onInstalled.addListener(handleInstalled);
}

init();
browser.runtime.onInstalled.addListener(handleInstalled);
17 changes: 9 additions & 8 deletions src/background/modules/OmniboxSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function loadEmojiMart() {
const emojiMartLoader = document.createElement("script");
emojiMartLoader.setAttribute("async", true);
emojiMartLoader.setAttribute("src", "/common/lib/emoji-mart-embed/dist/emoji-mart.js");
document.querySelector("head").appendChild(emojiMartLoader);
document.querySelector("head").append(emojiMartLoader);

emojiMartIsLoaded = true;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export function triggerOmnixboxSuggestion(text, suggest) {
export async function triggerOmnixboxDisabledSearch(text, disposition) {
// if search API is allowed, we just fall-back to default search
if (browser.search) {
let tabId = undefined;
let tabId;

switch (disposition) {
case "currentTab": {
Expand Down Expand Up @@ -138,10 +138,11 @@ export async function triggerOmnixboxDisabledSearch(text, disposition) {
* @public
* @param {string} text the string the user entered or selected
* @param {string} disposition how the result should be possible
* @returns {void}
* @returns {Promise<void>}
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/onInputEntered}
*/
export async function triggerOmnixboxSearch(text, disposition) {
text = text.trim();
const searchResult = window.emojiMart.emojiIndex.search(text);

const emojiSearch = await AddonSettings.get("emojiSearch");
Expand All @@ -154,7 +155,7 @@ export async function triggerOmnixboxSearch(text, disposition) {
if (foundEmoji) {
searchResult.push(foundEmoji);
}
} catch (e) {
} catch {
// ignore errors, as we usually expect text strings there and these are
// totally fine, too; search may find something here
}
Expand All @@ -171,7 +172,7 @@ export async function triggerOmnixboxSearch(text, disposition) {
copyToClipboard: true
});
} else if (emojiSearch.action === "emojipedia") {
const resultUrl = `https://emojipedia.org/search/?q=${emojiText}`;
const resultUrl = `https://emojipedia.org/search/?${new URLSearchParams({ q: emojiText })}`;
rugk marked this conversation as resolved.
Show resolved Hide resolved

// navigate to URL in current or new tab
openTabUrl(resultUrl, disposition);
Expand All @@ -187,7 +188,7 @@ export async function triggerOmnixboxSearch(text, disposition) {
// browser.browserAction.openPopup();

// search for result in emojipedia
const resultUrl = `https://emojipedia.org/search/?q=${text}`;
const resultUrl = `https://emojipedia.org/search/?${new URLSearchParams({ q: text })}`;
openTabUrl(resultUrl, disposition);
}
}
Expand All @@ -197,7 +198,7 @@ export async function triggerOmnixboxSearch(text, disposition) {
*
* @private
* @param {boolean} toEnable
* @returns {void}
* @returns {Promise<void>}
* @throws TypeError
*/
async function toggleEnabledStatus(toEnable) {
Expand All @@ -207,7 +208,7 @@ async function toggleEnabledStatus(toEnable) {
}

// if we do not have the permission for clipboard, and need it for settings, force-disable feature
if (!(await browser.permissions.contains(CLIPBOARD_WRITE_PERMISSION))) {
if (!await browser.permissions.contains(CLIPBOARD_WRITE_PERMISSION)) {
const emojiSearch = await AddonSettings.get("emojiSearch");

if (emojiSearch.action === "copy") {
Expand Down
8 changes: 3 additions & 5 deletions src/common/modules/BrowserCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
*
* @private
* @param {Object} switchBrowser an object with values to return per browser
* @returns {string}
* @returns {Promise<string>}
*/
export async function getBrowserValue(switchBrowser) {
if (browser.runtime.getBrowserInfo) {
const browserInfo = await browser.runtime.getBrowserInfo();

if (browserInfo.name === "Thunderbird") {
return switchBrowser.thunderbird;
} else {
return switchBrowser.firefox;
}
} else {
return switchBrowser.chrome;
return switchBrowser.firefox;
}
return switchBrowser.chrome;
}
10 changes: 5 additions & 5 deletions src/common/modules/EmojiInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as PageHandler from "./PageHandler.js";
* As per users settings, insert emoji into web page or copy to clipboard.
*
* @private
* @param {Object} text
* @param {string} text
* @param {Object} options
* @param {boolean} options.insertIntoPage whether to try to insert it into the active page
* @param {boolean} options.copyOnlyOnFallback whether to fallback to copying emojis (alos requires copyToClipboard=true)
Expand All @@ -16,7 +16,7 @@ export async function insertOrCopy(text, options) {
// destructure config
const {
insertIntoPage,
copyOnlyOnFallback,
copyOnlyOnFallback
} = options;
let copyToClipboard = options.copyToClipboard;

Expand All @@ -30,12 +30,12 @@ export async function insertOrCopy(text, options) {

// wait for successful execution, if wanted
if (insertIntoPage && copyOnlyOnFallback) {
await (emojiInsertResult.then(() => {
await emojiInsertResult.then(() => {
// if successful, do not copy emoji
copyToClipboard = false;
})).catch((e) => {
}).catch((error) => {
// log error just as a warning, as we expect copying can fail
console.warn(e);
console.warn(error);
// but resolve promise, so await/fallback continues
});
}
Expand Down
19 changes: 9 additions & 10 deletions src/common/modules/IconHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const POPUP_ICON_OPTION = "popupIconColored";
* Sets a popup icon variant.
*
* @private
* @param {string} icon version or "null"/"undefined" to reset to default
* @param {string|null} icon version or "null"/"undefined" to reset to default
* @returns {Promise}
*/
function setPopupIcon(icon) {
Expand All @@ -26,22 +26,22 @@ function setPopupIcon(icon) {
const browserAction = typeof messenger !== "undefined" ? browser.composeAction : browser.browserAction;

// ignore request if API is not available
if (browserAction.setIcon === undefined) {
if (!browserAction.setIcon) {
return Promise.resolve();
}

if (icon === null || icon === undefined) {
if (icon == null) {
return browserAction.setIcon({path: null});
}

// set colored icon
if (icon === "colored") {
// WTF: For whatever reason, these paths need to be absolute...
return browserAction.setIcon({path: {
"16": "/icons/icon_32.png",
"32": "/icons/icon_32.png",
"64": "/icons/icon_64.png",
"128": "/icons/icon_128.png"
16: "/icons/icon_32.png",
32: "/icons/icon_32.png",
64: "/icons/icon_64.png",
128: "/icons/icon_128.png"
}});
}

Expand All @@ -58,10 +58,9 @@ function setPopupIcon(icon) {
export function changeIconIfColored(popupIconColored) {
if (popupIconColored === true) {
return setPopupIcon("colored");
} else {
// reset icon
return setPopupIcon(null);
}
// reset icon
return setPopupIcon(null);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/common/modules/LanguageHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@
* @private
* @type {string[]}
*/
const ADDON_TRANSLATED_INTO = [
const ADDON_TRANSLATED_INTO = new Set([
// German
"de", "de-DE",
// Hebrew
// "he", // many untranslated strings
// English
"en", "en-US",
// French
"fr", "fr-FR",
].map((lang) => lang.toLowerCase());
"fr", "fr-FR"
].map((lang) => lang.toLowerCase()));

/**
* Returns whether the user also speaks a language that the add-on is not
* translated into.
*
* @public
* @returns {boolean}
* @returns {Promise<boolean>}
*/
export async function userSpeaksLocaleNotYetTranslated() {
const addonLanguage = browser.i18n.getMessage("@@ui_locale");
const uiLanguage = browser.i18n.getUILanguage();
const acceptedLanguages = await browser.i18n.getAcceptLanguages();

console.log("Addon is translated into", addonLanguage, ", browser into ", uiLanguage, "and user accepts the languages", acceptedLanguages, ".");
console.log("Addon is translated into", addonLanguage, ", browser into", uiLanguage, "and user accepts the languages", acceptedLanguages, ".");
// Note: actually addonLanguage and uiLanguage should be the same, see https://discourse.mozilla.org/t/not-clear-that-there-are-three-locales/27533

// for evaluation, we can assume the user also speaks the language their browser is translated into
acceptedLanguages.push(uiLanguage);

// if the language the user speaks is not already translated, they probably know another locale we do not know yet
return acceptedLanguages.some((userLang) => ! ADDON_TRANSLATED_INTO.includes(userLang.toLowerCase()));
return acceptedLanguages.some((userLang) => !ADDON_TRANSLATED_INTO.has(userLang.toLowerCase()));
}
2 changes: 1 addition & 1 deletion src/common/modules/MobileHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Returns whether the current runtime is a mobile one (true) or not (false).
*
* @public
* @returns {Promise} with Boolean
* @returns {Promise<boolean>} with Boolean
*/
export async function isMobile() {
const platformInfo = await browser.runtime.getPlatformInfo();
Expand Down
2 changes: 1 addition & 1 deletion src/common/modules/PageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function insertIntoPage(text) {
// send request to insert emoji
// This will not work in Manifest V3: https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings
return browser.tabs.executeScript(tab.id, {
code: `insertIntoPage("${text}");`,
code: `insertIntoPage(${JSON.stringify(text)});`,
rugk marked this conversation as resolved.
Show resolved Hide resolved
allFrames: true,
runAt: "document_end"
});
Expand Down
12 changes: 6 additions & 6 deletions src/common/modules/data/MessageLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* Specifies the message level to use,
*
* @readonly
* @enum {int}
* @enum {number}
* @default
*/
export const MESSAGE_LEVEL = Object.freeze({
"ERROR": 3,
"WARN": 2,
"INFO": 1,
"LOADING": -2,
"SUCCESS": -3
ERROR: 3,
WARN: 2,
INFO: 1,
LOADING: -2,
SUCCESS: -3
});
Loading