javascript 使用此脚本将您的index.html of create-react-app转换为index.html cordova就绪!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用此脚本将您的index.html of create-react-app转换为index.html cordova就绪!相关的知识,希望对你有一定的参考价值。

let FS = require('fs');

// read the index.html from build folder
let data = FS.readFileSync('./build/index.html', 'utf8');

function insertContent(fullContent, beforeWhat, newContent) {
  // get the position before which newContent has to be added
  const position = fullContent.indexOf(beforeWhat);

  // since splice can be used on arrays only
  let fullContentCopy = fullContent.split('');
  fullContentCopy.splice(position, 0, newContent);

  return fullContentCopy.join('');
}

// will add the <meta> tags needed for cordova app
const afterAddingMeta = insertContent(data, '<link',
  '<meta http-equiv="Content-Security-Policy" content="default-src *; style-src \'self\' http://* \'unsafe-inline\'; script-src \'self\' http://* \'unsafe-inline\' \'unsafe-eval\'" />' +
  '<meta name="format-detection" content="telephone=no">' +
  '<meta name="msapplication-tap-highlight" content="no">');
// will add <script> pointing to cordova.js
const afterAddingScript = insertContent(afterAddingMeta, '<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"', '<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script>');

// updates the index.html file
FS.writeFile('./build/index.html', afterAddingScript, 'utf8', (err) => {
  if (err) {
    throw err;
  };
  console.log('Cordova metas and scripts has been added !');
});

以上是关于javascript 使用此脚本将您的index.html of create-react-app转换为index.html cordova就绪!的主要内容,如果未能解决你的问题,请参考以下文章

为啥将您的 Javascript 文件移动到您也拥有的不同主域?

JavaScript 如何将您的网站重定向到移动版本

使用电容器将您的网站转换为安卓应用程序

如何将您的应用添加到通知中心?

javascript 适用于Samsung SmartTV平台的简单调试服务器。只需启动服务器,然后将您的机器连接到电视,它就会打印出来

使用JavaScript调用手机平台上的原生API