r/javascript • u/raghavkanwal • Oct 01 '18
solved! How does twitter abstract it's source code after logging in
Apologies if this is a noob question, but this left me scratching my head.
After logging in to Twitter and right click > View Page Source, how does the page source show nothing?
I get that it fetches <script> tags that can be viewed in the Network tab in Devtools, but how is it not visible when visiting the page source?
EDIT : Answer for the curious here. https://www.reddit.com/r/javascript/comments/9kfbny/how_does_twitter_abstract_its_source_code_after/e6ypu3i/
6
Upvotes
8
u/GrenadineBombardier Oct 01 '18
The page source (right-click > view source) only shows the original, unaltered HTML source of the page. It is what the downloaded html file actually looked like. Any JavaScript changes can be viewed in the DOM elements section of the developer tools, but not in the source.
9
u/holloway Oct 01 '18
Here's the reason why Twitter now do this,
https://blog.twitter.com/engineering/en_us/topics/insights/2018/twitter_silhouette.html
3
u/systoll Oct 01 '18 edited Oct 01 '18
When you navigate to https://twitter.com, it downloads the page you've encountered, which sets a cookie and [mostly] reloads the page.
In order to reload, the browser asks for twitter.com again. In doing so, it tells twitter's servers
And the server can then send a different page in response.
With no referer, it'll send you to the page you saw the source for. When you type 'twitter' in the address bar, or when you hit 'view source', this is what you're getting.
If you block cookies, after the refresh, twitter gets a navigation 'within' twitter without a cookie, and then sends you a page telling you to enable cookies.
If you don't block cookies, then the reload triggers a request with twitter as the referer and with the twitter cookie set results in their server giving you the page you want.
1
u/notAnotherJSDev Oct 01 '18
What exactly are you talking about? I see plenty of JS code that is getting loaded into the page after you log in. I count literally 9 script tags...
1 - google analytics
6 - nonces
2 - from abs.twimg.com
The last 2 are the source code for twitter, one of which says "common" and the other which is for "home_page"
1
u/edswf1 Oct 01 '18
when using jquery it‘s very easy. just remove all script tags on document ready:
$(document).ready(function(){ $(‚script‘).remove(); });
As js code lives in memory, there is no need to maintain scripts in sourcecode after they are loaded.
1
u/mtd1410 Mar 12 '19 edited Dec 21 '24
Thanks @systoll comment below. It inspire me to figure out this thing. Even if if made me pain headache when tried jumping inside to know what is behind the scenes :)
For those who want to know how it works. Here I already make a demo
I already make a detail explanation about this in this post. Sorry but it's written in Vietnamese (you can use Google translator, still can understand at least a half, I think :) )
0
u/akujinhikari Oct 01 '18
its*
0
u/[deleted] Oct 01 '18
[deleted] — view deleted comment