Jump to content

javascript in a string


Recommended Posts

hi all,

Got my new project starting.

i am playing a site game. where 2 javascript are legal. you are aloud to use them. its for recruiting soldiers.

In the java you can set the amount of soldiers and kinds that you wanne recruit.

now i wanted to make a GUI with tekst boxxes where people can fill in those numbers.

problem now is i can't get the java script in a string ^_^

java script:

java script: var configuration =  [0, 0, 7250, 0, 3000, 0, 300, 30]; var archers; var massmode = (document.URL.match('mode=mass')); var units; var unitsNL; var costs; function arrayMin(array) { var value = array[0]; for (var t = 1; t < array.length; t++) { if (array[t] < value) { value = array[t]; } } return value; } function linearDivideArray(array1, array2) { var value = []; if (array1.length == array2.length) { for (var t = 0; t < array1.length; t++) { if (array2[t] !== 0) { value[t] = array1[t] * 1.0 / array2[t]; } else { value[t] = 0; } } } return value; } function linearAddArray(array1, array2) { var value = []; if (array1.length == array2.length) { for (var t = 0; t < array1.length; t++) { value[t] = array1[t] + array2[t]; } } return value; } function getUnitId(unit) { for (var t = 0; t < units.length; t++) { if (unit == unitsNL[t]) { return t; } } return -1; } function getTagsSubset(tagname, classname) { var subset_t = document.getElementsByTagName(tagname); var subset_c = []; var u = 0; for (var t = 0; t < subset_t.length; t++) { if (subset_t[t].className == classname) { subset_c[u++] = subset_t[t]; } } return subset_c; } function getTable() { var candidates = getTagsSubset('table', 'vis'); var t; for (t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Dorp') { break; } } catch (e) { } } return candidates[t]; } function getQueue(record) { var value = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; if (massmode) { for (var u = 3; u < record.cells.length; u++) { try { value[u - 3] = parseInt(record.cells[u].childNodes[1].childNodes[1].firstChild.title, 10); } catch (e) { } if (isNaN(value[u - 3])) { value[u - 3] = 0; } } } else { var candidates = getTagsSubset('table', 'vis'); var u = 0; for (var t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Opleiding') { for (u = 1; u < candidates[t].rows.length; u++) { var data = candidates[t].rows[u].cells[0].innerHTML.split(' '); value[getUnitId(data[1])] += parseInt(data[0], 10); } } } catch (e) { } } } return value; } function getResource(type) { return parseInt(document.getElementById(type).innerHTML, 10); } function getResources(record) { var value = [0, 0, 0, 0]; var res = record.cells[1].textContent.split("\n"); var farm = record.cells[2].innerHTML.split('/'); value[0] = parseInt(res[1].replace(".", ""), 10); value[1] = parseInt(res[2].replace(".", ""), 10); value[2] = parseInt(res[3].replace(".", ""), 10); value[3] = farm[1] - farm[0]; return value; } function getFarmSpace() { var candidates = getTagsSubset('table', 'box'); var t; for (t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].firstChild.firstChild.title == 'Arbeiders') { break; } } catch (e) { } } var data = candidates[t].rows[0].cells[1].innerHTML.split('/'); return parseInt(data[1], 10) - parseInt(data[0], 10); } function getProduced(record) { var value = [0, 0, 7642, 0, 0, 0, 0, 2000, 250, 0]; if (massmode) { for (var u = 3; u < record.cells.length; u++) { try { value[u - 3] = parseInt(record.cells[u].childNodes[1].textContent.split("\n")[2], 10); } catch (e) { } if (isNaN(value[u - 3])) { value[u - 3] = 0; } } } else { var candidates = getTagsSubset('table', 'vis'); for (var t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Eenheid') { for (var u = 1; u < candidates[t].rows.length - 1; u++) { var data = candidates[t].rows[u].cells[6].innerHTML.split('/'); value[getUnitId(candidates[t].rows[u].cells[0].firstChild.childNodes[2].nodeValue.substr(1))] = parseInt(data[1], 10); } } } catch (e) { } } } return value; } function submitForm() { var candidates = document.getElementsByTagName("input"); var t; for (t = 0; t < candidates.length; t++) { if (candidates[t].type == "submit") { break; } } candidates[t].click(); } if (configuration.length == 10 || configuration.length == 8) { archers = (configuration.length == 10); if (document.URL.match('screen=train')) { var total_costs = [0, 0, 0, 0]; var i, factor, number; var queue = []; var produced = []; var resources = []; if (archers) { units = ['spear', 'sword', 'axe', 'archer', 'spy', 'light', 'marcher', 'heavy', 'ram', 'catapult']; unitsNL = ['Speervechter', 'Zwaardvechter', 'Bijlstrijder', 'Boogschutter', 'Verkenner', 'Lichte cavalerie', 'Bereden boogschutter', 'Zware cavalerie', 'Ram', 'Katapult']; costs = [ [50, 30, 10, 1], [30, 30, 70, 1], [60, 30, 40, 1], [100, 30, 60, 1], [50, 50, 20, 2], [125, 100, 250, 4], [250, 100, 150, 5], [200, 150, 600, 6], [300, 200, 200, 5], [320, 400, 100, 8] ]; } else { units = ['spear', 'sword', 'axe', 'spy', 'light', 'heavy', 'ram', 'catapult']; unitsNL = ['Speervechter', 'Zwaardvechter', 'Bijlstrijder', 'Verkenner', 'Lichte cavalerie', 'Zware cavalerie', 'Ram', 'Katapult']; costs = [ [50, 30, 10, 1], [30, 30, 70, 1], [60, 30, 40, 1], [50, 50, 20, 2], [125, 100, 250, 4], [200, 150, 600, 6], [300, 200, 200, 5], [320, 400, 100, 8] ]; } if (massmode) { var records = getTable().rows; for (r = 1; r < records.length; r++) { var subconf = []; total_costs = [0, 0, 0, 0]; resources = getResources(records[r]); queue = getQueue(records[r]); produced = getProduced(records[r]); for (i = 0; i < units.length; i++) { subconf[i] = configuration[i] - (queue[i] + produced[i]); if (subconf[i] < 0) { subconf[i] = 0; } for (var j = 0; j < 4; j++) { total_costs[j] += costs[i][j] * subconf[i]; } } factor = arrayMin(linearDivideArray(resources, total_costs)); if (factor > 1.0) { factor = 1.0; } for (i = 0; i < units.length; i++) { number = subconf[i] * factor; if (number < 0) { number = 0; } if (records[r].cells[3 + i].childNodes[3]) { var ibox = records[r].cells[3 + i].childNodes[3]; try { ibox.defaultValue = parseInt(number, 10); } catch (e) { } } } } } else { queue = getQueue(); produced = getProduced(); resources = [getResource('wood'), getResource('stone'), getResource('iron'), getFarmSpace()]; for (i = 0; i < units.length; i++) { configuration[i] -= queue[i] + produced[i]; if (configuration[i] < 0) { configuration[i] = 0; } for (var j = 0; j < 4; j++) { total_costs[j] += costs[i][j] * configuration[i]; } } factor = arrayMin(linearDivideArray(resources, total_costs)); if (factor > 1.0) { factor = 1.0; } for (i = 0; i < units.length; i++) { number = configuration[i] * factor; if (number < 0) { number = 0; } if (document.getElementById(units[i])) { document.getElementById(units[i]).defaultValue = parseInt(number, 10); } } } submitForm(); } else { alert('Ongeldige bewerking!\nDeze tool werkt enkel in het massarecruteren-scherm.'); } } else { alert('De configuratie voor de troepenaantallen is niet correct ingesteld.'); }

only thing i need to make variable is this part:

var configuration =  [0, 0, 7250, 0, 3000, 0, 300, 30]

what i tried to do:

$Value1 = 7250
$Value2 = 3000
$Value3 = 300
$Value4 = 30

$tekst = "]; var archers; var massmode = (document.URL.match('mode=mass')); var units; var unitsNL; var costs; function arrayMin(array)  { var value = array[0]; for (var t = 1; t < array.length; t++) { if (array[t] < value) { value = array[t]; } } return value; } function linearDivideArray(array1, array2) { var value = []; if (array1.length == array2.length) { for (var t = 0; t < array1.length; t++) { if (array2[t] !== 0) { value[t] = array1[t] * 1.0 / array2[t]; } else { value[t] = 0; } } } return value; } function linearAddArray(array1, array2) { var value = []; if (array1.length == array2.length) { for (var t = 0; t < array1.length; t++) { value[t] = array1[t] + array2[t]; } } return value; } function getUnitId(unit) { for (var t = 0; t < units.length; t++) { if (unit == unitsNL[t]) { return t; } } return -1; } function getTagsSubset(tagname, classname) { var subset_t = document.getElementsByTagName(tagname); var subset_c = []; var u = 0; for (var t = 0; t < subset_t.length; t++) { if (subset_t[t].className == classname) { subset_c[u++] = subset_t[t]; } } return subset_c; } function getTable() { var candidates = getTagsSubset('table', 'vis'); var t; for (t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Dorp') { break; } } catch (e) { } } return candidates[t]; } function getQueue(record) { var value = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; if (massmode) { for (var u = 3; u < record.cells.length; u++) { try { value[u - 3] = parseInt(record.cells[u].childNodes[1].childNodes[1].firstChild.title, 10); } catch (e) { } if (isNaN(value[u - 3])) { value[u - 3] = 0; } } } else { var candidates = getTagsSubset('table', 'vis'); var u = 0; for (var t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Opleiding') { for (u = 1; u < candidates[t].rows.length; u++) { var data = candidates[t].rows[u].cells[0].innerHTML.split(' '); value[getUnitId(data[1])] += parseInt(data[0], 10); } } } catch (e) { } } } return value; } function getResource(type) { return parseInt(document.getElementById(type).innerHTML, 10); } function getResources(record) { var value = [0, 0, 0, 0]; var res = record.cells[1].textContent.split("\n"); var farm = record.cells[2].innerHTML.split('/'); value[0] = parseInt(res[1].replace(".", ""), 10); value[1] = parseInt(res[2].replace(".", ""), 10); value[2] = parseInt(res[3].replace(".", ""), 10); value[3] = farm[1] - farm[0]; return value; } function getFarmSpace() { var candidates = getTagsSubset('table', 'box'); var t; for (t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].firstChild.firstChild.title == 'Arbeiders') { break; } } catch (e) { } } var data = candidates[t].rows[0].cells[1].innerHTML.split('/'); return parseInt(data[1], 10) - parseInt(data[0], 10); } function getProduced(record) { var value = [0, 0, 7642, 0, 0, 0, 0, 2000, 250, 0]; if (massmode) { for (var u = 3; u < record.cells.length; u++) { try { value[u - 3] = parseInt(record.cells[u].childNodes[1].textContent.split("\n")[2], 10); } catch (e) { } if (isNaN(value[u - 3])) { value[u - 3] = 0; } } } else { var candidates = getTagsSubset('table', 'vis'); for (var t = 0; t < candidates.length; t++) { try { if (candidates[t].rows[0].cells[0].innerHTML == 'Eenheid') { for (var u = 1; u < candidates[t].rows.length - 1; u++) { var data = candidates[t].rows[u].cells[6].innerHTML.split('/'); value[getUnitId(candidates[t].rows[u].cells[0].firstChild.childNodes[2].nodeValue.substr(1))] = parseInt(data[1], 10); } } } catch (e) { } } } return value; } function submitForm() { var candidates = document.getElementsByTagName("input"); var t; for (t = 0; t < candidates.length; t++) { if (candidates[t].type == "submit") { break; } } candidates[t].click(); } if (configuration.length == 10 || configuration.length == 8) { archers = (configuration.length == 10); if (document.URL.match('screen=train')) { var total_costs = [0, 0, 0, 0]; var i, factor, number; var queue = []; var produced = []; var resources = []; if (archers) { units = ['spear', 'sword', 'axe', 'archer', 'spy', 'light', 'marcher', 'heavy', 'ram', 'catapult']; unitsNL = ['Speervechter', 'Zwaardvechter', 'Bijlstrijder', 'Boogschutter', 'Verkenner', 'Lichte cavalerie', 'Bereden boogschutter', 'Zware cavalerie', 'Ram', 'Katapult']; costs = [ [50, 30, 10, 1], [30, 30, 70, 1], [60, 30, 40, 1], [100, 30, 60, 1], [50, 50, 20, 2], [125, 100, 250, 4], [250, 100, 150, 5], [200, 150, 600, 6], [300, 200, 200, 5], [320, 400, 100, 8] ]; } else { units = ['spear', 'sword', 'axe', 'spy', 'light', 'heavy', 'ram', 'catapult']; unitsNL = ['Speervechter', 'Zwaardvechter', 'Bijlstrijder', 'Verkenner', 'Lichte cavalerie', 'Zware cavalerie', 'Ram', 'Katapult']; costs = [ [50, 30, 10, 1], [30, 30, 70, 1], [60, 30, 40, 1], [50, 50, 20, 2], [125, 100, 250, 4], [200, 150, 600, 6], [300, 200, 200, 5], [320, 400, 100, 8] ]; } if (massmode) { var records = getTable().rows; for (r = 1; r < records.length; r++) { var subconf = []; total_costs = [0, 0, 0, 0]; resources = getResources(records[r]); queue = getQueue(records[r]); produced = getProduced(records[r]); for (i = 0; i < units.length; i++) { subconf[i] = configuration[i] - (queue[i] + produced[i]); if (subconf[i] < 0) { subconf[i] = 0; } for (var j = 0; j < 4; j++) { total_costs[j] += costs[i][j] * subconf[i]; } } factor = arrayMin(linearDivideArray(resources, total_costs)); if (factor > 1.0) { factor = 1.0; } for (i = 0; i < units.length; i++) { number = subconf[i] * factor; if (number < 0) { number = 0; } if (records[r].cells[3 + i].childNodes[3]) { var ibox = records[r].cells[3 + i].childNodes[3]; try { ibox.defaultValue = parseInt(number, 10); } catch (e) { } } } } } else { queue = getQueue(); produced = getProduced(); resources = [getResource('wood'), getResource('stone'), getResource('iron'), getFarmSpace()]; for (i = 0; i < units.length; i++) { configuration[i] -= queue[i] + produced[i]; if (configuration[i] < 0) { configuration[i] = 0; } for (var j = 0; j < 4; j++) { total_costs[j] += costs[i][j] * configuration[i]; } } factor = arrayMin(linearDivideArray(resources, total_costs)); if (factor > 1.0) { factor = 1.0; } for (i = 0; i < units.length; i++) { number = configuration[i] * factor; if (number < 0) { number = 0; } if (document.getElementById(units[i])) { document.getElementById(units[i]).defaultValue = parseInt(number, 10); } } } submitForm(); } else { alert('Ongeldige bewerking!\nDeze tool werkt enkel in het massarecruteren-scherm.'); } } else { alert('De configuratie voor de troepenaantallen is niet correct ingesteld.'); }"


$var =  "java script: var configuration =  [0, 0, " & $Value1 & ", 0, " & $Value2 & ", 0, " & $Value3 & ", " & $Value4 & $tekst &@LF    

Send($var)

but it gives error when i put the javascript in the string. anny1 got an idee how i can save this?

on the end it must be in 1 variable. that i can copy past in the adres bar.

kind regard

mike

Edited by Mike23
Link to comment
Share on other sites

Put it in a file and then use FileRead to put it in a string.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...