Rhopercy Rhopercy - 3 months ago 8
Javascript Question

How to iterate js variable array?

Well, this is a bit confusing for me, I want to iterate thorugh an array to get a value, I don't how to explain it:

ccs_cc_args =

ccs_cc_logsvc = function ccs_cc_logsvc(){var t=this;t.pStart="ccs_cc_log_startTime",t.pInit="ccs_cc_initParams",t.pId=t.genGuid(),"undefined"==typeof window[t.pStart]&&(window[t.pStart]=(new Date).getTime()),t.ping=function(){t.pingCount++,t.pingCount>1&&t.sendEvent("Ping"),t.pingCount<=t.maxPing&&window.setTimeout(t.ping,t.pingPeriod)};try{t.host=window.location.href.substr(0,300)}catch(e){}t.cnt=0,t.init(),t.sendEvent("TInit",null,!0),t.setPing(0,15e3)}
ccs_cc_contentloader = function ccs_cc_contentloader(t){var e=this,c="ccs_loading_scripts";"undefined"==typeof window[c]&&(window[c]=[]),e.isLoading=!1,e.addedObjects=[],e.waitLoadScripts=[],e.cssToLoad=0,e.scriptToLoad=0,e.objName=t,e.addedRefs=[],e.loadingCSS=[],e.isLoadFinishing=!1,e.objectToLoad=null,e.contextId=null,e.lastScript=null,e.lastTarget=null,e.navigated=!0,e.defaultUrl={},e.loadingScripts=window[c],e.ccsHost=ccs_cc_log.ccsHost,e.skey=ccs_cc_log.skey,window.setInterval(function(t){return function(){t.navTo()}}(this),50)}

ccs_cc_log = [object Object]
ccs_cc_initParams = ,ws.pagecontent.com

ccs_cc_log_startTime = 1471905959132

ccs_cc_debug_lastUrl = http://ws.pagecontent.com/7f08cde9/script/hp-auto-pp?cpn=CPN&mf=HP&pn=T1C06…ean=UPC_EAN_CODE&lang=ES&market=MX&host=&nld=1

ccs_cc_loadQueue =

ccs_loading_scripts =
ccs_cc_ld_hp-auto-pp = [object Object]
ccs_cc_loge_aa0e4d = function ccs_cc_loge_aa0e4d(id, clientParams, args){var et; var serverParams;switch (id) {
case '5901d207': et='ProductHookLoad'; serverParams={"ServerTime":"1","ResultCode":"100"}; break;
} ccs_cc_log.logEvent (et, 'SKey=abcd74589&LCID=2415&Market=MX&Locale=ES-MEX&ZoneId=hp-auto-pp&ZoneVer=1&SMfgName=HP&SMfgPn=TOM025HAC6547&MfgId=23', serverParams, clientParams); }


I want to get to ccs_cc_loge_xxxxxx (xxxxx is because it changes everytime page reloads), so inside this, I want to read/get Resultcode and its response.

I tried to read all globals like this, so I could read all data:

<script>

$( document ).ready(function() {
(function() {
var iframe = document.createElement('iframe');
iframe.onload = function() {
var iframeKeys = Object.keys(iframe.contentWindow);
Object.keys(window).forEach(function(key) {
if(!(key in iframeKeys)) {
console.log(key+" = "+window[key]);

}
});
};
iframe.src = 'about:blank';
document.body.appendChild(iframe);
})();
});
</script>


Any way to achieve this?

Answer

While this is a hack to a very horrible idea in the first place, you could do this by looping through all keys like you did and keeping only those matching the variable name pattern you need.

So in the code that loops and prints keys, do this:

if(/^ccs_cc_loge_[0-9]+$/.test(key)) {
  // call code that needs to use this variable;
};

This regex will match any variable starting with ccs_cc_loge_X where XXXX is any amount of digits. This is more specific and browser-compatible then using string functions like .startsWith