Skip to content

Commit

Permalink
fix: an unexpected error when find mode (#335)
Browse files Browse the repository at this point in the history
Firefox throws NS_ERROR_ILLEGAL_VALUE when `window.find()` is called.
This change catch an error.
  • Loading branch information
ueokande authored Feb 10, 2024
1 parent b9ab075 commit 5357d5c
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 97 deletions.
6 changes: 5 additions & 1 deletion src/background/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export default class Application {
private async onFindPortConnect(port: chrome.runtime.Port) {
const tabId = port.sender?.tab?.id;
const frameId = port.sender?.frameId;
if (typeof tabId === "undefined" || typeof frameId === "undefined") {
if (
typeof tabId === "undefined" ||
typeof frameId === "undefined" ||
port.sender?.url === "about:blank"
) {
return;
}

Expand Down
31 changes: 0 additions & 31 deletions src/background/messaging/BackgroundMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default class BackgroundMessageListener {
private readonly receiver: ReceiverWithContext<Schema, RequestContext> =
new ReceiverWithContext();

private readonly consolePorts: { [tabId: number]: chrome.runtime.Port } = {};

constructor(
@inject(SettingsController)
settingsController: SettingsController,
Expand Down Expand Up @@ -63,9 +61,6 @@ export default class BackgroundMessageListener {
this.receiver
.route("settings.validate")
.to(settingsController.validate.bind(settingsController));
this.receiver
.route("console.frame.message")
.to(this.onConsoleFrameMessage.bind(this));
this.receiver
.route("press.key")
.to(keyController.pressKey.bind(keyController));
Expand Down Expand Up @@ -117,31 +112,5 @@ export default class BackgroundMessageListener {
return true;
},
);
chrome.runtime.onConnect.addListener(this.onConnected.bind(this));
}

private onConnected(port: chrome.runtime.Port): void {
if (port.name !== "vimmatic-console") {
return;
}

if (port.sender && port.sender.tab && port.sender.tab.id) {
const id = port.sender.tab.id;
this.consolePorts[id] = port;
}
}

private onConsoleFrameMessage(
{ sender }: RequestContext,
message: any,
): void {
if (typeof sender.tab?.id === "undefined") {
return;
}
const port = this.consolePorts[sender.tab?.id];
if (!port) {
return;
}
port.postMessage(message);
}
}
4 changes: 0 additions & 4 deletions src/console/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ const App: React.FC = () => {
chrome.runtime.onMessage.addListener((message: any) => {
receiver.receive(message.type, message.args);
});
const port = chrome.runtime.connect({ name: "vimmatic-console" });
port.onMessage.addListener((message: any) => {
receiver.receive(message.type, message.args);
});

sendReady();
}, []);
Expand Down
33 changes: 0 additions & 33 deletions src/content/client/ConsoleClient.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/content/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { AddonEnabledRepositoryImpl } from "./repositories/AddonEnabledRepository";
import { AddressRepositoryImpl } from "./repositories/AddressRepository";
import { ClipboardRepositoryImpl } from "./repositories/ClipboardRepository";
import { ConsoleClientImpl } from "./client/ConsoleClient";
import { ConsoleFramePresenterImpl } from "./presenters/ConsoleFramePresenter";
import { FocusPresenterImpl } from "./presenters/FocusPresenter";
import { HintPresenterImpl } from "./presenters/HintPresenter";
Expand All @@ -28,7 +27,6 @@ const container = new Container({ autoBindInjectable: true });
container.bind("AddonEnabledRepository").to(AddonEnabledRepositoryImpl);
container.bind("AddressRepository").to(AddressRepositoryImpl);
container.bind("ClipboardRepository").to(ClipboardRepositoryImpl);
container.bind("ConsoleClient").to(ConsoleClientImpl);
container.bind("ConsoleFramePresenter").to(ConsoleFramePresenterImpl);
container.bind("FocusPresenter").to(FocusPresenterImpl);
container.bind("HintPresenter").to(HintPresenterImpl);
Expand Down
7 changes: 6 additions & 1 deletion src/content/presenters/FindPresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export class FindPresenterImpl implements FindPresenter {

// NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work
// because of same origin policy
return window.find(keyword, caseSensitive, backwards, wrapScan);
try {
return window.find(keyword, caseSensitive, backwards, wrapScan);
} catch (e) {
// Firefox throws NS_ERROR_ILLEGAL_VALUE sometimes
return false;
}
}

clearSelection(): void {
Expand Down
1 change: 0 additions & 1 deletion src/messaging/schema/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Duplex } from "../types";

export type Schema = {
"typeof addon.enabled.response": Duplex<{ enabled: boolean }>;
"console.frame.message": Duplex<{ message: any }>;
"background.operation": Duplex<{
repeat: number;
op: Operation;
Expand Down
24 changes: 0 additions & 24 deletions test/content/mock/MockConsoleClient.ts

This file was deleted.

0 comments on commit 5357d5c

Please # to comment.