0

I have an Android app which uses Trusted Web Activity with an external link. When clicking link it opens in an in-app browser (Custom Tabs). I want hide a div in that Custom Tabs page, but I want to show that div when the site opens in Chrome. Is it possible? The div is like below.

<div id="something" class="something"><a class="btn btn-primary" href="somelink">text</a></div>

  • You can load the page with javascript look at this question- https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – TIGERT Apr 30 '20 at 07:11
  • 1 solution is by detecting the location of the page whether it is on a webview page/regular browser, then do some function to hide the div element. Detect webview/browser reference: https://stackoverflow.com/questions/12727117/android-webview-user-agent-vs-browser-user-agent – fxbayuanggara Apr 30 '20 at 07:12
  • I have edited my question. It is a TWA app. Not a coder here. Please give answers in simple format. – Monsoon Malabar Apr 30 '20 at 07:23
  • Any javascript code to differentiate between android browser and in-app browser that can be inserted/used in website? – Monsoon Malabar May 01 '20 at 08:23
  • Here's how to detect if a site has been opened by a Trusted Web Activity: https://stackoverflow.com/questions/54580414/how-can-i-detect-if-my-website-is-running-inside-a-trusted-web-actvity/54580415#54580415. But it seems like you want to implement this on a navigation to outside the URL opened in the Trusted Web Activity, with is Custom Tabs, not WebView. Can you confirm? – andreban May 02 '20 at 08:00
  • I mean I have an external link in twa, which opens in in-app browser. In that in-app browser I want to hide a div, but it should be seen in normal android browser. Hope I was clear. It is not related with twa but related with in-app browser in twa. – Monsoon Malabar May 02 '20 at 11:20
  • Do you own the page being opened in the in-app browser? If you do, you can append a query parameter to the URL when the user navigates to the in-app browser. If you don't own the page, it's not possible (by design). – andreban May 06 '20 at 06:56
  • @andreban, Yes I do own the page that being opened in in-app browser – Monsoon Malabar May 06 '20 at 11:21
  • What query parameter you are referring to. – Monsoon Malabar May 07 '20 at 08:50
  • Let's say the Trusted Web Activity is openning `https://siteA.com` and `https://siteB.com` is opened, you want to hide a div when siteA opens siteB. You could link to `https://siteB.com?hideDiv=true`. siteA can append the extra parameter only when inside a Trusted Web Activity. siteB checks if the parameters exiss and hides the dive if it is present. – andreban May 08 '20 at 12:10
  • @andreban Thank you. It worked. I am using a static site as `https://siteB.com`. For me when I use `https://siteB.com?hideDiv=true` and go from `siteA.com`, it worked for the first click. After that `div` shows again. I used code as `` – Monsoon Malabar May 08 '20 at 16:31
  • I mean after first click then I surf through siteB.com, div shows again – Monsoon Malabar May 08 '20 at 16:47
  • Above code is available to all pages – Monsoon Malabar May 08 '20 at 17:39
  • Added as an actual answer. Could you accept it? – andreban May 11 '20 at 08:41

1 Answers1

0

Assuming the developer controls both sites involved, if the Trusted Web Activity is opening https://siteA.com and https://siteB.com is opened from a link on site A and you want to hide a div onlnye when siteA opens siteB.

You could link to https://siteB.com?hideDiv=true. siteA can append the extra parameter only when inside a Trusted Web Activity. siteB checks if the parameters exists and hides the dive if it is present.

Another alternative is checking document.referrer in JavaScript and checking if it matches https://siteA.com.

andreban
  • 3,575
  • 1
  • 16
  • 40