How do I differentiate between if my code is running inside a chrome browser or a chrome app ? The application uses webview to load specific pages in which the javascript code has to work differently for chrome app and chrome browser.
Asked
Active
Viewed 132 times
0
-
@Xan This is not a duplicate. This is about code inside a webview. – Daniel Herr Mar 23 '17 at 19:49
-
Right, sorry, itchy trigger finger. I'll undupe. – Xan Mar 23 '17 at 19:50
1 Answers
0
The cleanest way would be to inject a content script into the webview that, in turn, injects some code into the page before it loads.
Something along those lines:
webview.addContentScripts([{
name: 'beacon',
matches: ['https://www.example.com/*'],
js: { files: ['beacon.js'] },
run_at: 'document_start'
}]);
webview.src = "https://www.example.com/app"
// beacon.js
var script = document.createElement('script');
script.textContent = "window.inWebview = true;";
(document.head||document.documentElement).appendChild(script);
script.remove();
Then, in your code, you can check whether window.inWebview
is defined and true.
Less clean alternatives:
- Modify the user-agent in a predictable way with
webview.setUserAgentOverride
. - Simply load the page with an extra parameter:
https://www.example.com/app?webview=1
.

Xan
- 74,770
- 16
- 179
- 206