set_init_java_script

set_init_java_script($script_text,$add_file); - add to the current page and call any JS immediately after the entire page (and each page frame) is loaded. This code will be executed in each frame and on the page. This can be useful for changing page objects, such as navigator.appName. (available from version 4.9.34)

The function accepts parameters as input:

  • $script_text – script text. If you need to get the result of the script, it is transmitted by creating additional DOM objects on the page.
  • $add_file – path to the file whose contents will be added to the beginning of the script text (if empty or there is no file, it will not affect anything).

    After testing, the function returns the result of its work to the script :
  • true – completed successfully
  • false – failed

    PHP example: browser->".basename(__FILE__)."
    "; //1 echo "1.Let's set the initial Java Script"; //JS $js_new_naviagtor = " var platformProp = {get: function() {return '%% PLATFORM'; }}; var appName = {get: function() {return '%% APPNAME'; }}; var languageProp = {get: function() {return '%% LANGUAGE'; }}; var languageBrowserProp = {get: function() {return '%% LANGUAGE_BROWSER'; }}; var languageSystemProp = {get: function() {return '%% LANGUAGE_SYSTEM'; }}; var languageUserProp = {get: function() {return '%% LANGUAGE_USER'; }}; var hardwareConcurencyProp = {get: function() {return %% HWC; }}; //Object.defineProperty(window.navigator, 'plugins', {get: function() {return [{name: 'name1', description: 'desc1', version: '2.1'}, {name: 'name2', description: 'desc2', version: '2.1'}];}}); Object.defineProperty(window.navigator, 'platform', platformProp); Object.defineProperty(window.navigator, 'appName', appName); Object.defineProperty(window.navigator, 'language', languageProp); Object.defineProperty(window.navigator, 'browserLanguage', languageBrowserProp); Object.defineProperty(window.navigator, 'systemLanguage', languageSystemProp); Object.defineProperty(window.navigator, 'userLanguage', languageUserProp); Object.defineProperty(window.screen, 'width', {get: function() {return 1024;}}); Object.defineProperty(window.screen, 'height', {get: function() {return 768;}}); Object.defineProperty(window.navigator, 'hardwareConcurrency', hardwareConcurencyProp); window.navigator.javaEnabled = function() {return false;} ;; alert(navigator.plugins.length); "; //change the JS for our needs $js_new_naviagtor = str_replace("%% PLATFORM", "Win64", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% APPNAME", "Netscape", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% LANGUAGE_BROWSER", "ru-RU", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% LANGUAGE_SYSTEM", "ru-RU", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% LANGUAGE_USER", "ru-RU", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% LANGUAGE", "ru-RU", $js_new_naviagtor); $js_new_naviagtor = str_replace("%% HWC", "1", $js_new_naviagtor); echo $browser->set_init_java_script($js_new_naviagtor)."
    "; //2 echo "2.Let's see the changes:"; echo $browser->navigate("whoer.net").""; sleep(2); $anchor->get_by_attribute("class", "btn-more")->focus(); $anchor->get_by_attribute("class", "btn-more")->send_mouse_click(); //end echo "

    "; //Quit $app->quit(); ?>
    Python example: # Additional paths import sys sys.path.insert(0, '../../../Templates PY/') xhe_host = "127.0.0.1:7013" from xweb_human_emulator import * # Start echo("
    browser.xxxxxxxxx
    ") # 1 echo("1.Let's set the initial Java Script") # JS js_new_naviagtor = "\ u000Avar platformProp = {get: function() {return '%% PLATFORM';}}; \ u000Avar appName = {get: function() {return '%% APPNAME';}}; \ u000Avar languageProp = { get: function() {return '%% LANGUAGE';}}; \ u000Avar languageBrowserProp = {get: function() {return '%% LANGUAGE_BROWSER';}}; \ u000Avar languageSystemProp = {get: function() {return ' %% LANGUAGE_SYSTEM ';}}; \ u000Avar languageUserProp = {get: function() {return' %% LANGUAGE_USER ';}}; \ u000Avar hardwareConcurencyProp = {get: function() {return %% HWC;}}; \ u000AObject .defineProperty(window.navigator, 'platform', platformProp); \ u000AObject.defineProperty(window.navigator, 'appName', appName); \ u000AObject.defineProperty(window.navigator, 'language', languageProp); \ u000AObject.defineProperty(window.navigator, 'browserLanguage', languageBrowserProp); \ u000AObject.defineProperty(window.navigator, 'systemLanguage', languageSystemProp); \ u000AObject.defineProperty(window.navigator, 'userLanguage', languageU serProp); \ u000AObject.defineProperty(window.screen, 'width', {get: function() {return 1024; }}); \ u000AObject.defineProperty(window.screen, 'height', {get: function() {return 768;}}); \ u000AObject.defineProperty(window.navigator, 'hardwareConcurrency', hardwareConcurencyProp); \ u000Awindow.navigator.javaEnabled = function() {return false)})) \ u000Aalert(navigator.plugins.length)) \ u000A " # change JS to fit our needs js_new_naviagtor = js_new_naviagtor.replace("%% PLATFORM", "Win64") js_new_naviagtor = js_new_naviagtor.replace("%% APPNAME", "Netscape") js_new_naviagtor = js_new_naviagtor.replace("%% LANGUAGE_BROWSER", "ru-RU") js_new_naviagtor = js_new_naviagtor.replace("%% LANGUAGE_SYSTEM", "ru-RU") js_new_naviagtor = js_new_naviagtor.replace("%% LANGUAGE_USER", "ru-RU") js_new_naviagtor = js_new_naviagtor.replace("%% LANGUAGE", "ru-RU") js_new_naviagtor = js_new_naviagtor.replace("%% HWC", "1") echo(browser.set_init_java_script(js_new_naviagtor), "
    ") # user agent # browser.set_user_agent("Mozilla/5.0(Windows NT 10.0) WOW64) Trident/7.0) .NET4.0C) .NET4.0E) rv: 11.0) like Gecko")) #https: # panopticlick.eff.org/results? & t = 111 & dnt = 111 & a = 111 # fingerprintTable # 522df22d2d7b711e3964d40dea342b0f e2cc11feadb5c4d14da36a05b6c89fdf # 883b0b504c37666be40c1971ea4a159f f7280c6f24f28cbde3a0bf10ff38a036 # 2 echo("2.Let's see the changes:") echo(browser.navigate("whoer.net"), "") sleep(2) anchor.get_by_attribute("class", "btn-more").focus() anchor.get_by_attribute("class", "btn-more").send_mouse_click() # end echo("

    ") # Quit app.quit()
    C# example: #region using using System; using System.Diagnostics; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Threading; using XHE; using XHE.XHE_DOM; using XHE.XHE_System; using XHE.XHE_Window; using XHE.XHE_Web; #endregion class Program: XHEScript { static void Main(string [] args) { //init XHE server = "127.0.0.1:7019"; InitXHE(); //Start echo("
    browser.set_init_java_script
    "); //refresh the page(to re-execute the script) browser.refresh(); //1 step echo("1.Let's set the initial Java Script:"); echo(browser.set_init_java_script(@ " function setAppName(window, appName) { if(window.navigator.appName!= appName) { var appNameProp = {get: function() {return appName; }}; try { Object.defineProperty(window.navigator, 'appName', appNameProp); } catch(e) { window.navigator = Object.create(navigator, {appName: appNameProp}); } } } setAppName(window, 'new app name1'); "," ") +"
    "); //step 2 echo("2.Let's see the appName changes:"); echo(browser.navigate("whoer.net") + ""); sleep(2); anchor.get_by_attribute("class", "btn-more").focus(); anchor.get_by_attribute("class", "btn-more").send_mouse_click(); //end echo("

    "); app.quit(); } }
    Java Script example: //connect the object to control the emulator, if not already connected xhe_host = "127.0.0.1:7010"; echo = require("../../../Templates JS/xweb_human_emulator.js"); //Start echo("
    browser.set_init_java_script
    "); var sInitJS = "\u000Afunction setAppName(window, appName) {if(window.navigator.appName!= appName) {\u000A var appNameProp = {get: function() {return appName; }}; \u000A try {\u000A Object.defineProperty(window.navigator, 'appName', appNameProp); \u000A } catch(e) {\u000A window.navigator = Object.create(navigator, {\u000A appName: appNameProp \u000A }); \u000A } \u000A } \u000A} \u000AsetAppName(window, 'new app name'); "; //1 step echo("1.Let's set the initial Java Script:"); echo(browser.set_init_java_script(sInitJS) + "
    "); //step 2 echo("2.Let's see the appName changes:"); echo(browser.navigate("whoer.net") + ""); browser.sleep(2); anchor.get_by_attribute("class", "btn-more").focus(); anchor.get_by_attribute("class", "btn-more").send_mouse_click(); //end echo("

    "); //Quit app.quit();