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

Removed use of tabs.executeScript() and updated manifest files #83

Merged
merged 1 commit into from
Oct 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
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 12,
"ecmaVersion": 14,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
Expand All @@ -10,7 +10,7 @@
"env": {
"browser": true,
"webextensions": true,
"es2021": true
"es2023": true
},
"extends": "eslint:recommended",
"rules": {
Expand Down
7 changes: 3 additions & 4 deletions scripts/manifests/chromemanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
"homepage_url": "https://github.com/rugk/unicodify",

"options_ui": {
"page": "options/options.html",
"browser_style": true,
"chrome_style": true
"page": "options/options.html"
},

"background": {
"page": "background/background.html"
"scripts": ["browser-polyfill.js", "background/modules/InstallUpgrade.js", "background/background.js"],
"type": "module"
},
"content_scripts": [
{
Expand Down
8 changes: 4 additions & 4 deletions scripts/manifests/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"homepage_url": "https://github.com/rugk/unicodify",

"options_ui": {
"page": "options/options.html",
"browser_style": true
"page": "options/options.html"
},

"background": {
"page": "background/background.html"
"scripts": ["background/modules/InstallUpgrade.js", "background/background.js"],
"type": "module"
},
"content_scripts": [
{
Expand Down Expand Up @@ -45,7 +45,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "87.0"
"strict_min_version": "112.0"
}
}
}
8 changes: 4 additions & 4 deletions scripts/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"homepage_url": "https://github.com/rugk/unicodify",

"options_ui": {
"page": "options/options.html",
"browser_style": true
"page": "options/options.html"
},

"background": {
"page": "background/background.html"
"scripts": ["background/modules/InstallUpgrade.js", "background/background.js"],
"type": "module"
},
"content_scripts": [
{
Expand Down Expand Up @@ -43,7 +43,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "87.0"
"strict_min_version": "112.0"
}
}
}
8 changes: 4 additions & 4 deletions scripts/manifests/thunderbirdmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"homepage_url": "https://github.com/rugk/unicodify",

"options_ui": {
"page": "options/options.html",
"browser_style": true
"page": "options/options.html"
},

"background": {
"page": "background/background.html"
"scripts": ["background/modules/InstallUpgrade.js", "background/background.js"],
"type": "module"
},
"content_scripts": [
{
Expand Down Expand Up @@ -47,7 +47,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "91.0"
"strict_min_version": "112.0"
}
}
}
2 changes: 1 addition & 1 deletion src/background/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<head>
<meta charset="utf-8">
<!-- async currently disabled, because of https://bugzilla.mozilla.org/show_bug.cgi?id=1506464 -->
<script type="application/javascript" src="../browser-polyfill.js"></script>
<script src="../browser-polyfill.js" type="module"></script>
<script src="./modules/InstallUpgrade.js" type="module" charset="utf-8"></script>
<script async src="background.js" type="module" charset="utf-8"></script>
</head>
Expand Down
8 changes: 4 additions & 4 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ function sendSettings(autocorrect) {
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
autocorrections: autocorrections,
longest: longest,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
antipatterns: IS_CHROME ? antipatterns.source : antipatterns
}
Expand Down Expand Up @@ -260,8 +260,8 @@ browser.runtime.onMessage.addListener((message) => {
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
autocorrections: autocorrections,
longest: longest,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
antipatterns: IS_CHROME ? antipatterns.source : antipatterns
};
Expand Down
8 changes: 4 additions & 4 deletions src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ function handleMenuChoosen(info, tab) {
return;
}

browser.tabs.executeScript(tab.id, {
code: `insertIntoPage(${JSON.stringify(output)});`,
frameId: info.frameId
});
browser.tabs.sendMessage(tab.id, {
type: COMMUNICATION_MESSAGE_TYPE.INSERT,
text: output
}, { frameId: info.frameId });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/common/modules/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function showNotification(title, content, substitutions, requiredNotifica
browser.notifications.create({
type: "basic",
iconUrl: browser.runtime.getURL(ICON),
title: title,
title,
message: content
});
}
Expand All @@ -59,7 +59,7 @@ async function init() {

init();

BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS, async (request) => {
BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS, (request) => {
const notifications = request.optionValue;

SEND = notifications.send;
Expand Down
3 changes: 2 additions & 1 deletion src/common/modules/data/BrowserCommunicationTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export const COMMUNICATION_MESSAGE_TYPE = Object.freeze({
AUTOCORRECT_CONTENT: "autocorrectContent",
UNICODE_FONT: "unicodeFont",
UPDATE_CONTEXT_MENU: "updateContextMenu",
NOTIFICATIONS: "notifications"
NOTIFICATIONS: "notifications",
INSERT: "insert"
});
58 changes: 33 additions & 25 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const constants = Object.freeze({
// communication type
// directly include magic constant as a workaround as we cannot import modules in content scripts due to https://bugzilla.mozilla.org/show_bug.cgi?id=1451545
const AUTOCORRECT_CONTENT = "autocorrectContent";
const INSERT = "insert";

let insertedText; // Last insert text
let deletedText; // Last deleted text
Expand Down Expand Up @@ -272,7 +273,7 @@ function firstDifferenceIndex(a, b) {
*/
function autocorrect(event) {
// console.log('beforeinput', event.inputType, event.data);
if (!(event.inputType === "insertText" || event.inputType === "insertCompositionText" || event.inputType === "insertParagraph" || event.inputType === "insertLineBreak")) {
if (!["insertText", "insertCompositionText", "insertParagraph", "insertLineBreak"].includes(event.inputType)) {
return;
}
if (!symbolpatterns) {
Expand All @@ -287,7 +288,7 @@ function autocorrect(event) {
if (caretposition) {
const value = target.value || target.innerText;
let deletecount = 0;
let insert = event.inputType === "insertLineBreak" || event.inputType === "insertParagraph" ? "\n" : event.data;
let insert = ["insertLineBreak", "insertParagraph"].includes(event.inputType) ? "\n" : event.data;
const inserted = insert;
let output = false;
// Use Unicode smart quotes
Expand All @@ -311,8 +312,9 @@ function autocorrect(event) {
const aregexResult = symbolpatterns.exec(text);
const aaregexResult = antipatterns.exec(text);
if (!aaregexResult && (!aregexResult || (caretposition <= longest ? regexResult.index < aregexResult.index : regexResult.index <= aregexResult.index))) {
insert = autocorrections[regexResult[0]] + inserted;
deletecount = regexResult[0].length;
const [autocorrection] = regexResult;
insert = autocorrections[autocorrection] + inserted;
deletecount = autocorrection.length;
output = true;
}
} else {
Expand All @@ -327,11 +329,12 @@ function autocorrect(event) {
const text = value.slice(0, caretposition) + inserted;
const aregexResult = numberRegex.exec(text);
if (!aregexResult) {
const label = outputLabel(regexResult[0], regexResult.groups.fractionpart);
const index = firstDifferenceIndex(label, regexResult[0]);
const [number] = regexResult;
const label = outputLabel(number, regexResult.groups.fractionpart);
const index = firstDifferenceIndex(label, number);
if (index >= 0) {
insert = label.slice(index) + inserted;
deletecount = regexResult[0].length - index;
deletecount = number.length - index;
output = true;
}
}
Expand Down Expand Up @@ -404,24 +407,29 @@ function undoAutocorrect(event) {
* @returns {void}
*/
function handleResponse(message, sender) {
if (message.type !== AUTOCORRECT_CONTENT) {
return;
}
enabled = message.enabled;
quotes = message.quotes;
fracts = message.fracts;
autocorrections = message.autocorrections;
longest = message.longest;
symbolpatterns = IS_CHROME ? new RegExp(message.symbolpatterns, "u") : message.symbolpatterns;
antipatterns = IS_CHROME ? new RegExp(message.antipatterns, "u") : message.antipatterns;
// console.log(message);

if (enabled) {
addEventListener("beforeinput", undoAutocorrect, true);
addEventListener("beforeinput", autocorrect, true);
} else {
removeEventListener("beforeinput", undoAutocorrect, true);
removeEventListener("beforeinput", autocorrect, true);
if (message.type === AUTOCORRECT_CONTENT) {
({
enabled,
quotes,
fracts,
autocorrections,
longest,
symbolpatterns,
antipatterns
} = message);
symbolpatterns = IS_CHROME ? new RegExp(symbolpatterns, "u") : symbolpatterns;
antipatterns = IS_CHROME ? new RegExp(antipatterns, "u") : antipatterns;
// console.log(message);

if (enabled) {
addEventListener("beforeinput", undoAutocorrect, true);
addEventListener("beforeinput", autocorrect, true);
} else {
removeEventListener("beforeinput", undoAutocorrect, true);
removeEventListener("beforeinput", autocorrect, true);
}
} else if (message.type === INSERT) {
insertIntoPage(message.text);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/content_scripts/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ document.addEventListener("selectionchange", () => {
const selection = document.getSelection().toString();
browser.runtime.sendMessage({
type: UPDATE_CONTEXT_MENU,
selection: selection
selection
});
});
8 changes: 4 additions & 4 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"homepage_url": "https://github.com/rugk/unicodify",

"options_ui": {
"page": "options/options.html",
"browser_style": true
"page": "options/options.html"
},

"background": {
"page": "background/background.html"
"scripts": ["background/modules/InstallUpgrade.js", "background/background.js"],
"type": "module"
},
"content_scripts": [
{
Expand Down Expand Up @@ -45,7 +45,7 @@
"browser_specific_settings": {
"gecko": {
"id": "unicodify@rugk.github.io",
"strict_min_version": "87.0"
"strict_min_version": "112.0"
}
}
}
6 changes: 3 additions & 3 deletions src/options/modules/CustomOptionTriggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function applyAutocorrectPermissions(optionValue, option, event) {
// trigger update for current session
browser.runtime.sendMessage({
type: COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_BACKGROUND,
optionValue: optionValue
optionValue
});
}

Expand All @@ -47,7 +47,7 @@ function applyUnicodeFontSettings(optionValue) {
// trigger update for current session
browser.runtime.sendMessage({
type: COMMUNICATION_MESSAGE_TYPE.UNICODE_FONT,
optionValue: optionValue
optionValue
});
}

Expand All @@ -64,7 +64,7 @@ function applyNotificationSettings(optionValue) {
// trigger update for current session
browser.runtime.sendMessage({
type: COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS,
optionValue: optionValue
optionValue
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="../common/common.css">
<link rel="stylesheet" href="options.css">

<script type="application/javascript" src="../browser-polyfill.js"></script>
<script src="../browser-polyfill.js" type="module"></script>
<script async src="./fastLoad.js" type="module"></script>

<script defer src="../common/common.js" type="module"></script>
Expand Down
Loading