main.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const electron = require('electron');
  2. const path = require('path');
  3. const url = require('url');
  4. const autoUpdater = require('electron-updater').autoUpdater;
  5. const ipc = electron.ipcMain;
  6. const {ipcMain} = require('electron');
  7. const app = electron.app;
  8. const BrowserWindow = electron.BrowserWindow;
  9. let mainWindow,BrowserHistory,BrowserFeedback,AboutUsW;
  10. let message={
  11. appName:'有思浏览器',
  12. error:'检查更新出错, 请联系开发人员',
  13. checking:'正在检查更新……',
  14. updateAva:'检测到新版本,正在下载……',
  15. updateNotAva:'现在使用的就是最新版本,不用更新',
  16. downloaded: '最新版本已下载,点击安装已进行更新'
  17. };
  18. const debug = (process.argv.indexOf('--debug')>0);
  19. var version=require(__dirname+"/package.json").version;
  20. autoUpdater.setFeedURL('http://client.1473.cn/update');//设置检查更新的 url,并且初始化自动更新。这个 url 一旦设置就无法更改。
  21. function createWindow() {
  22. mainWindow = new BrowserWindow({//游览器窗口
  23. width: 1920,
  24. height: 1080,
  25. frame: false
  26. });
  27. mainWindow.loadURL(url.format({
  28. pathname: path.join(__dirname, 'browser.html'),
  29. protocol: 'file:',
  30. slashes: true
  31. }));
  32. if(debug) {
  33. mainWindow.webContents.openDevTools();
  34. }
  35. mainWindow.on('closed', function () {
  36. mainWindow = null
  37. });
  38. }
  39. function CreateHistory() {
  40. if(BrowserHistory){
  41. BrowserHistory.show();
  42. return false;
  43. }
  44. BrowserHistory = new BrowserWindow({//历史记录
  45. width: 800,
  46. height:600,
  47. resizable:false,
  48. frame: false,
  49. parent:mainWindow
  50. });
  51. BrowserHistory.loadURL(url.format({
  52. pathname: path.join(__dirname, 'history.html'),
  53. protocol: 'file:',
  54. slashes: true
  55. }));
  56. BrowserHistory.on('closed', function () {
  57. BrowserHistory = null
  58. });
  59. }
  60. function createFeedBack(){
  61. if(BrowserFeedback){
  62. BrowserFeedback.show();
  63. return false;
  64. }
  65. BrowserFeedback = new BrowserWindow({
  66. width: 500,
  67. height:320,
  68. frame:false,
  69. modal :true,
  70. resizable: false,
  71. maximizable:false,
  72. minimizable:false,
  73. parent:mainWindow
  74. });
  75. BrowserFeedback.loadURL(url.format({
  76. pathname: path.join(__dirname, 'feedback.html'),
  77. protocol: 'file:',
  78. slashes: true
  79. }));
  80. BrowserFeedback.on('closed', function () {
  81. BrowserFeedback = null
  82. });
  83. }
  84. function CreateAboutUs() {
  85. AboutUsW = new BrowserWindow({
  86. width: 500,
  87. height:280,
  88. frame:false,
  89. modal :true,
  90. resizable: false,
  91. maximizable:false,
  92. minimizable:false,
  93. show:false,
  94. parent:mainWindow
  95. });
  96. AboutUsW.loadURL(url.format({
  97. pathname: path.join(__dirname, 'about.html'),
  98. protocol: 'file:',
  99. slashes: true
  100. }));
  101. AboutUsW.on('closed', function () {
  102. AboutUsW = null
  103. });
  104. }
  105. function CheckUpdate(event) {
  106. //当开始检查更新的时候触发
  107. autoUpdater.on('checking-for-update', function() {
  108. event.sender.send('check-for-update',message.checking);//返回一条信息
  109. });
  110. //当发现一个可用更新的时候触发,更新包下载会自动开始
  111. autoUpdater.on('update-available', function(info) {
  112. event.sender.send('update-down-success', info);
  113. event.sender.send('check-for-update',message.updateAva);//返回一条信息
  114. });
  115. //当没有可用更新的时候触发
  116. autoUpdater.on('update-not-available', function(info) {
  117. event.sender.send('check-for-update',message.updateNotAva);//返回一条信息
  118. });
  119. autoUpdater.on('error', function(error){
  120. event.sender.send('check-for-update',message.error);//返回一条信息
  121. });
  122. // 更新下载进度事件
  123. autoUpdater.on('download-progress', function(progressObj) {
  124. //这个事件无法使用
  125. mainWindow.webContents.send('download-progress',progressObj)
  126. });
  127. autoUpdater.on('update-downloaded', function () {
  128. event.sender.send('check-for-update',message.downloaded);//返回一条信息
  129. //通过main进程发送事件给renderer进程,提示更新信息
  130. });
  131. //执行自动更新检查
  132. }
  133. function BindIpc(){
  134. /*浏览器ipc*/
  135. ipc.on('window-all-closed', function () {//退出
  136. app.quit();
  137. });
  138. ipc.on('hide-window', function () {//小化
  139. mainWindow.minimize();
  140. });
  141. ipc.on('show-window', function () {//最大化
  142. mainWindow.maximize();
  143. });
  144. ipc.on('orignal-window', function () {//还原
  145. mainWindow.unmaximize();
  146. });
  147. /*反馈窗口ipc*/
  148. ipc.on('BrowserFeedback-show', function () {
  149. createFeedBack();
  150. });
  151. ipc.on('BrowserFeedback-close', function () {//隐藏
  152. BrowserFeedback.hide();
  153. BrowserFeedback.close();
  154. BrowserFeedback=null;
  155. });
  156. /*历史记录ipc*/
  157. ipc.on('BrowserHistory-show', function () {//显示
  158. CreateHistory();
  159. });
  160. ipc.on('BrowserHistory-close', function () {//隐藏
  161. BrowserHistory.hide();
  162. BrowserHistory.close();
  163. BrowserHistory = null
  164. });
  165. /*关于我们IPC*/
  166. ipc.on('AboutUsW-show', function () {//显示
  167. AboutUsW.show();
  168. });
  169. ipc.on('AboutUsW-close', function () {//隐藏
  170. AboutUsW.hide();
  171. });
  172. /*关于我们获取版本信息*/
  173. ipcMain.on('get-version',function (event,arg) {
  174. event.sender.send('version',version);
  175. });
  176. /*接收打开历史记录*/
  177. ipc.on('open-history', function (event,arg) {//隐藏
  178. mainWindow.webContents.send('open-history', arg)
  179. });
  180. /*检查更新*/
  181. ipc.on('check-for-update', function(event, arg) {
  182. CheckUpdate(event);
  183. autoUpdater.checkForUpdates();
  184. });
  185. ipc.on('update', function(event, arg) {
  186. autoUpdater.quitAndInstall();
  187. });
  188. }
  189. app.on('ready', function (){
  190. createWindow();//打开窗口
  191. BindIpc();//ipc通信绑定
  192. CreateAboutUs();
  193. mainWindow.webContents.on('did-finish-load', function(){
  194. if(process.argv[1]&&process.argv[1].indexOf('--')<0) {
  195. mainWindow.webContents.send('url', process.argv[1])
  196. }
  197. });
  198. });
  199. app.on('window-all-closed', function () {
  200. if (process.platform !== 'darwin') {
  201. app.quit()
  202. }
  203. });
  204. app.on('activate', function () {
  205. if (mainWindow === null) {
  206. createWindow()
  207. }
  208. });