Adnan
Adnan

Reputation: 199

How to obtain the user ID using a Stack Snippet?

I am planning to make a kind of hashing algorithm for a challenge in Programming Puzzles and Code Golf. In order to do so, I need to have a salted hashing algorithm. I made the hashing algorithm, but now I need to obtain the user ID of the current user on the current StackExchange site using a Stack Snippet, which only uses HTML, CSS and Javascript.

The user ID is shown here:

enter image description here

Note that this is not the current URL I'm located on.

Does anyone know how to do this? I found the following in the source code of the site:

StackExchange.init({...,"user":"fkey":...,"rep":...,"userId":2976513,"accountId":...}

With dots representing irrelevant data. How do I obtain the userId from this? I'm using this to make a salted hash here, in the Stack Snippet.

Upvotes: 1

Views: 133

Answers (2)

GitaarLAB
GitaarLAB

Reputation: 14645

To the best of my knowledge, Stack-Nippets (hosted on http://stacksnippets.net/) will not be permitted to access http://stackoverflow.com (like window.top or other cross-domain stuff etc.) due to the same-origin policy.
Obviously this is for security, so that malicious users can't hide malicious scripts (automating some 'user-behaviour') in stack-snippets.

EDIT:
although you could get the question's url:

alert(document.referrer);

it normally does not contain the user-id:
https://stackoverflow.com/questions/[question_id]/[question_title]

EDIT2:
Also, a direct link like https://stackoverflow.com/a/34550464/588079
(format https://stackoverflow.com/a/[answer_id]/[user_id]) will be changed/redirected to https://stackoverflow.com/questions/[question_id]/[question_title]/[answer_id]#[answer_id].

Upvotes: 2

Reeno
Reeno

Reputation: 5705

StackExchange.options.user.userId contains the current user id at any StackExchange site (it differs between the sites, even for the same user).

If the user isn't logged in, it's undefined.

Upvotes: 0

Related Questions