main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. const electron = require('electron');
  2. const ipc = electron.ipcMain;//用于接收命令的ipc模块
  3. const app = electron.app;
  4. const BrowserWindow = electron.BrowserWindow;//引入一个BrowserWindow
  5. const path = require('path');
  6. const url = require('url');//引入url处理模块
  7. const autoUpdater = require('electron-updater').autoUpdater;
  8. const {ipcMain} = require('electron');
  9. const dialog = require('electron').dialog;
  10. const Menu = electron.Menu;//引入菜单慕课
  11. const debug = (process.argv.indexOf('--debug')>0);
  12. let message={
  13. appName:'有思浏览器',
  14. error:'检查更新出错, 请联系开发人员',
  15. checking:'正在检查更新……',
  16. updateAva:'检测到新版本,正在下载……',
  17. updateNotAva:'现在使用的就是最新版本,不用更新',
  18. downloaded: '最新版本已下载,点击安装进行更新'
  19. };
  20. let childProcess =require('child_process');
  21. let CampusWindow,mainWindow,BrowserHistory,BrowserFeedback,AboutUsW;
  22. try {
  23. let flash=app.getPath('pepperFlashSystemPlugin');
  24. app.commandLine.appendSwitch('ppapi-flash-path',flash);
  25. }catch (e) {
  26. dialog.showErrorBox('缺少flash插件', '请前往https://www.flash.cn/下载该插件,点击确定后将打开flash下载地址');
  27. process.argv[1]='https://www.flash.cn/';
  28. }
  29. var version=require(__dirname+"/package.json").version;
  30. const startOnBoot = require('./startOnBoot.js');//自启动
  31. function createCampusWindow(flag) {
  32. if(CampusWindow){
  33. CampusWindow.show();
  34. CampusWindow.restore();
  35. CampusWindow.focus();
  36. return
  37. }
  38. createIndexWindow(false);
  39. if(!flag) {
  40. setTimeout(function () {
  41. if (CampusWindow) {
  42. CampusWindow.show();
  43. CampusWindow.restore();
  44. CampusWindow.focus();
  45. }
  46. }, 1000 * 60*5);
  47. return false;
  48. }
  49. }
  50. function createIndexWindow(flag) {
  51. CampusWindow = new BrowserWindow({ width: 750, height: 540, maxWidth: 750, maxHeight: 540,resizable:false,frame: false });
  52. CampusWindow.loadURL(url.format({
  53. pathname: path.join(__dirname, 'index.html'),
  54. protocol: 'file:',
  55. slashes: true
  56. }));
  57. if(debug) {
  58. CampusWindow.webContents.openDevTools();
  59. }
  60. CampusWindow.on('closed', function () {
  61. CampusWindow = null
  62. });
  63. if(!flag) {
  64. CampusWindow.hide();
  65. }
  66. }
  67. autoUpdater.setFeedURL('http://client.1473.cn/update');//设置检查更新的 url,并且初始化自动更新。这个 url 一旦设置就无法更改。
  68. function createWindow(request_url) {
  69. if(mainWindow){
  70. mainWindow.show();
  71. mainWindow.focus();
  72. return false;
  73. }
  74. mainWindow = new BrowserWindow({//游览器窗口
  75. width: 1920,
  76. height: 1080,
  77. backgroundColor:'#fff',
  78. webPreferences:{
  79. 'plugins': true
  80. },
  81. frame: false
  82. });
  83. mainWindow.loadURL(url.format({
  84. pathname: path.join(__dirname, 'browser.html'),
  85. protocol: 'file:',
  86. slashes: true
  87. }));
  88. if(debug) {
  89. mainWindow.webContents.openDevTools();
  90. }
  91. mainWindow.on('closed', function () {
  92. mainWindow = null
  93. });
  94. mainWindow.webContents.on('did-finish-load', function(){
  95. if(request_url) {
  96. mainWindow.webContents.send('flag', '1');
  97. mainWindow.webContents.send('url', request_url)
  98. }else {
  99. mainWindow.webContents.send('flag', '-1');
  100. }
  101. });
  102. mainWindow.focus();
  103. mainWindow.on('maximize',function () {
  104. mainWindow.webContents.send('size', 1);
  105. });
  106. mainWindow.on('unmaximize',function () {
  107. mainWindow.webContents.send('size', -1);
  108. });
  109. }
  110. function CreateHistory() {
  111. if(BrowserHistory){
  112. BrowserHistory.show();
  113. return false;
  114. }
  115. BrowserHistory = new BrowserWindow({//历史记录
  116. width: 800,
  117. height:600,
  118. resizable:false,
  119. frame: false,
  120. parent:mainWindow
  121. });
  122. BrowserHistory.loadURL(url.format({
  123. pathname: path.join(__dirname, 'history.html'),
  124. protocol: 'file:',
  125. slashes: true
  126. }));
  127. BrowserHistory.on('closed', function () {
  128. BrowserHistory = null
  129. });
  130. }
  131. function createFeedBack(){
  132. if(BrowserFeedback){
  133. BrowserFeedback.show();
  134. return false;
  135. }
  136. BrowserFeedback = new BrowserWindow({
  137. width: 500,
  138. height:320,
  139. frame:false,
  140. modal :true,
  141. resizable: false,
  142. maximizable:false,
  143. minimizable:false,
  144. parent:mainWindow
  145. });
  146. BrowserFeedback.loadURL(url.format({
  147. pathname: path.join(__dirname, 'feedback.html'),
  148. protocol: 'file:',
  149. slashes: true
  150. }));
  151. BrowserFeedback.on('closed', function () {
  152. BrowserFeedback = null
  153. });
  154. }
  155. function CreateAboutUs() {
  156. AboutUsW = new BrowserWindow({
  157. width: 500,
  158. height:280,
  159. frame:false,
  160. modal :true,
  161. resizable: false,
  162. maximizable:false,
  163. minimizable:false,
  164. show:false,
  165. parent:mainWindow
  166. });
  167. AboutUsW.loadURL(url.format({
  168. pathname: path.join(__dirname, 'about.html'),
  169. protocol: 'file:',
  170. slashes: true
  171. }));
  172. AboutUsW.on('closed', function () {
  173. AboutUsW = null
  174. });
  175. }
  176. function CheckUpdate(event) {
  177. //当开始检查更新的时候触发
  178. autoUpdater.on('checking-for-update', function() {
  179. event.sender.send('check-for-update',message.checking);//返回一条信息
  180. });
  181. //当发现一个可用更新的时候触发,更新包下载会自动开始
  182. autoUpdater.on('update-available', function(info) {
  183. event.sender.send('update-down-success', info);
  184. event.sender.send('check-for-update',message.updateAva);//返回一条信息
  185. });
  186. //当没有可用更新的时候触发
  187. autoUpdater.on('update-not-available', function(info) {
  188. event.sender.send('check-for-update',message.updateNotAva);//返回一条信息
  189. });
  190. autoUpdater.on('error', function(error){
  191. event.sender.send('check-for-update',message.error);//返回一条信息
  192. });
  193. // 更新下载进度事件
  194. autoUpdater.on('download-progress', function(progressObj) {
  195. //这个事件无法使用
  196. mainWindow.webContents.send('download-progress',progressObj)
  197. });
  198. autoUpdater.on('update-downloaded', function () {
  199. event.sender.send('check-for-update',message.downloaded);//返回一条信息
  200. //通过main进程发送事件给renderer进程,提示更新信息
  201. });
  202. //执行自动更新检查
  203. }
  204. function BindIpc(){
  205. ipc.on('window-min', function () {
  206. CampusWindow.minimize();
  207. });
  208. ipc.on('window-max', function () {
  209. if (CampusWindow.isMaximized()) {
  210. CampusWindow.restore();
  211. } else {
  212. CampusWindow.maximize();
  213. }
  214. });
  215. ipc.on('window-close', function () {
  216. CampusWindow.close();
  217. });
  218. /*校园资讯打开浏览器*/
  219. ipcMain.on('open-browers',function (e,arg) {
  220. mainWindow.webContents.send('url', arg);
  221. mainWindow.show();
  222. mainWindow.focus();
  223. });
  224. /*浏览器ipc*/
  225. ipc.on('newBrowersWindow',function () {
  226. childProcess.exec(process.argv[0]);//启动新的浏览器
  227. });
  228. ipc.on('CampusInfo',function () {
  229. createCampusWindow(true);
  230. });
  231. ipc.on('window-all-closed', function () {//退出
  232. mainWindow.close();
  233. });
  234. ipc.on('hide-window', function () {//小化
  235. mainWindow.minimize();
  236. });
  237. ipc.on('show-window', function () {//最大化
  238. mainWindow.maximize();
  239. });
  240. ipc.on('orignal-window', function () {//还原
  241. mainWindow.restore();
  242. });
  243. ipc.on('elm',function (e,arg){
  244. mainWindow.webContents.send('elm', arg);
  245. });
  246. /*反馈窗口ipc*/
  247. ipc.on('BrowserFeedback-show', function () {
  248. createFeedBack();
  249. });
  250. ipc.on('BrowserFeedback-close', function () {//隐藏
  251. BrowserFeedback.hide();
  252. BrowserFeedback.close();
  253. BrowserFeedback=null;
  254. });
  255. /*历史记录ipc*/
  256. ipc.on('BrowserHistory-show', function () {//显示
  257. CreateHistory();
  258. });
  259. ipc.on('BrowserHistory-close', function () {//隐藏
  260. BrowserHistory.hide();
  261. BrowserHistory.close();
  262. BrowserHistory = null
  263. });
  264. /*关于我们IPC*/
  265. ipc.on('AboutUsW-show', function () {//显示
  266. AboutUsW.show();
  267. });
  268. ipc.on('AboutUsW-close', function () {//隐藏
  269. AboutUsW.hide();
  270. });
  271. /*关于我们获取版本信息*/
  272. ipcMain.on('get-version',function (event,arg) {
  273. event.sender.send('version',version);
  274. });
  275. /*接收打开历史记录*/
  276. ipc.on('open-history', function (event,arg) {//隐藏
  277. mainWindow.webContents.send('open-history', arg)
  278. });
  279. /*检查更新*/
  280. ipc.on('check-for-update', function(event, arg) {
  281. CheckUpdate(event);
  282. autoUpdater.checkForUpdates();
  283. });
  284. ipc.on('update', function(event, arg) {
  285. autoUpdater.quitAndInstall();
  286. });
  287. }
  288. const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {});
  289. app.on('ready',function () {
  290. startOnBoot.enableAutoStart('有思浏览器', process.execPath+" start");
  291. BindIpc();//ipc通信绑定
  292. createWindow();
  293. if(process.argv[1]==='start'){
  294. mainWindow.hide();
  295. if (shouldQuit) {
  296. if (CampusWindow){
  297. CampusWindow.restore();
  298. CampusWindow.focus();
  299. CampusWindow.show();
  300. }
  301. app.quit();
  302. return;
  303. }
  304. createCampusWindow();
  305. }else{
  306. if(process.argv[1]&&process.argv[1].indexOf('--')<0) {
  307. createWindow(process.argv[1]);
  308. }
  309. createWindow();
  310. CreateAboutUs();
  311. /* if(!CampusWindow&&!shouldQuit){//让校园资讯在多个浏览器窗口开启的情况下也只有一个实例存在
  312. createCampusWindow();
  313. }*/
  314. }
  315. } );
  316. app.on('window-all-closed', function () {
  317. if (process.platform !== 'darwin') {
  318. app.quit()
  319. }
  320. });
  321. app.on('activate', function () {
  322. if (CampusWindow === null&&process.argv[1]==='browers') {
  323. createIndexWindow(true)
  324. }
  325. });