1

There is a page where it displays a certain value when you hover over an image. I need to programatically fetch that value from the page. I've got access to the JS console within the page, but cannot figure out where it comes from.

I tried adding breakpoints on DOM changes, but it still unclear where the data came from.

I have also tried dumping the whole window object but it does not seem to dump out enough data or contain the data (number) that are displayed (censor function taken from Chrome sendrequest error: TypeError: Converting circular structure to JSON)

function censor(censor) {
  var i = 0;

  return function(key, value) {
    if(i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value) 
      return '[Circular]'; 

    if(i >= 1129)
      return '[Unknown]';

    ++i; // so we know we aren't using the original object anymore

    return value;  
  }
}

for(var key in window){
  console.log(key);
  if(key === 'frames' || key === 'self' || key === 'window' || key === 'parent' || key === 'top' || key === 'document') continue;
  console.log(JSON.stringify(window[key], censor(window[key])))
}

Are there any ideas how I can get a full dump of the data in the page (so I could search where exactly it came from) or get clear trace where the data in the DOM came from? The DOM element is added on image hover, and then removed when mouse is moved away.

Script47
  • 14,230
  • 4
  • 45
  • 66
Giedrius
  • 1,370
  • 3
  • 14
  • 37

1 Answers1

0

Using Chrome dev tools, right-click element and select inspect. In the righthand panel, select the Event Listeners tab. In the list, expand mouseover (or other appropriate event).

You will be presented links to code that directly effects that element.

Edit #1

Reference the comments regarding retrieving data from some other (presumably non-controlled) code:

If that data is created/derived/contolled within a closure that was not created by your code, it is unlikely that you will be able to retrieve. This is the nature of closures in JavaScript. The data might be inside a bubble you cannot reach into.

Edit #2

A hack you could use: force the mouseover event on the element. This puts the value you are after into the DOM. Pull the value from the DOM. Then turn off the mouseover event.

Script47
  • 14,230
  • 4
  • 45
  • 66
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • That's a great suggestion! I have tried that, but unfortunately the code is all minified so it's not a great read :( That's why I'm looking more for a way to retrieve the actual data as it must be stored somewhere accessible, as it's connected to that particular DOM element in some way. So looking to find some path that will retrieve me the value that is rendered. – Giedrius Apr 22 '18 at 18:21
  • Anything you get from the browser will simply be a deconstruction of exactly the same information. Cannot simply "un-minify" code to reproduce the original (cannot reverse engineer automatically). Put up a link and I'll attempt to answer the question. – Randy Casburn Apr 22 '18 at 18:23
  • Yeah, that's why my idea is that the data is still contained somewhere in the JS, and connected to that DOM element. So if I could somehow get all in-memory data or something, and then search for that particular number that is displayed, perhaps I could then find a way to fetch that out via the JS console? – Giedrius Apr 22 '18 at 18:25
  • Thanks for the update. That makes sense. The data is coming through an AJAX request, so it's not _created_ inside a closure - I guess it might be attached to the DOM element in some capacity anyway? This discussion gave me another idea of intercepting the AJAX requests, so I will try that route too. – Giedrius Apr 22 '18 at 18:29
  • 1
    If it is coming from an AJAX request it most likely (certainly) is inside a closure. Anyway, you keep coming back to "_attached to the DOM_" and that is your error. Not everything in JS is attached to some DOM thing. The JavaScript virtual machine (V8) manages its own stack, heap and call buffers; your data may be in the heap, and only in the DOM during the `mouseover` event. So you can grab it from the DOM when mouseover is fired, but no other time. – Randy Casburn Apr 22 '18 at 18:37
  • Please check more Edits in answer. – Randy Casburn Apr 22 '18 at 18:38