此问题悬赏金(10元),帮助我解决问题即可获得,联系方式:1109039558(QQ)
我写了一个electron文件,文件中包含index.html(主)和test.html
单独运行test页面,功能没有任何问题
但是从index页面进入test页面,test页面无法正常运行.
原因可能如下,我在test导入bcli.js库,这个js是我自己写的,里面有一些函数,如check123 opening123等等
<script> require("./js/electron/bcli.js"); </script>
我上网上查了好多资料,都没有任何用,包括下面这个
webPreferences: {
nodeIntegration: true
}
我现在很无奈,求各位大佬帮帮我,错误问题如下,其中check123,我定义在了bcli.js中.
Uncaught ReferenceError: check123 is not defined
刚才一位朋友提出的建议,我试了,代码如下:上面两行代码没有注释时,结果可以运行,可是我将上面两行代码改成下面两行,结果运行不了.
// const $ = require('jquery'); // jQuery
// const shell = require('shelljs'); // cli executor
import {$} from require('jquery');
import {shell} from require('shelljs');
可能是我理解有问题,上面的问题,我想在描述一下,我普通的function可以运行,可是当我在支页面运行我写的代码,就运行不了,我把我写的代码附上去:
// libraries
const os = require('os'); // operating system
const path = require('path'); // path joiner
const $ = require('jquery'); // jQuery
const shell = require('shelljs'); // cli executor
// global varialbes, DO NOT CHANGE
let executing = false;
let imgNotFound = false;
let imgID = "#shrimp";
var fs=require("fs")
const {dialog}=require('electron').remote
opendialog=function(){
if (executing) return;
let imgPathes = [];
dialog.showOpenDialog({
title:"选择文件:",
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'bmp'] },
]
},result => {
if (executing) return; // do nothing if we are already executing the files
// otherwise try to get pdf pathes user has dropped to us
let imgPathes = [];
imgPathes.push(result);
fs.writeFile("/home/tju/桌面/abcd28/path1.txt",result,(err,data)=>{
if(err) throw err;
})
let len = imgPathes.length;
var i=0;
if (len >= 1) { // if we got 1 or more pdf pathes, start executing
console.log(imgPathes[i]);
execute(imgPathes[i]);
i=i+1;
} else {
console.error("Need exact 1 image");
}
})
}
function rmImg(imgPath) {
let str1=`cp -r `+ imgPath + ` `+` /home/tju/桌面/xuejie/train/SRGAN_CL/input/new.jpg`;
fs.writeFile("./path.txt",str1,(err,data)=>{
if(err) throw err;
})
let str=str1 +` && cd /home/tju/桌面/xuejie/train/SRGAN_CL && python test2.py --path1=/home/tju/桌面/xuejie/train/SRGAN_CL/input/new.jpg`;
fs.writeFile("/home/tju/桌面/cmd.txt",str,(err,data)=>{
if(err) throw err;
})
let cmd = str;
shell.exec(cmd, {silent:false}, (code, stdout, stderr)=>{
if(code === 0){
console.log({code: code, stdout: stdout, stderr: stderr});
}else{
console.error({code: code, stdout: stdout, stderr: stderr});
}
});
};
$('#refresh').click(()=>{
// $('#input1').load(location.href+'#input1');
setTimeout(() => {
javascript:location.reload();
}, 10000);
});
async function execute(imgPath) {
executing = true; // tell ui we are doing executing!
try {
let result = await rmImg(imgPath);
console.log(result);
} catch (err) { // will catch >>first<< error in the try block
console.error(error);
}
executing = false; // tell ui the shrink is done
}
check123=function(){
var textarea=this.document.querySelector('#textarea');
fs.readFile('/home/tju/桌面/abcd28/path1.txt','utf-8',function(err,data){
if(err){
console.error(err);
}
else{
textarea.innerHTML= data;
}
});
var textarea1=this.document.querySelector('#textarea1');
fs.readFile('/home/tju/桌面/xuejie/train/SRGAN_CL/sta_2019_16_1080/shuchu.csv','utf-8',function(err,data){
if(err){
console.error(err);
}
else{
textarea1.innerHTML= data;
}
});
}
console.log("script loaded");