How to create script for web extension - grabliapp/scripts GitHub Wiki

Instructions and API

The "grabli" web extension for grabli.app allows you to write a script to automatically interact with any web page opened in a browser. Such a script is programmed in JavaScript, which is ubiquitous and is used to write scripts for user interaction with a website page.

Any script executed in a web extension can work as a pipeline, receiving some data at the input and writing some data at the output. For such operations, there are two tabs "Input data" and "Output data". "Input data" allows you to load a file to work with its content in a script through the functions of the web extension library. For example, you can load the url of pages that need to be bypassed in order to collect some information, or vice versa, place data from a file on the required page. The " Output data" tab contains the results of the data collection script. Also, through the extension interface, you can dynamically receive the "Output data" data for the operation of another script. For example, you can collect all the news links with one script and get the content of the pages in another.

There are two types of scripts, for execution in single user mode and for execution in campaign mode . The second mode allows you to use dynamic parameters that are specified by the advertiser when creating a campaign. Such scripts can be assembled into templates that statements can execute. Thus, you can scale the execution of the "flow" of scripts to solve business problems. For scripts executed in campaign mode , two key points must be programmed - receiving the MC.getParam parameter (see below) and sending the MC.sendStatus status ( see below)

Parameters can be of two types - static and dynamic. The web extension allows you to add an array of parameters, where each parameter object must have three properties - name, description, type. The resulting array must be represented as a JSON string. For example: [{"name": "message", "description": "Text that will be inserted into the form for sending messages", "type": "static"}, {"name": "list", "description": "Profile numbers for which the action will be performed during the message sending cycle", "type": "dynamic"}]

For convenience and to increase the speed of writing a script, it will be useful for a developer to familiarize himself with the functions of the library of the "grabli" web extension.

During the execution of any script of the web extension, the MC variable is added to the page, which is a single entry point for working with the functions of the "grabli" web extension library. Below, wherever there is a _callback parameter, the convention is that if the parameter is not provided, then a Promise is returned.

To work with the DOM of a web page, every time any script is executed, the jQuery library version 3.5.1 is loaded. The library object is available in scripts through the _jQuery variable.

  • To load a file from the "Input" tab, use the MC.loadFile(filename: string [, parseAs: string (json | text)] [, _callback: function]) function, which accepts input one required parameter filename , which is the name of the file that was loaded into the tab. The second argument can take two values โ€‹โ€‹- json or text . By default, the file will be returned in text format.

  • To save a string on the "Output" tab, use the MC.saveRow(title: string, content: string [, _callback: function])) function , which takes two parameters as input - the name collections of strings and the contents of the current string. If the collection name is already in use, then the string will be added to this collection.

  • To get rows from a collection, you need to use the MC.getRows(title: string, [, number: number] [, count: number] [, _callback: function])) passing to the input collection name, line number (number) from the beginning of which it is necessary to return a certain number of lines (count).

  • If you need to clear the entire collection, then you need to use the function MC.saveRow(title: string, "clean", 1 [, _callback: function])) with parameters the name of the collection to clear (title) and the other two parameters must be "clean" and 1.

  • If you need to get the value ("name") of the parameter for the current campaign, you must use the MC.getParam(name [, parseAs: string (json | text)] [, callback: function])

  • If you need to send a status for the "name" parameter for the current campaign, then you need to use MC.sendStatus(name, value [, customStatus: string] [, result: string] [, callback: function]), where name and value are required parameters. You can also set customStatus, result - for example, conversation content, flag - change balance or not.

  • In general, it is convenient to use the format MC.saveRow(title: string, content: string [, rewrite: number (0 | 1)] [, _callback: function])

  • To show the text to the user during script execution, use MC.console(...args)

  • To clear all messages in the user's console MC.clearConsole() If the script is going to be used in the scheduled launch mode, then you need to add console.log ("script is ended") to the code to stop the task, otherwise the script will be time limited and receive a warning.

Helper functions:

  • MC.helpers.getUrl() - returns the href of the page (string).

  • MC.helpers.getHost() - returns the page hostname (string).

  • MC.helpers.toHex(input) - returns hex format based on input string (string).

  • MC.helpers.isMobile() - returns true if running on a mobile device.

  • MC.helpers.getCookie(name) - returns a value by the name of the cookie.

  • MC.helpers.loadScript(url, callback) - attaches an external script to the current page and runs the callback function. For example, you can download a library for working with GraphQl via cdnjs.

  • MC.helpers.generateForm (formDescription, buttonLabel, callback) - Builds a form and returns values โ€‹โ€‹after submission. formDescription is an array of objects with properties: {type (input | select | checkbox), label: string, name: string,? options: [(label: string, name: string, value: string)]} The "options" properties apply only to the "select" type callback returns the values โ€‹โ€‹of all fields in the array.

  • MC.helpers.packCSV(arr, _delim, _sanitize) - returns the packed csv string by "arr" (array or object) with "_delim" (separator, optional, default ",") and safe if _sanitize is true (default true).

  • MC.helpers.unpackCSV(string, _delim) - transforms a string into "_delim" (separator string, optional, default ",") into an array.

  • MC.helpers.getHeaderCSV(object) - get the field names from the incoming object.

  • MC.helpers.filterObject(object, filter) - returns an object with fields specified in the filter parameter or sets an empty string.

  • MC.helpers.flatten(object, _allObjects) - Returns a "flat" object. If _allObjects is true, then all objects will have a depth-indexed path. If properties with a depth of more than 1 will be converted to a string.

Translations

Will be added soon. Please contribute if you can translate on own native language.