gpt4 book ai didi

javascript - 在 PhoneGap/Cordova 中使用 InAppBrowser 在默认浏览器中打开链接

转载 作者:行者123 更新时间:2023-12-03 11:52:50 24 4
gpt4 key购买 nike

我正在构建一个应用程序,该应用程序使用 PhoneGap(特别是 PhoneGap Build)和 InAppBrowser 将响应式在线网站包装到易于使用的系统中。

该应用程序加载并运行良好,但有一项功能除外 - 在系统默认浏览器中打开外部链接。

这是 95% 的工作,但是特定情况会导致链接重定向出现问题(例如 http://www.facebook.com 重定向到 https://www.facebook.com 重定向到 https://m.facebook.com )。

使用我的代码,它检测到 URL 超出了配置的域,并在新的系统窗口中打开它,然后加载其他两个重定向并将其加载到应用程序中。

所需的操作是在系统默认浏览器中打开链接并停止所有进一步处理,包括重定向。

请注意,仅将 target 从 HTML 页面上的链接更改为 _system 是行不通的,因为它来自实时应用程序,因此位于应用程序外部。网站。

非常感谢任何提示和建议。

最重要的代码摘录如下:

从 PhoneGap 应用内:

//Called on deviceready
function launchSite(){
// Get Webpage in InAppBrowser (using the _blank target) and hide it until loaded
iabRef = window.open(url+'?inApp=true'), '_blank', 'location=no,hidden=yes');

//Attach listener for external link intercepting
iabRef.addEventListener('loadstart', iabLoad);
//Attach listener for loading complete
iabRef.addEventListener('loadstop', iabLoaded);
}

//Check if links are external, if they are, open them in the system's default browser
function iabLoad(event){
if(debug) alert('Page load request for ' + event.url);
//Check the link to see if it is external, which is flagged by #appExt from injection.js
if(event.url.indexOf('appExt') > -1){
//Open the window using the _system target to use the system's default browser
if(debug) alert('Opening '+event.url + ' in system window')
window.open(event.url,'_system');

if(debug) alert('Stopping anything more');
//Mobile redirects keep triggering load events from initial link, stop the process now.
window.stop(); // <-Does not stop
}
else{
if(debug) alert('Opening normally without interference');
}
}

//If window was hidden whilst loading, show it now that it has loaded
function iabLoaded(event){
if(debug) alert('Showing IAB window');
iabRef.show();
}

来自响应式网站中包含的 JavaScript 文件:

function isExternal(url) {
var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/);
if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return true;
if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host) return true;
return false;
}

function goToPage(url, external){
if(external)
//Go to new URL with appended hash flag
window.location=encodeURI(url)+'#appExt';
else{
//Show loading screen
$('#app-overlay').css('display','block');
//Wait for 100ms then change page, so the loading screen gets time to show
setTimeout(function(){window.location=encodeURI(url)},100);
}
}

$('a').on('click',function(e){
e.preventDefault();

if(isExternal($(this).attr('href'))){
//If the URL is an external one, the app.js file will open the url in a new window after setting the appExt hash flag
if(debug) alert('Stopping navigation from injection.js as external link detected');
goToPage($(this).attr('href'), true);
}
else{
//Otherwise, continue normally
if(debug) alert('Regular link clicked');
goToPage($(this).attr('href'), false);
}

//Cancel the default link operation, as we've already handled it
return false;
});

更新:继续调整和重新编译并没有让我有任何进一步的进展 - 有人能够为我关注这个吗?

最佳答案

我不确定这是最好的答案,但我通过跟踪最后访问的 URL(通过 startLoad 事件)以及当我看到有人链接到与该网站不同的网站时“解决”了这个问题已经加载(使用 startLoad 事件),我在外部 URL 上使用 _system 执行 window.open,然后将 InAppBrowser 的页面更改回它所在的最后一个 URL(我正在跟踪)。

不是最优雅的,但我找不到任何其他方法来做到这一点,而且看起来效果还不错。使用cordova 3.6.3(希望下周升级)和inAppBrowser 0.6.0。如果有人认为它可能有用,我可能可以发布代码片段。

关于javascript - 在 PhoneGap/Cordova 中使用 InAppBrowser 在默认浏览器中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com