Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Patch for __pwInitScripts #52

Open
tonybruess opened this issue Oct 28, 2024 · 5 comments
Open

Patch for __pwInitScripts #52

tonybruess opened this issue Oct 28, 2024 · 5 comments

Comments

@tonybruess
Copy link

tonybruess commented Oct 28, 2024

Creating this issue so I can track it 😄

Need to fix the __pwInitScripts detection introduced in playwright 1.46.1

microsoft/playwright@c9e673c#diff-087773eea292da9db5a3f27de8f1a2940cdb895383ad750c3cd8e01772a35b40R909-R924

@ofirk
Copy link

ofirk commented Nov 5, 2024

Same issue in my side as well, can it cause a detection?
Thanks again for a great repo!

@daniellionel01
Copy link

I got it fixed with the following code:

await browser.addInitScript(() => {
  delete (window as any).__pwInitScripts;
})

@gohoski
Copy link

gohoski commented Jan 2, 2025

I got it fixed with the following code:

await browser.addInitScript(() => {
  delete (window as any).__pwInitScripts;
})

Works only every second try. Is there any solution that 100% works?

@gohoski
Copy link

gohoski commented Jan 3, 2025

Solution via a proxy

    await context.addInitScript(() => {
      // Delete the property if it exists
      delete (window as any).__pwInitScripts;

      // Redefine `Object.getOwnPropertyNames`
      const originalGetOwnPropertyNames = Object.getOwnPropertyNames;
      Object.getOwnPropertyNames = function (obj) {
        const props = originalGetOwnPropertyNames(obj);
        return props.filter((prop) => prop !== '__pwInitScripts');
      };

      // Use a Proxy to handle access to `window`
      const windowHandler = {
        get(target: any, prop: string) {
          if (prop === '__pwInitScripts') {
            return undefined; // Hide the property
          }
          return Reflect.get(target, prop);
        },
        has(target: any, prop: string) {
          if (prop === '__pwInitScripts') {
            return false; // Prevent detection via "in" operator
          }
          return Reflect.has(target, prop);
        },
      };

      const proxiedWindow = new Proxy(window, windowHandler);
      Object.defineProperty(globalThis, 'window', {
        value: proxiedWindow,
        configurable: false,
        writable: false,
      });
    })

@daniellionel01
Copy link

I'd be careful with using proxies. The presence of a proxy can be detected by client bot detector libraries and a proxy on the window object is probably going to ring some alarm bells. Just FYI.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants