hardkoded/puppeteer-sharp

page.RemoveExposedFunctionAsync() doesn't fully remove function

ravriel opened this issue · 2 comments

Description

page.RemoveExposedFunctionAsync() doesn't fully remove the function.
Trying to expose the function again will fail.

Complete minimal example reproducing the issue

// Arrange
var options = new LaunchOptions { /*  */ };
var chromiumRevision = BrowserFetcher.DefaultRevision;
var browser = await Puppeteer.LaunchAsync(options, chromiumRevision);
var page = await browser.NewPageAsync();

await page.ExposeFunctionAsync("f", () => { });
await page.RemoveExposedFunctionAsync("f");

// This will throw "PuppeteerSharp.PuppeteerException: Failed to add page binding with name f: window['f'] already exists!"
await page.ExposeFunctionAsync("f", () => { });
...

Expected behavior:

Adding a function after removing it should succeed.

Actual behavior:

Exception is thrown:
PuppeteerSharp.PuppeteerException: Failed to add page binding with name f: window['f'] already exists!

Versions

  • Which version of PuppeteerSharp are you using? 18.0.2

  • Which .NET runtime and version are you targeting? .Net 8.0

Additional Information

Fix in RemoveExposedFunctionAsync(string name) :

        if (!_exposedFunctions.TryRemove(name, out var exposedFun) && !_bindings.TryRemove(name, out _))
should be
        if (!_exposedFunctions.TryRemove(name, out var exposedFun) || !_bindings.TryRemove(name, out _))

Thanks for a great project!

Good catch! Do you want to create a PR with the fix?

I will. Thanks