Skip to content

Commit

Permalink
Use Messenger in more places (Native Editor, Dev Tools)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Sep 25, 2021
1 parent d6a29a6 commit 97167e5
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 289 deletions.
60 changes: 53 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"webext-content-scripts": "^0.9.0",
"webext-detect-page": "^3.0.2",
"webext-dynamic-content-scripts": "^8.0.0",
"webext-messenger": "^0.7.1",
"webext-messenger": "^0.8.0",
"webext-patterns": "^1.1.1",
"webext-polyfill-kinda": "^0.1.0",
"webextension-polyfill-ts": "^0.26.0"
Expand Down
18 changes: 7 additions & 11 deletions src/background/contextMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import { browser, Menus, Tabs } from "webextension-polyfill-ts";
import { isBackgroundPage } from "webext-detect-page";
import { reportError } from "@/telemetry/logging";
import { noop } from "lodash";
import { handleMenuAction } from "@/contentScript/messenger/api";
import { showNotification } from "@/contentScript/notify";
import {
handleMenuAction,
showNotification,
} from "@/contentScript/messenger/api";
import { ensureContentScript } from "@/background/util";
import { reportEvent } from "@/telemetry/events";
import { getErrorMessage, hasCancelRootCause } from "@/errors";
Expand Down Expand Up @@ -75,22 +77,16 @@ async function dispatchMenu(
args: info,
maxWaitMillis: CONTEXT_MENU_INSTALL_MS,
});
void showNotification(target, {
message: "Ran content menu item action",
className: "success",
});
void showNotification(target, "Ran content menu item action", "success");
} catch (error: unknown) {
if (hasCancelRootCause(error)) {
void showNotification(target, {
message: "The action was cancelled",
className: "info",
});
void showNotification(target, "The action was cancelled", "info");
} else {
const message = `Error processing context menu action: ${getErrorMessage(
error
)}`;
reportError(new Error(message));
void showNotification(target, { message, className: "error" });
void showNotification(target, message, "error");
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/background/devtools/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import { isBackgroundPage } from "webext-detect-page";
import { uuidv4 } from "@/types/helpers";
import { callBackground } from "@/background/devtools/external";
import { ensureContentScript } from "@/background/util";
import * as nativeEditorProtocol from "@/nativeEditor";
import { reactivate } from "@/background/navigation";
import { expectContext, forbidContext } from "@/utils/expectContext";
import { getErrorMessage, isPrivatePageError } from "@/errors";
import { clearDynamicElements } from "@/contentScript/messenger/api";

const TOP_LEVEL_FRAME_ID = 0;

Expand Down Expand Up @@ -184,10 +184,7 @@ export function liftBackground<

async function resetTab(tabId: number): Promise<void> {
try {
await nativeEditorProtocol.clear(
{ tabId, frameId: TOP_LEVEL_FRAME_ID },
{}
);
await clearDynamicElements({ tabId, frameId: TOP_LEVEL_FRAME_ID }, {});
} catch (error: unknown) {
console.warn("Error clearing dynamic elements for tab: %d", tabId, {
error,
Expand Down
39 changes: 18 additions & 21 deletions src/background/devtools/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import { browser, Runtime } from "webextension-polyfill-ts";
import * as contentScriptProtocol from "@/contentScript/devTools";
import * as robotProtocol from "@/contentScript/uipath";
import { Framework, FrameworkMeta } from "@/messaging/constants";
import * as nativeSelectionProtocol from "@/nativeEditor/selector";
import * as nativeEditorProtocol from "@/nativeEditor";
import { PanelSelectionResult } from "@/nativeEditor/insertPanel";
import { Availability } from "@/blocks/types";
import { ReaderTypeConfig } from "@/blocks/readers/factory";
Expand All @@ -37,6 +35,7 @@ import {
removeActionPanel,
showActionPanel,
} from "@/contentScript/messenger/api";
import * as contentScript from "@/contentScript/messenger/api";

export const registerPort = liftBackground(
"REGISTER_PORT",
Expand Down Expand Up @@ -67,15 +66,14 @@ export const ensureScript = liftBackground(

export const readSelectedElement = liftBackground(
"READ_ELEMENT",
(target: Target) => async () => contentScriptProtocol.readSelected(target)
(target: Target) => async () => contentScript.readSelected(target)
);

export const detectFrameworks: (
port: Runtime.Port
) => Promise<FrameworkMeta[]> = liftBackground(
"DETECT_FRAMEWORKS",
(target: Target) => async () =>
contentScriptProtocol.detectFrameworks(target) as Promise<FrameworkMeta[]>
(target: Target) => async () => contentScript.detectFrameworks(target, null)
);

export const cancelSelectElement = liftBackground(
Expand Down Expand Up @@ -113,14 +111,14 @@ export const selectElement = liftBackground(

export const insertButton = liftBackground(
"INSERT_BUTTON",
(target: Target) => async () => nativeEditorProtocol.insertButton(target)
(target: Target) => async () => contentScript.insertButton(target)
);

export const insertPanel: (
port: Runtime.Port
) => Promise<PanelSelectionResult> = liftBackground(
"INSERT_PANEL",
(target: Target) => async () => nativeEditorProtocol.insertPanel(target)
(target: Target) => async () => contentScript.insertPanel(target)
);

export const showBrowserActionPanel = liftBackground(
Expand All @@ -131,42 +129,42 @@ export const showBrowserActionPanel = liftBackground(
export const updateDynamicElement = liftBackground(
"UPDATE_DYNAMIC_ELEMENT",
(target: Target) => async (element: DynamicDefinition) =>
nativeEditorProtocol.updateDynamicElement(target, element)
contentScript.updateDynamicElement(target, element)
);

export const clearDynamicElements = liftBackground(
"CLEAR_DYNAMIC",
(target: Target) => async ({ uuid }: { uuid?: UUID }) =>
nativeEditorProtocol.clear(target, { uuid })
contentScript.clearDynamicElements(target, { uuid })
);

export const enableDataOverlay = liftBackground(
"ENABLE_ELEMENT",
(target: Target) => async (uuid: UUID) =>
nativeEditorProtocol.enableOverlay(target, `[data-uuid="${uuid}"]`)
contentScript.enableOverlay(target, `[data-uuid="${uuid}"]`)
);

export const enableSelectorOverlay = liftBackground(
"ENABLE_SELECTOR",
(target: Target) => async (selector: string) =>
nativeEditorProtocol.enableOverlay(target, selector)
contentScript.enableOverlay(target, selector)
);

export const disableOverlay = liftBackground(
"DISABLE_ELEMENT",
(target: Target) => async () => nativeEditorProtocol.disableOverlay(target)
(target: Target) => async () => contentScript.disableOverlay(target)
);

export const getInstalledExtensionPointIds = liftBackground(
"INSTALLED_EXTENSION_POINT_IDS",
(target: Target) => async () =>
nativeEditorProtocol.getInstalledExtensionPointIds(target)
contentScript.getInstalledExtensionPointIds(target)
);

export const checkAvailable = liftBackground(
"CHECK_AVAILABLE",
(target: Target) => async (availability: Availability) =>
nativeEditorProtocol.checkAvailable(target, availability)
contentScript.checkAvailable(target, availability)
);

export const searchWindow: (
Expand All @@ -175,13 +173,13 @@ export const searchWindow: (
) => Promise<{ results: unknown[] }> = liftBackground(
"SEARCH_WINDOW",
(target: Target) => async (query: string) =>
contentScriptProtocol.searchWindow(target, query)
contentScript.searchWindow(target, query)
);

export const runBlock = liftBackground(
"RUN_BLOCK",
(target: Target) => async (args: contentScriptProtocol.RunBlockArgs) =>
contentScriptProtocol.runBlock(target, args)
contentScript.runBlock(target, args)
);

export const runReaderBlock = liftBackground(
Expand All @@ -193,7 +191,7 @@ export const runReaderBlock = liftBackground(
id: RegistryId;
rootSelector?: string;
}) =>
contentScriptProtocol.runReaderBlock(target, {
contentScript.runReaderBlock(target, {
id,
rootSelector,
})
Expand All @@ -208,25 +206,24 @@ export const runReader = liftBackground(
config: ReaderTypeConfig;
rootSelector?: string;
}) =>
contentScriptProtocol.runReader(target, {
contentScript.runReader(target, {
config,
rootSelector,
})
);

export const uninstallActionPanelPanel = liftBackground(
"UNINSTALL_ACTION_PANEL_PANEL",
// False positive - it's the inner method that should be async
(target) => async ({ extensionId }: { extensionId: UUID }) =>
removeActionPanel(target, extensionId)
);

export const initUiPathRobot = liftBackground(
"UIPATH_INIT",
(target: Target) => async () => robotProtocol.initRobot(target)
(target: Target) => async () => contentScript.initRobot(target)
);

export const getUiPathProcesses = liftBackground(
"UIPATH_GET_PROCESSES",
(target: Target) => async () => robotProtocol.getProcesses(target)
(target: Target) => async () => contentScript.getProcesses(target)
);
8 changes: 2 additions & 6 deletions src/background/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import * as contentScript from "@/contentScript/lifecycle";
import { liftBackground } from "@/background/protocol";
import { browser, WebNavigation } from "webextension-polyfill-ts";
import { reactivateTab } from "@/contentScript/messenger/api";
import { handleNavigate, reactivateTab } from "@/contentScript/messenger/api";
import { notifyTabs } from "@/background/util";

async function historyListener(
details: WebNavigation.OnHistoryStateUpdatedDetailsType
) {
try {
await contentScript.notifyNavigation(
{ tabId: details.tabId, frameId: details.frameId },
{}
);
await handleNavigate(details);
} catch (error: unknown) {
console.warn("Error notifying page navigation", error);
}
Expand Down
8 changes: 4 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import { createSendScriptMessage } from "./messaging/chrome";
import {
DETECT_FRAMEWORK_VERSIONS,
FrameworkMeta,
READ_WINDOW,
SEARCH_WINDOW,
} from "./messaging/constants";
import type { Library } from "@/vendors/libraryDetector/detect";

type ReadSpec = <T extends Record<string, string>>(arg: {
pathSpec: T;
Expand All @@ -36,6 +36,6 @@ export const withSearchWindow = createSendScriptMessage<{ results: unknown[] }>(
SEARCH_WINDOW
);

export const withDetectFrameworkVersions = createSendScriptMessage<Library[]>(
DETECT_FRAMEWORK_VERSIONS
);
export const withDetectFrameworkVersions = createSendScriptMessage<
FrameworkMeta[]
>(DETECT_FRAMEWORK_VERSIONS);
Loading

0 comments on commit 97167e5

Please sign in to comment.