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

How to set the client plugin on the handler. #426

Open
tienne opened this issue Jan 17, 2022 · 4 comments
Open

How to set the client plugin on the handler. #426

tienne opened this issue Jan 17, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@tienne
Copy link

tienne commented Jan 17, 2022

How to set the client plugin on the handler.

Don't set it up inside the dispatchAgent.
Is there any way to create a handler?

@blakebyrnes
Copy link
Contributor

Can you share the code you tried to use? Did you try to make it the first command inside each dispatch on your agents?

@tienne
Copy link
Author

tienne commented Jan 17, 2022

The code below is a function that generates a handler.
I want to set the plugin when I create it here.

async createHandler(options: AgentCreateOptions): Promise<CreateHandlerResult> {
    const createOption: AgentCreateOptions = {
      ...AgentFactory.defaultCreateOptions,
      ...options,
    };

    const handler = new Handler({
      host: process.env.coreServer,
      maxConcurrency: 4,
    });

    if (createOption.headless) {
      handler.defaultAgentOptions.showReplay = false;
      handler.defaultAgentOptions.blockedResourceTypes = [
        BlockedResourceType.BlockCssResources,
        BlockedResourceType.BlockImages,
      ];
    }

    handler.defaultAgentOptions.userAgent = '~ chrome 87';
    // this point handler agents set plugins 
    // like handler.use(ExecuteJsPlugin);

    const agent = (await handler?.createAgent()) as Agent;
    agent.use(ExecuteJsPlugin);

    return { handler, agent };
}

In fact, it is not very good to play agent.use every time in business logic.

// ...
    handler.dispatchAgent(async (agent) => {
        // agent.use(ExecuteJsPlugin);
        try {
            const result = await this.brandPerfumes(agent, brandName);
            perfumes.push(...result);
        } catch (e) {
            console.log(`${brandName} brand error:`, e);
        }
    });

@blakebyrnes
Copy link
Contributor

Oh, I see. Your ticket is that you're requesting an ability to set a client plugin on the overall handler. Got it. Makes sense.

BTW, this code is going to create an agent outside of the dispatch process. I don't think it's what you want - in case it wasn't just example code.

   const agent = (await handler?.createAgent()) as Agent;
    agent.use(ExecuteJsPlugin);

@tienne
Copy link
Author

tienne commented Jan 17, 2022

Oh, I see. Your ticket is that you're requesting an ability to set a client plugin on the overall handler. Got it. Makes sense.

Right, that's what I wanted.

BTW, this code is going to create an agent outside of the dispatch process. I don't think it's what you want - in case it wasn't just example code.

The code is generated separately by handler to use a single agent separately.

I wrote the agent to set up the plugin and use it.

@blakebyrnes blakebyrnes added the enhancement New feature or request label Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants