gpt4 book ai didi

javascript - 在 Cordova Webview 中执行脚本

转载 作者:行者123 更新时间:2023-11-30 00:12:07 27 4
gpt4 key购买 nike

我正在使用 this InApp浏览器插件
按照答案中的建议进行操作后,我仍然无法正常工作。

我的 app.js

(function () {
'use strict';

var app = angular
.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform){
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});

app.controller("starter", function($scope, $rootScope, $cordovaInAppBrowser){

console.log("inside controller");

$cordovaInAppBrowser.open('https://google.com','_self', {location: "no"})
.then(function(event) {
console.log("success");
})
.catch(function(event) {
console.log(event)
});

$rootScope.$on('$cordovaInAppBrowser:loadstop', function(e, event){
console.log("loadstop called");
$cordovaInAppBrowser.executeScript({
code: 'alert("from app to browser");'
});
});

$rootScope.$on('$cordovaInAppBrowser:loaderror', function(e, event){
console.log(e);
});

})

angular.element(document).ready(function() {
document.addEventListener("deviceready", function() {
console.log("deviceready called");
angular.bootstrap(document, ["starter"]);
}, false);
});


}());

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>

<script src="cordova.js"></script>
<!-- your app's js -->
<script src="src/app.js"></script>
</head>
<body>
<ion-pane ng-controller="starter">
</ion-pane>
</body>
</html>

配置文件

 <content src="index.html"/>
<allow-navigation href="http://*/*"/>
<allow-navigation href="https://*/*"/>
<access browserOnly="true" origin="*" />
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="android-minSdkVersion" value="16"/>
<preference name="BackupWebStorage" value="none"/>
<preference name="SplashScreen" value="screen"/>
<preference name="SplashScreenDelay" value="5000"/>
<preference name="orientation" value="portrait"/>
<feature name="StatusBar">
<param name="ios-package" onload="true" value="CDVStatusBar"/>
</feature>
<platform name="android">
<icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
<icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
<icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
<icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
<icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
<icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
<splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
<splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
<splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
<splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
<splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
<splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
</platform>
<icon src="resources/android/icon/drawable-xhdpi-icon.png"/>

控制台输出

I/chromium(18704): [INFO:library_loader_hooks.cc(130)] Chromium logging enabled: level = 0, default verbosity = 0
I/chromium(18704): [INFO:CONSOLE(46)] "deviceready called", source: file:///android_asset/www/src/app.js (46)
I/chromium(18704): [INFO:CONSOLE(20)] "inside controller", source: file:///android_asset/www/src/app.js (20)

$cordovaInAppBrowser:loadstop 事件永远不会触发。 Cordova版本6.0.0,ionic版本1.7.14

最佳答案

如果您使用 _self,事件监听器将无法工作。

当您使用 _self 时,您是在 cordova webview 中加载网站,而不是 inAppBrowser webview,然后您将用您加载的网站中的代码替换所有现有代码,因此当您页面加载完成,你的代码已经消失,不会被执行。

您必须使用 _blank 打开真正的 inAppBrowser 窗口。

关于javascript - 在 Cordova Webview 中执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038076/

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