3

I know about the "Mobile" link at the bottom of SE pages.

I want to make use of the mobile version of SE as part of an editor plugin I'm currently creating (don't ask which editor, I'm creating that too! lol).

However the mobile version doesn't seem to be working with a distinct URL.

So, how can it be accessed?


P.S. My guess: the culprit is a cookie setting theme to 2. Am I right? Is there no other simpler workaround to get the mobile version? That was it! Answer posted below. :-)

2
  • 1
    Since this is for an app, it kinda feels like you should be using the api; there are search methods - look for /search - live preview Sep 30, 2014 at 11:21
  • @MarcGravell This is a fine idea, and exactly what I had in mind. However, at this early (mostly experimental) stage, I decided that forwarding to the results page would be enough. When it starts... growing a bit more, that's what I'm gonna do! ;-) Sep 30, 2014 at 11:24

2 Answers 2

2

URL is actually {some_site}/mobile/on to go to the mobile page and {some_site}/mobile/off to go to the full page. But...

StackExchange.switchMobile = function(e) {
    $.post("/mobile/" + e, {
         "returnurl": window.location.href
    }, function(e) {
         window.location.href = e;
    });
}

...as you can see, they use post method. Earlier it was possible to switch between mobile / full, but since they started to use post method, it is not possible anymore.

So now you can use:

  • StackExchange.switchMobile("on") to switch to mobile version and StackExchange.switchMobile("off") to switch to the full (this is how mobile link works)

  • $.post("/mobile/on",function(){window.location.reload()}); to switch to mobile version and

    $.post("/mobile/off",function(){window.location.reload()});to switch to the full version

7
  • Thanks a lot! Though, to be honest I expected something more straightforward. I mean I just want to forward to a mobile search page: e.g. http://stackoverflow.com/mobile/search?q=search+for+sth. It shouldn't be any more complicated than that... Sep 30, 2014 at 7:42
  • @Dr.Kameleon what do you mean "mobile search"? Sep 30, 2014 at 7:50
  • OK. This stackoverflow.com/search?q=beautify+css is a URL returning the search results from SO for the term beautify css. I want that very same thing, but the mobile version of the page. Sep 30, 2014 at 7:52
  • 1
    @Dr.Kameleon you can't force a theme change via GET url only, that's plain XSRF attack that's why it's possible only via a POST. Sep 30, 2014 at 7:53
  • Just found a simple, no POST-related way I'm going to post right now. :-) Sep 30, 2014 at 8:39
  • @Dr.K yeah, show it plz.
    – nicael
    Sep 30, 2014 at 8:39
  • @nicael Just did. :-) Sep 30, 2014 at 8:50
2

@nicael and @Shadow Wizard are right in their explanation. However, put simply I wanted to retrieve a search page (in its mobile version) with a single request, inside a Cocoa Webview (no post-processing, no js, no nothing).


So, I looked into the HTTP headers AFTER setting the mobile view. And noticed an interesting thing : When Mobile mode is on, we are packing a theme cookie with the value 2. (While, for the "Full Site" mode, theme is set to 0).

And that's how I came up to the following very simple code, in Objective-C/Cocoa :

- (void)mobileSearchSO:(NSString *)searchFor
{
    // Format the search path
    NSString* searchPath = [NSString stringWithFormat:@"http://stackoverflow.com/search?q=%@",searchFor];
    NSURL* searchUrl = [[NSURL alloc] initWithString:searchPath];

    // Create the request
    NSMutableRequest* req = [NSMutableURLRequest requestWithURL:searchUrl];

    // Create the cookie
    NSHTTPCookie* cookie = [NSHTTPCookie cookieWithProperties:@{
        NSHTTPCookieDomain  : @"stackoverflow.com",
        NSHTTPCookiePath    : @"/",
        NSHTTPCookieName    : @"theme",
        NSHTTPCookieValue   : @"2"
    }];

    // Set the cookie
    [req setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:@[cookie]]];

    // Load the request
    [[_webView mainFrame] loadRequest:req];
}

Tested and working fine. Enjoy! :-)

11
  • 1
    thats already looks like too programming question :DD Maybe it should be migrated to SO?
    – nicael
    Sep 30, 2014 at 8:55
  • 1
    @nicael Haha! To be honest, while I'm a full-time SO addict, I'm not that familiar with Meta and I wasn't sure where to post it... I don't know. If someone thinks it should be migrated, let it be! :-) Sep 30, 2014 at 9:00
  • "I'm a full-time SO addict" I already noticed :D Looking at your SO profile.
    – nicael
    Sep 30, 2014 at 9:00
  • @nicael I don't know. Maybe it's that I keep considering the whole SE network, a totally programmer-oriented... domain. OK, with some of us interested in Photography, Buddhism, or whatever. (lol). But still programmers. Sep 30, 2014 at 9:03
  • 1
    By the way, I participate in SO too (because have some programming skills), but I am offering lots of bounties :D
    – nicael
    Sep 30, 2014 at 9:07
  • And, by the way, can you answer this question?
    – nicael
    Sep 30, 2014 at 9:14
  • @nicael Hmm. Unfortunately not. I am using Xcode 6. But have absolutely no idea regarding "Playground"s... Sep 30, 2014 at 9:17
  • Considering that you needed code for your iOS app and didn't mention it in your question my answer is completely useless now?
    – nicael
    Sep 30, 2014 at 9:30
  • First-off, (it might be valid for iOS too though) it's for an OSX app. As for your answer, I think it's really useful, but a different approach. It's not "wrong" by any means. Rather informative too, I'd say. (Tried it myself, and it works fine) Sep 30, 2014 at 9:33
  • Looks like it will work also for iOS.
    – nicael
    Sep 30, 2014 at 9:34
  • It most likely will, but - given that I have around 1000x times more OSX-coding experience - I won't take any chances... lol Sep 30, 2014 at 9:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .