ulixee/secret-agent

How to set the client plugin on the handler.

tienne opened this issue · 4 comments

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?

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

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);
        }
    });

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);

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.