Skip to content

Commit

Permalink
fix: show error when yank/paste on chrome (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ueokande authored Mar 19, 2023
1 parent abd423d commit 7d78151
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions script/build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const buildScripts = async (browser) => {
await build({
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.BROWSER': JSON.stringify(browser),
},
entryPoints: {
console: "src/console/index.tsx",
Expand Down
2 changes: 1 addition & 1 deletion src/background/controllers/OperationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export default class OperationController {
props: Record<string, string | number | boolean>;
}
): Promise<void> {
this.operationUseCase.run(ctx, name, props, repeat);
await this.operationUseCase.run(ctx, name, props, repeat);
}
}
11 changes: 9 additions & 2 deletions src/background/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { FindClientImpl } from "./clients/FindClient";
import { ConsoleFrameClientImpl } from "./clients/ConsoleFrameClient";
import { FindRepositoryImpl } from "./repositories/FindRepository";
import { ReadyFrameRepositoryImpl } from "./repositories/ReadyFrameRepository";
import { ClipboardRepositoryImpl } from "./repositories/ClipboardRepository";
import {
FirefoxClipboardRepositoryImpl,
ChromeClipboardRepositoryImpl,
} from "./repositories/ClipboardRepository";
import { PropertySettingsImpl } from "./settings/PropertySettings";
import { SearchEngineSettingsImpl } from "./settings/SearchEngineSettings";
import { TransientSettingsRepotiory } from "./settings/SettingsRepository";
Expand Down Expand Up @@ -43,7 +46,11 @@ container.bind("NavigateClient").to(NavigateClientImpl);
container.bind("ConsoleClient").to(ConsoleClientImpl);
container.bind("ConsoleFrameClient").to(ConsoleFrameClientImpl);
container.bind("ReadyFrameRepository").to(ReadyFrameRepositoryImpl);
container.bind("ClipboardRepository").to(ClipboardRepositoryImpl);
if (process.env.BROWSER === "firefox") {
container.bind("ClipboardRepository").to(FirefoxClipboardRepositoryImpl);
} else {
container.bind("ClipboardRepository").to(ChromeClipboardRepositoryImpl);
}
container.bind("PropertySettings").to(PropertySettingsImpl);
container.bind("SearchEngineSettings").to(SearchEngineSettingsImpl);
container.bind("KeyCaptureClient").to(KeyCaptureClientImpl);
Expand Down
18 changes: 11 additions & 7 deletions src/background/messaging/BackgroundMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import OperationController from "../controllers/OperationController";
import KeyController from "../controllers/KeyController";
import ConsoleController from "../controllers/ConsoleController";
import FindController from "../controllers/FindController";
import ConsoleClient from "../clients/ConsoleClient";
import { ReceiverWithContext } from "../../messaging";
import type { Schema } from "../../messaging/schema/background";
import RequestContext from "./RequestContext";
Expand All @@ -31,7 +32,9 @@ export default class BackgroundMessageListener {
@inject(ConsoleController)
consoleController: ConsoleController,
@inject(FindController)
findController: FindController
findController: FindController,
@inject("ConsoleClient")
private readonly consoleClient: ConsoleClient
) {
this.receiver
.route("background.operation")
Expand Down Expand Up @@ -93,18 +96,19 @@ export default class BackgroundMessageListener {
const style = "background-color: purple; color: white; padding: 4px;";
console.debug("%cRECEIVE%c %s %o", style, "", type, args);
}
const ret = this.receiver.receive(ctx, type, args);
Promise.resolve(ret)

Promise.resolve()
.then(() => this.receiver.receive(ctx, type, args))
.then(sendResponse)
.catch((err) => {
console.error(err);
if (!sender.tab || !sender.tab.id) {
return;
}
chrome.tabs.sendMessage(sender.tab.id, {
type: "console.show.error",
text: err.message,
});
if (typeof err.message !== "string") {
return;
}
this.consoleClient.showError(sender.tab.id, err.message);
});
return true;
}
Expand Down
13 changes: 12 additions & 1 deletion src/background/repositories/ClipboardRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default interface ClipboardRepository {
}

@injectable()
export class ClipboardRepositoryImpl implements ClipboardRepository {
export class FirefoxClipboardRepositoryImpl implements ClipboardRepository {
async read(): Promise<string> {
const value = await navigator.clipboard.readText();
return value;
Expand All @@ -17,3 +17,14 @@ export class ClipboardRepositoryImpl implements ClipboardRepository {
await navigator.clipboard.writeText(value);
}
}

@injectable()
export class ChromeClipboardRepositoryImpl implements ClipboardRepository {
async read(): Promise<string> {
throw new Error("Chrome does not support clipboard access");
}

async write(_value: string): Promise<void> {
throw new Error("Chrome does not support clipboard access");
}
}
2 changes: 0 additions & 2 deletions src/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"sessions",
"storage",
"tabs",
"clipboardRead",
"clipboardWrite",
"notifications",
"bookmarks"
],
Expand Down

0 comments on commit 7d78151

Please sign in to comment.