9Hits Macros - Build-in functions

Note that some functions are required await keyword

Access your campaign information (on the viewer only)

You can get some informations of your campaign from TheCampaign object.

{ 
   "id": 123,
   "url": "http://google.com",
   "duration": 15
}
//For example, to get the campaign ID, you can use
TheCampaign.id

Log (msg1[, msg2, msg3, ...]) #

Print one or more message to the Macros Editor output.

Parameters:
  • msg: The text message.
Example:
await WaitForLoading ();
Log("hello", new Date(), EvalScript ("location.href"));


await Delay (ms [, ms2]) #

Pause the script for a period of time in milliseconds

Parameters:
  • ms: Delay time in miliseconds.
Example:
await Delay(3000);//delay for 3 seconds
await Delay(3000, 10000);//delay random between 3-10 seconds

Random (min, max) #

Returns a random integer that is within a specified range.

Parameters:
  • min: The inclusive lower bound of the random number returned.
  • max: The exclusive upper bound of the random number returned. max must be greater than or equal to min.

RandomArray (array) #

Take a random item from an array.

Parameters:
  • array: Source array.
Example:
let randomKeyword = RandomArray(["traffic exchange", "auto surf"]);
Log (randomKeyword);

await ClickByCoordinates (X1, Y1, X2, Y2, nClick=1) #

Click on an area on your website based on coordinates.

Parameters:
  • X1, Y1, X2, Y2: coordinates of the area
    X1, Y1 X2, Y2
  • nClick: number of clicks, the default is 1.
Example:
await ClickByCoordinates(100, 100, 200, 200);
await ClickByCoordinates(100, 100, 200, 200, 2);

You can also get the coordinates from the Macros Editor like this


await ClickBySelector (selector, index=0, nClick = 1, frameSearch="", frameSearchType="") #

Click on an element based on css selector.

Parameters:
  • selector: CSS selector.
  • index: Index of the element in case there are multiple elements matched the selector. You can use number (0 for the first element) or "random" to randomly click an element, default is 0.
  • nClick: number of clicks, the default is 1.
  • frameSearch: If the target element is in an iframe, you have to tell the macros what frame is, empty mean top main frame.
  • frameSearchType: Frame search conditions, possible value: "src-starts", "src-ends", "src-equals", "src-contains", "src-regex", "frame-path", default is "src-contains"
Examples:
//click to a link that contains 9hits.com
await ClickBySelector("a[href*='9hits.com']");

//click to a button with id #submit, inside an iframe which has src starts with http://9hits.com
await ClickBySelector("button#submit", 0, 1, "http://9hits.com", "src-starts");

//click to a button with id #submit, inside an iframe which has src equals to http://9hits.com/contact.html
await ClickBySelector("button#submit", 0, 1, "http://9hits.com/contact.html", "src-equals");

//click to a button with id #submit, inside an iframe which has src ends with /contact.html
await ClickBySelector("button#submit", 0, 1, "/contact.html", "src-ends");

//click to a button with id #submit, inside an iframe which has src contains 9hits.com
await ClickBySelector("button#submit", 0, 1, "9hits.com", "src-contains");

//click to a button with id #submit, inside an iframe which has src matched regex \d+hits\.com
await ClickBySelector("button#submit", 0, 1, "\d+hits\.com", "src-regex");

//click to a button with id #submit, inside the first frame
await ClickBySelector("button#submit", 0, 1, "0", "frame-path");

//click to a button with id #submit, inside the 2nd frame of the first frame
await ClickBySelector("button#submit", 0, 1, "0>1", "frame-path");

Are you confused? Don't worry! You can also generate this click command easily by using the Macros Editor like this


await ClickById (id, nClick=1, frameSearch="", frameSearchType="") #

Click on an element based on its ID.

Parameters: Example:
await ClickById("load-more-button");

await ClickByXpath (xpath, nClick=1, frameSearch="", frameSearchType="") #

Click on an element based on its xpath.

Parameters: Example:
await ClickByXpath("/html/body/div/div[1]/div/ul/li[6]/a/div[1]/span");
await ClickByXpath(GenerateXpath("a", "href", "https://9hits.com%"));

await ClickByTag (tag, index, nClick=1, frameSearch="", frameSearchType="") #

Click on an element based on its tag name.

Parameters:
  • tag: Tag name of the element.
  • index: Index of the element in case there are multiple elements with the same tag name. You can use number (0 for the first element) or "random" to randomly click an element, default is 0.
  • nClick: number of clicks, the default is 1.
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
await ClickByTag("a"); //click to the first link element
await ClickByTag("a", 1); //click to the 2nd link element
await ClickByTag("a", "random"); //click random a link

await ClickByClass (class, index, nClick=1, frameSearch="", frameSearchType="") #

Click on an element based on its class name.

Parameters:
  • class: Class name of the element.
  • index: Index of the element in case there are multiple elements with the same class name. You can use number (0 for the first element) or "random" to randomly click an element, default is 0.
  • nClick: number of clicks, the default is 1.
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
await ClickByClass("skip-button"); //Click to the first element has class name "skip-button"
await ClickByClass("skip-button", 1); //Click to the 2nd element has class name "skip-button"
await ClickByClass("skip-button", "random"); //Click to the random any element has class name "skip-button"

Randomly click any link on your website.

Parameters: Example:
await ClickRandomLink();

Randomly click any internal link on your website.

Parameters: Example:
await ClickRandomInternalLink();

Randomly click any external link on your website.

Parameters: Example:
await ClickRandomExternalLink();

await SetById (id, attr, value, frameSearch="", frameSearchType="") #

Set the value for an attribute of an element based on its ID.

Parameters:
  • id: ID of the element.
  • attr: The attribute you want to set.
  • value: The value you want to set.
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
SetById("email", "value", "[email protected]");

await SetByXpath (xpath, attr, value, frameSearch="", frameSearchType="") #

Set the value for an attribute of an element based on its xpath.

Parameters:
  • xpath: Xpath of the element.
  • attr: The attribute you want to set.
  • value: The value you want to set.
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
await SetByXpath ('//input[@type="email"]', 'value', '[email protected]');

await SetByClass (class, attr, value, index, frameSearch="", frameSearchType="") #

Set the value for an attribute of an element based on its class name.

Parameters:
  • class: Class name of the element.
  • attr: The attribute you want to set.
  • value: The value you want to set.
  • index: Index of the element in case there are multiple elements with the same class name. 0 is the first element, you can also use "random" or "all".
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
SetByClass("text-box", "value", "text value");
SetByClass("text-box", "value", "text value", 2);
SetByClass("text-box", "value", "text value", "random");
SetByClass("text-box", "value", "text value", "all");

await SetByTag (tag, attr, value, index, frameSearch="", frameSearchType="") #

Set the value for an attribute of an element based on its tag name.

Parameters:
  • tag: Tag name of the element.
  • attr: The attribute you want to set.
  • value: The value you want to set.
  • index: Index of the element in case there are multiple elements with the same tag name. 0 is the first element, you can also use "random" or "all".
  • frameSearch: See ClickBySelector.
  • frameSearchType: See ClickBySelector
Example:
await SetByTag("input", "value", "text value");
SetByTag("input", "value", "text value", 2);
SetByTag("input", "value", "text value", "random");
SetByTag("input", "value", "text value", "all");

await EvalScript (jsCode, frameSearch="", frameSearchType="") #

Execute javascript code on the website and return value.

Parameters: Example:
let loc = EvalScript ('window.location');
loc.href="https://9hits.com";
Log(loc);

GenerateXpath (tag, attr, value, index) #

Create xpath to target the element. You can combine this function with ClickByXpath, SetByXpath or any function which use xpath.

Parameters:
  • tag: Tag name of the element.
  • attr: The attribute of the element.
  • value: The value of the element.
  • index: The index of the element.
Example:
//a link has href = "http://google.com"
GenerateXpath("a", "href", "http://google.com");

//a link has href starts with "http://google"
GenerateXpath("a", "href", "http://google%");

//a link has href ends with "google.com"
GenerateXpath("a", "href", "%google.com");

//a link has href contains "google"
GenerateXpath("a", "href", "%google%");

await GetAttribute (xpath, attr, frameSearch="", frameSearchType="") #

Get the value of an attribute of an element based on its xpath.

Parameters: Example:
let result = GetAttribute ('//input[@type="email"]', 'value');
Log (result);

await GetElementPos (jsCode, frameSearch="", frameSearchType="") #

Get the coordinates of an element.

Parameters: Example:
let pos = GetElementPos ("document.getElementById('submit-button');");
//Return: object {X1: 100, Y1: 100, X2:200, Y2: 200}
if(pos) {
    ClickByCoordinates(pos.X1, pos.Y1, pos.X2, pos.Y2);
}

await GetBrowserSize () #

Return the browser size.

Example:
let size = await GetBrowserSize();
//Return: object {width: 1000, height: 800}

await GetSource () #

Return the source code of the website.


await GetHtml () #

Return the HTML code of the website.


await GetUrl () #

Return the current URL.


GetUserAgent () #

Return the current User-Agent.


await IsLoading () #

Indicates that the browser is loading or not.


IsMobile () #

Indicates that the browser is mobile or not (base on the User-Agent).


await IsHideBrowser () #

Indicates the browser visibility.


await WaitForLoading (timeout=0) #

Wait until the browser has finished loading, return true if browser is fully loaded, false if timedout (if timeout is passed).

Parameters:
  • timeout: In seconds, 0 mean unlimited

Navigate to an url

Parameters:
  • url: The URL
  • referrer: Spoof referrer url
Example:
Navigate("https://google.com/");
await WaitForLoading();
Navigate("https://9hits.com/", "https://google.com/");

await GetDuration () # [ Only available on the 9Hits Viewer]

Returns the maximum duration in seconds that the viewer will view your website.


await GetViewedDuration () # [ Only available on the 9Hits Viewer]

Returns the total time in seconds that the viewer viewed your website.


await GetMaxPopups () # [ Only available on the 9Hits Viewer]

Returns the maximum number of popups your site may be allowed to open, if the viewer is disabled popups by the owner, the return value will be 0


await GetConnectionType () # [ Only available on the 9Hits Viewer]

Returns the connection type of the viewer. Possible values are: system, http, socks4, socks5, ssh.


GetImageByXpath (xpath, frameSearch="", frameSearchType="") #

Get an image in base64 format based on its xpath.

Parameters:
await _2CaptchaSolve (params, timeout) #

Solve captcha by 2Captcha service. If you want to use another provider that is similar with 2captcha, you can set the api endpoint to _2CaptchaServer.

Parameters: Example:
//_2CaptchaServer = "http://a-similar-2captcha.com";
let captBase64 = await GetImageByXpath('//*[@id="CAPTCHA"]');
let result = await _2CaptchaSolve({
    'key' : '2CAPTCHA_API_KEY', //replace with your 2Captcha API Key
    'method' : 'base64',
    'json' : 1,
    'body': captBase64
});
Log("Result is:", result.request);
//Example Result: {status:1, request: VMXKDG, captchaId: 1241352612}

await _2CaptchaReportBad (key, captchaId) #

If you believe that captcha is resolved incorrectly, you can use this function to report it. However do not abuse.

Parameters:
  • key: Your 2Captcha API Key.
  • captchaId: ID of the captcha returned by the _2CaptchaSolve function.
Example:
let captBase64 = await GetImageByXpath('//*[@id="CAPTCHA"]');
let result = await _2CaptchaSolve({
    'key' : '2CAPTCHA_API_KEY', //replace with your 2Captcha API Key
    'method' : 'base64',
    'json' : 1,
    'body': captBase64
});
//do something...
await _2CaptchaReportBad('2CAPTCHA_API_KEY', result.captchaId);

await ACSolve (params, timeout) #

Solve captcha by anti-captcha service. If you want to use another provider that is similar with anti-captcha, you can set the api endpoint to AntiCaptchaServer.
Demo .

Parameters: Example:
//AntiCaptchaServer = "http://a-similar-anti-captcha.com";
let image = await GetImageByXpath('//*[@id="captcha_one"]');
let result = await ACSolve({
    "clientKey":"Your anti-captcha API Key",
    "task":
    {
        "type":"ImageToTextTask",
        "body":image,
        "phrase":false,
        "case":false,
        "numeric":false,
        "math":0,
        "minLength":0,
        "maxLength":0
    }
}, 150);
Example Result:
{
    "taskId":1234567,
    "errorId":0,
    "status":"ready",
    "solution":
        {
            "text":"deditur",
            "url":"http:\/\/61.39.233.233\/1\/147220556452507.jpg"
        },
    "cost":"0.000700",
    "ip":"46.98.54.221",
    "createTime":1472205564,
    "endTime":1472205570,
    "solveCount":"0"
}

await ACReportIncorrectImage(key, taskId) #

If you believe that the image captcha is resolved incorrectly, you can use this function to report it. However do not abuse.

Parameters: Example:
let image = await GetImageByXpath('//*[@id="captcha_one"]');
let result = await ACSolve({
    "clientKey":"Your anti-captcha API Key",
    "task":
    {
        "type":"ImageToTextTask",
        "body":image,
        "phrase":false,
        "case":false,
        "numeric":false,
        "math":0,
        "minLength":0,
        "maxLength":0
    }
}, 150);
...
//do something | submit captcha...
...
if(somehow you know that the captcha was incorrect)
{
    ACReportIncorrectImage("Your anti-captcha API Key", result.taskId);
}

await ACReportIncorrectRecaptcha(key, taskId) #

If you believe that the reCaptcha is resolved incorrectly, you can use this function to report it. However do not abuse.

Parameters: Example:
var result = await ACSolve({
    "clientKey": "Your anti-captcha API Key",
    "task":
    {
        "type":"NoCaptchaTaskProxyless",
        "websiteURL":"https://9hits.com",
        "websiteKey":"6LdPp08UAAAAADi4dE6frVDXCv2CgESTpcscb_LS"
    }
}, 600);
...
//do something | submit captcha...
...
if(somehow you know that the reCaptcha was incorrect)
{
    ACReportIncorrectRecaptcha("Your anti-captcha API Key", result.taskId);
}

TryToCallRecaptchaCallBack (result) #

Try to execute recaptcha callback


TabCount () #

Returns the number of opened tabs.


await Typing (text[, speed1, speed2]) #

Simulate typing

Parameters:
  • text: Text to type.
  • speed1 - speed2: Delay time range to adjust typing speed (in miliseconds).
Example:
await Typing ("Hello 9Hits");
await Typing ("Hello 9Hits", 300, 500);

await ResizeTo (width, height) #

Change the browser size.

Parameters:
  • width: New width.
  • height: New height.

ScrollTo (X, Y) #

Scroll the browser to the specified coordinates.

Parameters:
  • X: X coordinates.
  • Y: Y coordinates.

await TabFocus (target) #

Focusing on the specified tab, macros will be executed on the focused tab after this function call

Parameters:
  • target: Can be a tab index (1 is the first tab) or a search pattern by URL.
Example:
await WaitForLoading();
EvalScript('open("https://google.com")');
EvalScript('open("https://facebook.com")');
await Delay(2000);
EvalScript('document.write("this is main tab")');
TabFocus(2); //focus to the 2nd tab
await Delay(1000);
EvalScript('document.write("this 2nd tab")');
TabFocus("facebook.com"); //focus to the tab which has url contains "facebook.com"
await Delay(1000);
EvalScript('document.write("this facebook tab")');

//focus to the first tab that does not contains facebook.com
TabFocus("!facebook.com");

await SendKeyDown (keyCode) #

Simulate keydown event.

Parameters:
  • keyCode: Key Code.

await SendKeyUp (keyCode) #

Simulate keyup event.

Parameters:
  • keyCode: Key Code.

await SendKeyChar (keyCode) #

Simulate keychar event.

Parameters:
  • keyCode: Key Code.

await SendKeyPress (keyCode) #

Simulate keypress event.

Parameters:
  • keyCode: Key Code.

await SendMouseClick (x, y, button) #

Simulate mouseclick event.

Parameters:
  • x: X coordinates.
  • y: Y coordinates.
  • button: can be "left", "right" or "middle"

await SendMouseMove (x, y) #

Simulate mousemove event.

Parameters:
  • x: X coordinates.
  • y: Y coordinates.

Parameters:
  • deltaX: Delta X.
  • deltaX: Delta Y.
Example:
await SendMouseWheel(0, -120); //scroll down 120px
await Delay(3000);
await SendMouseWheel(0, 120); //scroll up 120px

Exit () #

Close the browser immediately.


await HttpRequest (url, params, referrer, headers) # [ Only available on the 9Hits Traffic Bot]

Make HTTP Request

Parameters:
  • url: URL to send request.
  • params: Post Parameters.
  • referrer: Referrer URL.
  • headers: Custom HTTP headers.
Example:
let test1 = await HttpRequest("http://google.com");
let test2 = await HttpRequest("http://google.com", {"post_data" : "xxx", "other_data", "yyy"});
let test3 = await HttpRequest("http://google.com", {"post_data" : "xxx", "other_data", "yyy"}, {"custom-header", "custom-value"});

await ReadFile (path) # [ Only available on 9Hits the Traffic Bot]

Read and return the contents of a file as text

Parameters:
  • path: Path to file.
Example:
let test = await ReadFile("C:\\test.txt");
Log(test);

await WriteFile (path, content, mode) # [ Only available on the 9Hits Traffic Bot]

Write text to a file

Parameters:
  • path: Path to file.
  • content: Text content.
  • mode: Can be "override" or "append".
Example:
await WriteFile("C:\\test.txt", "Hello");
await WriteFile("C:\\test.txt", "Hello", "append");

await GetCurrentDir () # [ Only available on the 9Hits Traffic Bot]

Returns the current path that 9Hits Traffic Bot is running


await GetDownloadedFiles () # [ Only available on the 9Hits Traffic Bot]

Returns the array of downloaded filenames


await IsFileDownloaded (fileName) # [ Only available on the 9Hits Traffic Bot]

Check if the given file has been downloaded

Parameters:
  • fileName: Name of the file to check.

SetUploadFileOrFolder (files) # [ Only available on the 9Hits Traffic Bot]

Set the files will be selected when you click to a Chose File button on a web-page.

Parameters:
  • files: a path to file/folder or an array of paths
Example:
SetUploadFileOrFolder("C:\\test.txt");
SetUploadFileOrFolder("C:\\myfiles");
SetUploadFileOrFolder(["C:\\test_1.txt", "C:\\test_2.txt"]);

Want a new function?

Feel free to request a new function here.

By using our site, you acknowledge that you have read and understood our Cookie Policy, Privacy Policy and our Terms of Service