在Cordova的InAppBrowser上定义用户代理 [英] Define User Agent on InAppBrowser from Cordova

查看:170
本文介绍了在Cordova的InAppBrowser上定义用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cordova的 InAppBrowser 插件,而且我想从我的应用程式( Android,iOS )开启网页。但是,网站应该知道用户是否通过移动浏览器或通过我的应用程序输入。

I use InAppBrowser plugin from Cordova and I would like to open a webpage from my app (Android, iOS). However, the website should know if the user has entered by the mobile browser or through my app.

我虽然从我的应用程序更改用户代理将能够工作。是否有类似的解决方案?

I though that changing the user agent from my app would be able to work. Is there a similar solution to that?

我想在我打开一个新窗口之前,我应该定义,用户已通过我的应用程序进入网站

I guess right before I open a new window I should define that the user has entered the website through my app.

window.open("http://test.example.com", "_blank", "location=no");


推荐答案

当然,您可以:

window.open("http://test.example.com?entry=app","_blank","location=no");
// see how we added ?entry=app query string?

然后在你打开的网页的JavaScript的某个地方,只是解析出条目参数,并根据条目的值执行任何特定的操作。

and then somewhere in the JavaScript of the web page that you are opening up, just parse out the "entry" parameter and do whatever specific thing you want to do based on the value of entry.

您可能需要设置一些cookie或某些东西,如果你想使用逻辑,因为如果他们单击InAppBrowser中的链接,它将松开?entry = app 查询字符串。

You may need to set some cookie or something though if you want to use this logic on multiple pages, because if they click on a link within the InAppBrowser, it will loose the ?entry=app query string.

我想你可以尝试在每次点击InAppBrowser中的一个新链接时使用 addEventListener(loadstart,fn )。这样,每当用户单击新页面时,将调用 fn 函数,在这里您可以重写URL。它可以是:

I suppose you could try to override the URL every time they click on a new link in the InAppBrowser using an addEventListener("loadstart",fn). This way the function fn would get called every time the user clicks on a new page and here you could rewrite the URL. It could be something like:

var ref = window.open("http://test.example.com?entry=app", "_blank", "location=no");
ref.addEventListener("loadstart", IABcallback);

function IABcallback(event){
    if( event.url.substr(event.url.search(/entry/, '')) === "entry=app"){
        // This is a lazy check for "entry" paramater, you should use a library or something
        console.log("already have entry=app query string");
    }else{
        // open a new window with the same URL but add ?entry=app
        ref = window.open(event.url+"?entry=app", "_blank", "location=yes");
        // reattach this event listener since it is a new window
        ref.addEventListener("loadstart", IABcallback);
    }
}

主要的问题是,它打开一个新的InAppBrowser窗口,每次他们点击当前窗口顶部的链接,使后退按钮不可用。你可以尝试通过使用 ref.close()来关闭窗口,然后重新打开它,但我有问题,让这个工作。我也尝试使用自定义名称的窗口(而不是_blank),但这也没有工作。所以,对不起,这是一个有点不完全的答案,但希望它可以给你一些想法。使用自定义查询字符串初始化加载应用程序可能会更容易一些,然后记住您通过localStorage或cookie或其他方式来自InAppBrowser。

This works "okay." The major problem is that it opens up a new InAppBrowser window every time they tap a link on top of the current window, making the back button unusable. You could try to get around this by using ref.close() to close the window and then reopen it, but I was having problems getting this to work. I also tried using a custom name for the window (instead of "_blank"), but this did not work either. So, sorry that this is a somewhat incomplete answer but hopefully it can give you some idea. It'd probably be much easier to just initally load the app with the custom query string and then remember that you came from the InAppBrowser via localStorage or a cookie or something.

这篇关于在Cordova的InAppBrowser上定义用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆