1

I'm developing userscript for one webpage (aka browser plugin). I need to update one global Javascript variable (lets call it gVariable (it is an array)) with the one I get with ajax request.

In ajax request I'm requesting for the same page I'm on. But just want to "extract" that one global variable and replace current one with the one downloaded.

This is something I have now (not working).

function LoadNewItemList() {
    $.get(window.location, function (data) {
        var $data = $(data);       
        unsafeWindow.gVariable = data.gVariable; //I'm getting 'undefined'
    });
}

JS test: http://jsfiddle.net/ywVKT/15/

Hooch
  • 28,817
  • 29
  • 102
  • 161
  • It seems to me you are reloading the entire current page via AJAX? And then hoping to extract a variable defined in JavaScript within the markup of that page? Or am I misreading things... `window.location` would have to point to a JSON structure in order to treat `data` as a navigable object. When you load via AJAX you are just loading the page markup, not a rendered DOM like you would loading a new window... – Pebbl Apr 18 '14 at 08:30
  • @pebbl Yes. I'm loading whole page. But is there any way to parse scripts and get that variable value? – Hooch Apr 18 '14 at 08:41
  • @PratikJoshi How would that help? All code is in my post. – Hooch Apr 18 '14 at 08:47
  • plz post it .else you will keep getting comments ,like past half an hour – Pratik Joshi Apr 18 '14 at 08:48
  • @PratikJoshi http://jsfiddle.net/ywVKT/14/ – Hooch Apr 18 '14 at 08:57
  • just tell what output you expect . write in comment Output : YOUR REQUIRED OUTPUT – Pratik Joshi Apr 18 '14 at 08:58
  • its working ,it gives output.please tell what do u expect – Pratik Joshi Apr 18 '14 at 09:01
  • @PratikJoshi Really? I know you think you can help. But everything is in my question. – Hooch Apr 18 '14 at 09:06
  • Unfortunately there isn't an easy way to parse for that information. You could use a RegExp but that would be quite prone to breaking if anything in the page content changed... and it would be difficult to support difficult structures -- possible if it's just strings/numbers? Your best bet would be to construct a server-side request that screen-scraped the info for you and returned JSON. However, as this is browser plugin you may not have that luxury. – Pebbl Apr 18 '14 at 09:15
  • @pebbl Friend of mine helped me. Look at my answer if you are interested. – Hooch Apr 18 '14 at 09:44

2 Answers2

1

Looking at your fiddle, there are mainly 4 tags. i.e. "title" , "link" , "ul" , "script". That's why you need to use index as 3, since the script tag contains the variable name and value. Try this and it would work.

$('#variableHere').text(data2[3].innerText);

it will return you following o/p: var gVariable = 0; gVariable = 5 Now you can use regex/substring function to extract the vairable name and value..

Vishal Khode
  • 831
  • 2
  • 9
  • 15
0

I found solution:

unsafeWindow.gVariable = 0;
var $script = $data.filter('script:contains("var gVariable")').first();
eval($script.text());
unsafeWindow.gVariable = gVariable;
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • Glad you found an answer, but personally I wouldn't like to use that solution myself. From a point of view of optimisation it's just not cricket... you are evaluating the entire returned DOM and script tag.. if that script tag only contains a simple variable then fine, but a RegExp on the markup for something so simple would be safer, more optimal and faster. – Pebbl Apr 18 '14 at 10:58
  • Yes. I use regex to only evaluate value for that one variable. But I didn't show it to make this code as simple as possible. – Hooch Apr 18 '14 at 23:19