Skip to content

Commit

Permalink
Merge pull request #203 from ator-dev/1.15
Browse files Browse the repository at this point in the history
Convert scripts into ES modules
  • Loading branch information
ator-dev authored Jan 23, 2025
2 parents dd6a886 + a34679a commit 4e7b3c8
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 269 deletions.
7 changes: 1 addition & 6 deletions pages/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
<meta charset="utf-8">
</head>
<body>
<script src="/dist/include/utility.js"></script>
<script src="/dist/include/pattern-stem.js"></script>
<script src="/dist/include/pattern-diacritic.js"></script>
<script src="/dist/include/util-privileged.js"></script>
<script src="/dist/include/storage.js"></script>
<script src="/dist/pages/options.js"></script>
<script src="/dist/pages/options.mjs" type="module"></script>
</body>
</html>
9 changes: 1 addition & 8 deletions pages/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
<meta charset="utf-8">
</head>
<body>
<script src="/dist/include/utility.js"></script>
<script src="/dist/include/pattern-stem.js"></script>
<script src="/dist/include/pattern-diacritic.js"></script>
<script src="/dist/include/util-privileged.js"></script>
<script src="/dist/include/storage.js"></script>
<script src="/lib/email.min.js"></script>
<script src="/dist/include/page-build.js"></script>
<script src="/dist/pages/popup-build.js"></script>
<script src="/dist/pages/popup-build.mjs" type="module"></script>
</body>
</html>
9 changes: 1 addition & 8 deletions pages/sendoff.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
<meta charset="utf-8">
</head>
<body>
<script src="/dist/include/utility.js"></script>
<script src="/dist/include/pattern-stem.js"></script>
<script src="/dist/include/pattern-diacritic.js"></script>
<script src="/dist/include/util-privileged.js"></script>
<script src="/dist/include/storage.js"></script>
<script src="/lib/email.min.js"></script>
<script src="/dist/include/page-build.js"></script>
<script src="/dist/pages/sendoff-build.js"></script>
<script src="/dist/pages/sendoff-build.mjs" type="module"></script>
</body>
</html>
11 changes: 2 additions & 9 deletions pages/startpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
<meta charset="utf-8">
</head>
<body>
<script src="/dist/include/utility.js"></script>
<script src="/dist/include/pattern-stem.js"></script>
<script src="/dist/include/pattern-diacritic.js"></script>
<script src="/dist/include/util-privileged.js"></script>
<script src="/dist/include/storage.js"></script>
<script src="/lib/email.min.js"></script>
<script src="/dist/include/page-build.js"></script>
<script src="/dist/pages/startpage-build.js"></script>
<script src="/dist/content.js"></script>
<script src="/dist/pages/startpage-build.mjs" type="module"></script>
<script src="/dist/content.mjs" type="module"></script>
</body>
</html>
26 changes: 8 additions & 18 deletions platform/chromium/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Mark My Search",
"description": "Highlight searched keywords. Find matches instantly.",
"version": "1.14.3",
"version": "1.15.0",

"icons": {
"16": "/icons/dist/mms-16.png",
Expand All @@ -24,18 +24,14 @@
],

"background": {
"service_worker": "/dist/background.js"
"type": "module",
"service_worker": "/dist/background.mjs"
},

"content_scripts": [
{
"matches": [ "*://*/*" ],
"js": [
"/dist/include/utility.js",
"/dist/include/pattern-stem.js",
"/dist/include/pattern-diacritic.js",
"/dist/content.js"
],
"js": [ "/dist/entrypoints/content.js" ],
"run_at": "document_start"
}
],
Expand All @@ -56,16 +52,10 @@
"web_accessible_resources": [
{
"resources": [
"/dist/paint.js",
"/icons/arrow.svg",
"/icons/close.svg",
"/icons/search.svg",
"/icons/show.svg",
"/icons/refresh.svg",
"/icons/create.svg",
"/icons/delete.svg",
"/icons/edit.svg",
"/icons/reveal.svg"
"/icons/*.svg",
"/dist/content.mjs",
"/dist/entrypoints/content.js",
"/dist/modules/*.mjs"
],
"matches": [ "*://*/*" ]
}
Expand Down
33 changes: 8 additions & 25 deletions platform/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Mark My Search",
"description": "Highlight searched keywords. Find matches instantly.",
"version": "1.14.3",
"version": "1.15.0",

"browser_specific_settings": { "gecko": { "id": "{3c87dcad-dbbd-4be1-b07b-b6d0739b0aec}" } },

Expand All @@ -28,25 +28,14 @@
],

"background": {
"scripts": [
"/dist/include/utility.js",
"/dist/include/pattern-stem.js",
"/dist/include/pattern-diacritic.js",
"/dist/include/util-privileged.js",
"/dist/include/storage.js",
"/dist/background.js"
]
"type": "module",
"scripts": [ "/dist/background.mjs" ]
},

"content_scripts": [
{
"matches": [ "*://*/*" ],
"js": [
"/dist/include/utility.js",
"/dist/include/pattern-stem.js",
"/dist/include/pattern-diacritic.js",
"/dist/content.js"
],
"js": [ "/dist/entrypoints/content.js" ],
"run_at": "document_start"
}
],
Expand All @@ -64,16 +53,10 @@
"web_accessible_resources": [
{
"resources": [
"/dist/paint.js",
"/icons/arrow.svg",
"/icons/close.svg",
"/icons/search.svg",
"/icons/show.svg",
"/icons/refresh.svg",
"/icons/create.svg",
"/icons/delete.svg",
"/icons/edit.svg",
"/icons/reveal.svg"
"/icons/*.svg",
"/dist/content.mjs",
"/dist/entrypoints/content.js",
"/dist/modules/*.mjs"
],
"matches": [ "*://*/*" ]
}
Expand Down
48 changes: 25 additions & 23 deletions src/background.ts → src/background.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
* Licensed under the EUPL-1.2-or-later.
*/

if (this.importScripts) {
// Required for service workers, whereas event pages use declarative imports.
this.importScripts(
"/dist/include/utility.js",
"/dist/include/pattern-stem.js",
"/dist/include/pattern-diacritic.js",
"/dist/include/util-privileged.js",
"/dist/include/storage.js",
);
}
import type { MatchTerms, HighlightMessage, BackgroundMessage, BackgroundMessageResponse } from "/dist/modules/utility.mjs";
import {
log, assert,
MatchTerm,
CommandType,
messageSendHighlight, parseCommand, sanitizeForRegex,
} from "/dist/modules/utility.mjs";
import { Engine, isTabResearchPage } from "/dist/modules/util-privileged.mjs";
import type {
ResearchInstance, Engines, URLFilter,
StorageSyncValues, StorageSessionValues, StorageLocalValues,
} from "/dist/modules/storage.mjs";
import {
StorageSession, StorageLocal, StorageSync,
storageGet, storageSet, storageInitialize,
optionsRepair,
} from "/dist/modules/storage.mjs";

// DEPRECATE
/**
Expand Down Expand Up @@ -187,7 +194,7 @@ const manageEnginesCacheOnBookmarkUpdate = (() => {
};

return () => {
if (useChromeAPI() || !chrome.bookmarks) {
if (!globalThis.browser || !chrome.bookmarks) {
return;
}
browser.bookmarks.getTree().then(async nodes => {
Expand Down Expand Up @@ -232,12 +239,7 @@ const injectIntoTabs = async () => {
(await chrome.tabs.query({})).filter(tab => tab.id !== undefined).forEach(tab => {
chrome.scripting.executeScript({
target: { tabId: tab.id as number },
files: [
"/dist/include/utility.js",
"/dist/include/pattern-stem.js",
"/dist/include/pattern-diacritic.js",
"/dist/content.js",
],
files: [ "/dist/entrypoints/content.js" ],
}).catch(() => chrome.runtime.lastError); // Read `lastError` to suppress injection errors.
});
};
Expand All @@ -249,7 +251,7 @@ const injectIntoTabs = async () => {
const updateActionIcon = (enabled?: boolean) =>
enabled === undefined
? storageGet("local", [ StorageLocal.ENABLED ]).then(local => updateActionIcon(local.enabled))
: chrome.action.setIcon({ path: useChromeAPI()
: chrome.action.setIcon({ path: !globalThis.browser
? enabled ? "/icons/dist/mms-32.png" : "/icons/dist/mms-off-32.png" // Chromium lacks SVG support for the icon.
: enabled ? "/icons/mms.svg" : "/icons/mms-off.svg"
})
Expand All @@ -269,7 +271,7 @@ const updateActionIcon = (enabled?: boolean) =>
* Registers items to selectively appear in context menus, if not present, to serve as shortcuts for managing the extension.
*/
const createContextMenuItems = () => {
if (useChromeAPI() && chrome.contextMenus.onClicked["hasListeners"]()) {
if (!globalThis.browser && chrome.contextMenus.onClicked["hasListeners"]()) {
return;
}
chrome.contextMenus.removeAll();
Expand Down Expand Up @@ -424,7 +426,7 @@ const pageChangeRespondOld = async (urlString: string, tabId: number) => {
return;
}
if (openerTabId === undefined) {
if (!useChromeAPI()) { // Must check `openerTabId` manually for Chromium, which may not define it on creation.
if (globalThis.browser) { // Must check `openerTabId` manually for Chromium, which may not define it on creation.
return;
}
openerTabId = (await chrome.tabs.get(tab.id)).openerTabId;
Expand All @@ -442,7 +444,7 @@ const pageChangeRespondOld = async (urlString: string, tabId: number) => {
});

chrome.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
if (useChromeAPI()) {
if (!globalThis.browser) {
// Chromium emits no `tabs` event for tab reload.
if (changeInfo.status === "loading" || changeInfo.status === "complete") {
pageChangeRespondOld((await chrome.tabs.get(tabId)).url ?? "", tabId);
Expand Down Expand Up @@ -543,7 +545,7 @@ const pageChangeRespondOld = async (urlString: string, tabId: number) => {
return;
}
if (openerTabId === undefined) {
if (!useChromeAPI()) { // Must check `openerTabId` manually for Chromium, which may not define it on creation.
if (globalThis.browser) { // Must check `openerTabId` manually for Chromium, which may not define it on creation.
return;
}
openerTabId = (await chrome.tabs.get(tab.id)).openerTabId;
Expand All @@ -568,7 +570,7 @@ const pageChangeRespondOld = async (urlString: string, tabId: number) => {
};

// Note: emitted events differ between Firefox and Chromium.
if (useChromeAPI()) {
if (!globalThis.browser) {
chrome.tabs.onUpdated.addListener(pageEventListener);
} else {
browser.tabs.onUpdated.addListener(pageEventListener, { properties: [ "url", "status" ] });
Expand Down
19 changes: 17 additions & 2 deletions src/content.ts → src/content.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
* Licensed under the EUPL-1.2-or-later.
*/

import type {
CommandInfo,
HighlightDetailsRequest, HighlightMessage, HighlightMessageResponse,
MatchMode, MatchTerms,
} from "/dist/modules/utility.mjs";
import {
assert,
CommandType,
itemsMatch,
MatchTerm,
messageSendBackground, parseCommand, termEquals,
} from "/dist/modules/utility.mjs";
import type { StorageSyncValues, StorageSync } from "/dist/modules/storage.mjs";

type BrowserCommands = Array<chrome.commands.Command>
type HighlightTags = {
reject: ReadonlySet<string>,
Expand Down Expand Up @@ -558,7 +572,6 @@ const focusOnScrollMarkerPaint = (term: MatchTerm | undefined, container: HTMLEl
// Depends on scroll markers refreshed Paint implementation (TODO)
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const focusOnScrollMarker = (term: MatchTerm | undefined, container: HTMLElement, controlsInfo: ControlsInfo) =>
focusOnScrollMarkerClassic(term, container)
;
Expand Down Expand Up @@ -3183,7 +3196,7 @@ const getTermsFromSelection = () => {

return () => {
if (!paintUsePaintingFallback) {
(CSS["paintWorklet"] as PaintWorkletType).addModule(chrome.runtime.getURL("/dist/paint.js"));
CSS["paintWorklet"].addModule(chrome.runtime.getURL("/dist/paint.js"));
}
// Can't remove controls because a script may be left behind from the last install, and start producing unhandled errors. FIXME
//controlsRemove();
Expand Down Expand Up @@ -3388,3 +3401,5 @@ const getTermsFromSelection = () => {
messageHandleHighlightGlobal = messageHandleHighlight;
};
})()();

export type { TermSelectorStyles, HighlightBox };
7 changes: 7 additions & 0 deletions src/entrypoints/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* This file is part of Mark My Search.
* Copyright © 2021-present ‘ator-dev’, Mark My Search contributors.
* Licensed under the EUPL-1.2-or-later.
*/

import(chrome.runtime.getURL("/dist/content.mjs"));
Loading

0 comments on commit 4e7b3c8

Please sign in to comment.