main.js 9.7 KB

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