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就绪!的主要内容,如果未能解决你的问题,请参考以下文章