Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Disable profiler when creating screenshots #147

Closed
tacman opened this issue Mar 19, 2024 · 4 comments · Fixed by #166
Closed

Disable profiler when creating screenshots #147

tacman opened this issue Mar 19, 2024 · 4 comments · Fixed by #166
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@tacman
Copy link
Contributor

tacman commented Mar 19, 2024

Description

I can't imagine that anyone would want to see the debug toolbar in the screenshots they're creating.

It can be disabled programmatically:

https://symfony.com/doc/current/profiler.html#enabling-the-profiler-programmatically-or-conditionally

Example

No response

@Spomky Spomky added the enhancement New feature or request label Mar 21, 2024
@Spomky Spomky added this to the 1.2.0 milestone Mar 21, 2024
@Spomky Spomky self-assigned this Mar 21, 2024
@Spomky
Copy link
Member

Spomky commented Mar 21, 2024

I think an event subscriber like as follows should do the job.
Not tested, but basically it disables the profiler if avaialble and if the user agent contains a specific keyword

<?php

namespace App\EventSubscriber;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Profiler\Profiler;

final readonly class MySubscriber implements EventSubscriberInterface
{

    public function __construct(
        #[Autowire(service: 'profiler')]
        private ?Profiler $profiler = null
    ){}

    public static function getSubscribedEvents(): array
    {
        return [
            'kernel.request' => 'onKernelRequest',
        ];
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if ($this->profiler === null) {
            return;
        }

        $userAgent = $event->getRequest()->headers->get('user-agent');
        if ($userAgent === null || !str_contains($userAgent, 'xxxxxxx')) { // <= PUT THE USER-AGENT KEYWORD HERE
            return;
        }

        $this->profiler->disable();
    }

}

@tacman
Copy link
Contributor Author

tacman commented Mar 21, 2024

Great idea. Then the pwa:create:screenshots will set a user-agent?

@Spomky
Copy link
Member

Spomky commented Apr 3, 2024

It looks like it is possible. The user-agent is set to something something HeadelessChrome/something, which is already a clue.

See https://webscraping.ai/faq/symfony-panther/is-there-a-way-to-simulate-different-user-agents-with-symfony-panther

use Facebook\WebDriver\WebDriverCapabilityType;

$customUserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) PWABundle/123.0.0.0 Safari/537.36';
$client = static::createPantherClient([
    'capabilities' => [
        WebDriverCapabilityType::CHROME => [
            'args' => ["--user-agent={$customUserAgent}"],
        ],
    ],
]);

Copy link

github-actions bot commented May 9, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants