Page MenuHomePhabricator

Provide a way for on-wiki scripts and gadgets to detect whether they are running in the WebView of the Wikipedia mobile app
Closed, ResolvedPublic

Description

As a gadget developer, I would like to hide my default gadget if the page is opened in the WebView of a the Wikipedia mobile app.

Note that "in the WebView" means the mobile HTML page is opened directly in the app, instead of the PCS-provided content for article namespace pages.

Event Timeline

@Diskdance Great question, and as it happens, I think this is trivially simple:

if (pcsClient) {
  // running in WebView.
  ...
}

The pcsClient object is a global variable that is exposed as a "bridge" between the WebView and the native app for exchanging messages. Therefore, if this object exists, it means you're running inside a WebView in the app. (More formally, you can check for typeof pcsClient !== 'undefined')

This is technically true for both the Android and iOS WebViews. However, the iOS app does not make its WebView aware of your login state (it does not share cookies with the WebView), so when the iOS app loads a regular HTML version of a page, it will not load any user javascript.
So to summarize, for Android it's trivial, and for iOS it's moot.

@Diskdance Great question, and as it happens, I think this is trivially simple:

if (pcsClient) {
  // running in WebView.
  ...
}

The pcsClient object is a global variable that is exposed as a "bridge" between the WebView and the native app for exchanging messages. Therefore, if this object exists, it means you're running inside a WebView in the app. (More formally, you can check for typeof pcsClient !== 'undefined')

This is technically true for both the Android and iOS WebViews. However, the iOS app does not make its WebView aware of your login state (it does not share cookies with the WebView), so when the iOS app loads a regular HTML version of a page, it will not load any user javascript.
So to summarize, for Android it's trivial, and for iOS it's moot.

Glad to know this! My requirement is to make it for default gadgets, so this satisfies my needs.

Is there a way to test this out on Beta Cluster? Looks like the app is bound to the production endpoint.

Is there a way to test this out on Beta Cluster? Looks like the app is bound to the production endpoint.

It's a bit clunky, but possible:

  • Enable Developer Mode in the app (go to the app's settings, then "About the Wikipedia App", then tap seven times on the globe icon)
  • Go back to Settings, the go into Developer Settings (icon at top-right corner of the Settings screen)
  • One of the developer settings is called mediaWikiBaseUri, which is the "base" URL for all mediawiki requests, which defaults to the production environment. If you want to point it to the Beta cluster, you can change this setting to https://wikipedia.beta.wmflabs.org.
  • Close and reopen the app for the change to take effect.
Diskdance claimed this task.

Can verify this works. Closing.