Clusteer
Github Repo
  • โšกIntroduction
  • ๐ŸŽ‰Support
  • Getting Started
    • ๐Ÿ†Benchmarks
    • ๐Ÿš€Installation
      • Docker
      • Laravel Sail (Docker)
    • โœจStarting the server
    • ๐Ÿ™ŒBasic example
  • Client commands
    • ๐Ÿ˜Cookies
    • ๐ŸžDebug console
    • ๐Ÿ‘จโ€๐ŸŽจ Screenshot
    • ๐ŸงตRequest metadata
    • โŒšWaiting for requests
  • Actions
    • #๏ธโƒฃ Keyboard commands
    • ๐ŸMouse commands
  • Advanced
    • ๐ŸŽ€Custom Script
    • ๐Ÿ‘ฉโ€๐Ÿš€ Custom Actions
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Advanced

๐Ÿ‘ฉโ€๐Ÿš€ Custom Actions

PreviousCustom Script

Last updated 3 years ago

Was this helpful?

Besides the built-in actions, you are free to extend them as you like. Before diving in, make sure to check the documentation, as the following example requires custom server.js file.

Creating the class

All classes must extend the \RenokiCo\Clusteer\Actions\Action class and implement the \RenokiCo\Clusteer\Contracts\Actionable interface.

use RenokiCo\Clusteer\Actions\Action;
use RenokiCo\Clusteer\Contracts\Actionable;

class TypeDunnoLolEmoji extends Action implements Actionable
{
    //
}

When processing your action, it requires the input data (such as what it should do), and on the other side, the formatted data that will be sent to the script is going to be placed in a format() method:

use RenokiCo\Clusteer\Actions\Action;
use RenokiCo\Clusteer\Contracts\Actionable;

class TypeDunnoLolEmoji extends Action implements Actionable
{
    protected $selector;

    public function __construct(string $selector)
    {
        $this->selector = $selector;
    }
    
    /**
     * Format to an array that can instructm and be read
     * by the JS script to perform a specific action.
     *
     * @return array
     */
    public function format(): array
    {
        return [
            'name' => 'type-emoji', // required
            'selector' => $this->selector,
            'emoji' => 'ยฏ\_(ใƒ„)_/ยฏ',
        ];
    }
}

One last thing is to check our server.js file and add a new option to handle the action called type-emoji:

await actions.reduce(async (promise, action) => {
  // ...

  if (action.name === 'type-emoji') {
    await page.type(action.selector, action.emoji);
  }
  
  // ...
});

In your PHP code, you may invoke the action() method. Additionally, all classes that extend the Action class will have a static new method that's useful:

Clusteer::to('https://example.com')
    ->action(TypeDunnoLolEmoji::new('input[type="text"]'))
    ->get();
Custom Script