JavaScript

Hello, i need to get the Facebook ID by JavaScript. I try with

Dim t as String=me.EvaluateJavaScriptMBS("window.status = document.getElementsByTagName('USER_ID')[0].innerText;")

But this don’t work, any ideas? Thanks in advance!

Try it without the “window.status =” part.

No, this doesn’t work too

[quote=203624:@Greg O’Lone]Greg O’Lone 18 minutes ago Xojo Inc
Try it without the “window.status =” part.

Federico José D’Andrea is not verified 6 minutes ago
@Greg O’Lone Try it without the “window.status =” part.
No, this doesn’t work too
[/quote]

Federico, you have mixed up the window.status method and EvaluateJavaScriptMBS.

Greg is right. You want to drop window.status =

Moreover, who told you to use ‘USER_ID’ ? Looking at the code of the Facebook login page with the developer tools of the browser, it appear the field where one enters the email is

<input type="text" class="inputtext" name="email" id="email" value="" tabindex="1">

This should work :

Dim t as String=me.EvaluateJavaScriptMBS("document.getElementsById('email')[0].innerText;")

[quote=203627:@Michel Bujardet]Federico, you have mixed up the window.status method and EvaluateJavaScriptMBS.

Greg is right. You want to drop window.status =

Moreover, who told you to use ‘USER_ID’ ? Looking at the code of the Facebook login page with the developer tools of the browser, it appear the field where one enters the email is

<input type="text" class="inputtext" name="email" id="email" value="" tabindex="1">

This should work :

Dim t as String=me.EvaluateJavaScriptMBS("document.getElementsById('email')[0].innerText;")

Nope, that don’t works too. I use “USER_ID” because I found the element with the developer tools of the browser too, but in the part of the profile.

Ah, well you can’t use getElementById to get that item anyway. That’s a JavaScript object somewhere on the page.

Oh, but, there’s other way to get it? Or not?

RegEx :wink:

It would seem possible to get all the content of the using GetElementByTagName.
See http://www.w3schools.com/jsref/met_doc_getelementsbytagname.asp

Then you will have to use Regex or Instr() to locate “USER_ID” or “ACCOUNT_ID” within that big string and use Regex or Mid() to fetch the information you need.

What I am not sure is if EvaluateJavaScriptMBS can get the whole thing in one pass. It may have a limit which is not mentioned on the MBS site. Maybe Christian can tell.

If there is a limit, you may have to cut that information and feed it by chunks to EvaluateJavaScriptMBS in JavaScript using Substr
http://www.w3schools.com/jsref/jsref_substr.asp

Otherwise, I know Christian has a way of getting the entire content of the DOM. So you could get the entire page and then search within that. Unfortunately I never used it so I do not know the exact name of the plugin. You would have to ask him.

[quote=203640:@Michel Bujardet]It would seem possible to get all the content of the using GetElementByTagName.
See http://www.w3schools.com/jsref/met_doc_getelementsbytagname.asp

Then you will have to use Regex or Instr() to locate “USER_ID” or “ACCOUNT_ID” within that big string and use Regex or Mid() to fetch the information you need.

What I am not sure is if EvaluateJavaScriptMBS can get the whole thing in one pass. It may have a limit which is not mentioned on the MBS site. Maybe Christian can tell.

If there is a limit, you may have to cut that information and feed it by chunks to EvaluateJavaScriptMBS in JavaScript using Substr

Otherwise, I know Christian has a way of getting the entire content of the DOM. So you could get the entire page and then search within that. Unfortunately I never used it so I do not know the exact name of the plugin. You would have to ask him.[/quote]
But acording to Regex is for search and repleace and what i need is get the content of “USER_ID”.But Can I get this content with Regex?

Regex is also for searching if you don’t want to replace. If your search text only occurs once in the text that needs to be searched then an instr will do fine. Try the simplest method first and then go more complex if simple doesn’t do it.

Wait. There is an alternative. JavaScript String search().
http://www.w3schools.com/jsref/jsref_search.asp

So the process would be to get the content of , then search for USER_ID and then get the position plus 10, and get the 15 characters of the account number with Substr()
http://www.w3schools.com/jsref/jsref_substr.asp

The result would be only 15 characters, which is just fine with EvaluateJavaScriptMBS

That removes the complexity of Regex. I know it is regarded as an high art, but sometimes, keeping it simple is the way to avoid headaches :wink:

Just one little problem with this: you have to be brilliant to find and see the simple solution!
Glad you are aboard Michel! :slight_smile:

[quote=203659:@Andre Kuiper]Just one little problem with this: you have to be brilliant to find and see the simple solution!
Glad you are aboard Michel! :)[/quote]

Just trying :wink: Thank you Andre.

Here is the final JS code :

[code] Dim JS as string
JS = JS + “var x = document.getElementsByTagName(”“HEAD”"); "
JS = JS + “var headContent = x[0].innerHTML ;”
JS = JS + “var n = headContent.search(”“USER_ID”"); "
JS = JS + “var res = headContent.substr(n + 10, 15); "
JS = JS + “var dq = res.search(’””’) ; "
JS = JS + "if ( dq > 0) { "
JS = JS + "var Result = res.substr(0, dq) ; "
JS = JS + "} else {Result = res; } ; "
JS = JS + “window.status= Result ;”

HTMLViewer1.ExecuteJavaScript(JS)
[/code]

Note that the code not only fetches 15 characters which is the length of the account number in the example posted above, but as I was experimenting with my own account which being ancient has a much shorter account number, I added a search for double quote, and cleaned the result so it gets you only the numbers and not the quote. Since Facebook is now enlisting half the population in the US and Europe, it is possible the length of the account number grows in the coming years. Then on line 5, one can put 20 instead or 15 for instance, the cutting routine will take care of the difference.

This code works fine on Mac. However, be careful with window.status if you plan to build on Windows, where that command does not work anymore for security reasons. It should be replaced by document.title and the TitleChanged event, making sure to cancel all Facebook titles that come (there are about 6).

I usually use a tag in what I feed to Title so it can be recognized by Xojo on arrival. Something like Tag:123456789 then in TitleChanged I can recognize what comes from JavaScript, and simply get the right side of the string. It is also proper to save the current value of window.title in a JavaScript variable, and restore it after using the title to return something, in case that title is displayed in the app.

I quickly looked at EvaluateJavaScriptMBS ; it is for Mac only so that would not solve the issue on PC.

[quote=203661:@Michel Bujardet]Just trying :wink: Thank you Andre.

Here is the final JS code :

[code] Dim JS as string
JS = JS + “var x = document.getElementsByTagName(”“HEAD”"); "
JS = JS + “var headContent = x[0].innerHTML ;”
JS = JS + “var n = headContent.search(”“USER_ID”"); "
JS = JS + “var res = headContent.substr(n + 10, 15); "
JS = JS + “var dq = res.search(’””’) ; "
JS = JS + "if ( dq > 0) { "
JS = JS + "var Result = res.substr(0, dq) ; "
JS = JS + "} else {Result = res; } ; "
JS = JS + “window.status= Result ;”

HTMLViewer1.ExecuteJavaScript(JS)
[/code]

Note that the code not only fetches 15 characters which is the length of the account number in the example posted above, but as I was experimenting with my own account which being ancient has a much shorter account number, I added a search for double quote, and cleaned the result so it gets you only the numbers and not the quote. Since Facebook is now enlisting half the population in the US and Europe, it is possible the length of the account number grows in the coming years. Then on line 5, one can put 20 instead or 15 for instance, the cutting routine will take care of the difference.

This code works fine on Mac. However, be careful with window.status if you plan to build on Windows, where that command does not work anymore for security reasons. It should be replaced by document.title and the TitleChanged event, making sure to cancel all Facebook titles that come (there are about 6).

I usually use a tag in what I feed to Title so it can be recognized by Xojo on arrival. Something like Tag:123456789 then in TitleChanged I can recognize what comes from JavaScript, and simply get the right side of the string. It is also proper to save the current value of window.title in a JavaScript variable, and restore it after using the title to return something, in case that title is displayed in the app.

I quickly looked at EvaluateJavaScriptMBS ; it is for Mac only so that would not solve the issue on PC.[/quote]
Wow, Thank you so much! It’s works!

Of course it works. I customarily test what I post :wink:

Glad to be of help.