Skip to content

Conversation

@wogns3623
Copy link
Contributor

FIx #99

I tested it locally but not sure there aren't potential issue

const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
if (!window["webview_bridge_debug_injected"]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!window["webview_bridge_debug_injected"]) {
if (window["webview_bridge_debug_injected"]) {
return;
}

how about use early return ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and It’s a minor detail, but as shown above, generating a random value and using it can be a good idea.
Although the possibility is low, storing it on the window object can cause name collisions.

import { createRandomId } from "@webview-bridge/utils";

const randomId = createRandomId();

export const INJECT_DEBUG = `
{
 const originalConsoleLog = console.log;
 const originalConsoleError = console.error;
 const originalConsoleWarn = console.warn;
 if (!window["webview_bridge_debug_injected_${randomId}"]) { // <<

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the possibility is low, storing it on the window object can cause name collisions.

I was worried about naming collisions too, but couldn't think of a way around it.
This seems like the perfect solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about prefix webview_bridge_debug_injected_?
If there is a naming convention within the project, I will follow it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__bridge_debug_injected_{randomId} 👍

- Use early return
- Append random id after injection detect key for prevent collision
@wogns3623
Copy link
Contributor Author

Here you go 😉

@gronxb
Copy link
Owner

gronxb commented May 7, 2025

Thanks !!

@gronxb gronxb merged commit 1c0c2a9 into gronxb:main May 7, 2025
1 check passed
@wogns3623 wogns3623 deleted the prevent-multiple-injection branch May 7, 2025 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webview console output printed multiple times

2 participants