从Javascript检查Cordova版本 [英] Check Cordova Version from Javascript

查看:36
本文介绍了从Javascript检查Cordova版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从Javascript检查应用程序正在运行哪个版本的Cordova?

Is there any way to check from Javascript what version of Cordova an app is running?

我为什么要问:

我们已将Cordova从2.8升级到4.0.2,并且 new Cordova JS文件不适用于 old Cordova Android代码.我们想强迫用户升级他们的应用程序(从而更新他们的Cordova Android版本),但是,我们需要首先检测到他们使用的是旧版本.

We've upgraded our Cordova from 2.8 to 4.0.2 and the new Cordova JS file does not work with the old Cordova Android code. We want to force the user to upgrade their app (to in turn update their Cordova Android version), however, we need to detect that they're on the old version first.

为什么device.cordova无法正常工作:

似乎旧的Cordova JS代码从未初始化,因为它无法与新的Cordova Android代码通信.因此,永远不会加载诸如 device plugin 之类的插件.我们在控制台中收到一条消息,指出:

It seems that the old Cordova JS code never initializes because it can't communicate with the new Cordova Android code. So the plugins, such as the device plugin are never loaded. We get a message in the console stating:

deviceready has not fired after 5 seconds

推荐答案

EDIT :现在最简单的解决方案是:

EDIT: now the simplest solution is this:https://stackoverflow.com/a/65476892/1243247

我制作了一个功能钩子脚本,并将其存储在 hooks/setVersion.js 中.我现在已经对其进行了测试,并且可以正常工作(仅在Android中,对于iOS,您只需要复制 wwwDir )

I made a functional hook script which I stored at hooks/setVersion.js. I just tested it now and it works (just in Android, for iOS you just need to replicate the wwwDir)

#!/usr/bin/env node
var wwwFileToReplace = 'index.html'

var fs = require('fs')
var path = require('path')

module.exports = function (context) {
  var projectRoot = context.opts.projectRoot
  const wwwDir = path.join(projectRoot, 'platforms', 'android', 'app', 'src', 'main', 'assets', 'www')

  var configXMLPath = 'config.xml'
  loadConfigXMLDoc(configXMLPath, (rawJSON) => {
    var version = rawJSON.widget.$.version
    console.log('Version:', version)

    var fullfilename = path.join(wwwDir, wwwFileToReplace)
    if (fs.existsSync(fullfilename)) {
      replaceStringInFile(fullfilename, '%%VERSION%%', version)
      console.log(context.hook + ': Replaced version in file: ' + path.relative(projectRoot, fullfilename))
    } else {
      console.error('File does not exist: ', path.relative(projectRoot, fullfilename))
      process.exit(1)
    }
  })
}

function loadConfigXMLDoc (filePath, callback) {
  var fs = require('fs')
  var xml2js = require('xml2js')
  try {
    var fileData = fs.readFileSync(filePath, 'ascii')
    var parser = new xml2js.Parser()
    parser.parseString(fileData.substring(0, fileData.length), function (err, result) {
      if (err) {
        console.error(err)
        process.exit(1)
      } else {
        // console.log("config.xml as JSON", JSON.stringify(result, null, 2))
        console.log("File '" + filePath + "' was successfully read.")
        callback(result)
      }
    })
  } catch (ex) {
    console.log(ex)
    process.exit(1)
  }
}

function replaceStringInFile (filename, toReplace, replaceWith) {
  var data = fs.readFileSync(filename, 'utf8')

  var result = data.replace(new RegExp(toReplace, 'g'), replaceWith)
  fs.writeFileSync(filename, result, 'utf8')
}

您还必须添加config.xml

You have also to add in config.xml

<hook src="hooks/setVersion.js" type="after_prepare"/>

此脚本用config.xml中的应用程序版本替换文件中的文本 %% VERSION %% ,因此您可以在 index.html 文件中添加一些内容喜欢

This script replaces the text %%VERSION%% in a file with the app version from config.xml, so you can have in your index.html file something like

<html data-appversion="%%VERSION%%">

和您的JS

const version = document.documentElement.getAttribute("data-appversion");

这篇关于从Javascript检查Cordova版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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