main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. mainWindow.on('maximize',function () {
  94. mainWindow.webContents.send('size', 1);
  95. });
  96. mainWindow.on('unmaximize',function () {
  97. mainWindow.webContents.send('size', -1);
  98. });
  99. }
  100. function CreateHistory() {
  101. if(BrowserHistory){
  102. BrowserHistory.show();
  103. return false;
  104. }
  105. BrowserHistory = new BrowserWindow({//历史记录
  106. width: 800,
  107. height:600,
  108. resizable:false,
  109. frame: false,
  110. parent:mainWindow
  111. });
  112. BrowserHistory.loadURL(url.format({
  113. pathname: path.join(__dirname, 'history.html'),
  114. protocol: 'file:',
  115. slashes: true
  116. }));
  117. BrowserHistory.on('closed', function () {
  118. BrowserHistory = null
  119. });
  120. }
  121. function createFeedBack(){
  122. if(BrowserFeedback){
  123. BrowserFeedback.show();
  124. return false;
  125. }
  126. BrowserFeedback = new BrowserWindow({
  127. width: 500,
  128. height:320,
  129. frame:false,
  130. modal :true,
  131. resizable: false,
  132. maximizable:false,
  133. minimizable:false,
  134. parent:mainWindow
  135. });
  136. BrowserFeedback.loadURL(url.format({
  137. pathname: path.join(__dirname, 'feedback.html'),
  138. protocol: 'file:',
  139. slashes: true
  140. }));
  141. BrowserFeedback.on('closed', function () {
  142. BrowserFeedback = null
  143. });
  144. }
  145. function CreateAboutUs() {
  146. AboutUsW = new BrowserWindow({
  147. width: 500,
  148. height:280,
  149. frame:false,
  150. modal :true,
  151. resizable: false,
  152. maximizable:false,
  153. minimizable:false,
  154. show:false,
  155. parent:mainWindow
  156. });
  157. AboutUsW.loadURL(url.format({
  158. pathname: path.join(__dirname, 'about.html'),
  159. protocol: 'file:',
  160. slashes: true
  161. }));
  162. AboutUsW.on('closed', function () {
  163. AboutUsW = null
  164. });
  165. }
  166. function CheckUpdate(event) {
  167. //当开始检查更新的时候触发
  168. autoUpdater.on('checking-for-update', function() {
  169. event.sender.send('check-for-update',message.checking);//返回一条信息
  170. });
  171. //当发现一个可用更新的时候触发,更新包下载会自动开始
  172. autoUpdater.on('update-available', function(info) {
  173. event.sender.send('update-down-success', info);
  174. event.sender.send('check-for-update',message.updateAva);//返回一条信息
  175. });
  176. //当没有可用更新的时候触发
  177. autoUpdater.on('update-not-available', function(info) {
  178. event.sender.send('check-for-update',message.updateNotAva);//返回一条信息
  179. });
  180. autoUpdater.on('error', function(error){
  181. event.sender.send('check-for-update',message.error);//返回一条信息
  182. });
  183. // 更新下载进度事件
  184. autoUpdater.on('download-progress', function(progressObj) {
  185. //这个事件无法使用
  186. mainWindow.webContents.send('download-progress',progressObj)
  187. });
  188. autoUpdater.on('update-downloaded', function () {
  189. event.sender.send('check-for-update',message.downloaded);//返回一条信息
  190. //通过main进程发送事件给renderer进程,提示更新信息
  191. });
  192. //执行自动更新检查
  193. }
  194. function BindIpc(){
  195. ipc.on('window-min', function () {
  196. CampusWindow.minimize();
  197. });
  198. ipc.on('window-max', function () {
  199. if (CampusWindow.isMaximized()) {
  200. CampusWindow.restore();
  201. } else {
  202. CampusWindow.maximize();
  203. }
  204. });
  205. ipc.on('window-close', function () {
  206. CampusWindow.close();
  207. });
  208. /*校园资讯写注册表*/
  209. regedit.putValue({
  210. 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run':{
  211. 'CampusInfo' : {
  212. value : process.argv[0]+' start',
  213. type : 'REG_SZ'
  214. }
  215. }
  216. },function () {
  217. });
  218. /*校园资讯打开浏览器*/
  219. ipcMain.on('open-browers',function (e,arg) {
  220. mainWindow.webContents.send('url', arg);
  221. mainWindow.focus();
  222. });
  223. /*浏览器ipc*/
  224. ipc.on('newBrowersWindow',function () {
  225. childProcess.exec(process.argv[0]);//启动新的浏览器
  226. });
  227. ipc.on('CampusInfo',function () {
  228. if(CampusWindow){
  229. CampusWindow.show();
  230. CampusWindow.restore();
  231. CampusWindow.focus();
  232. }else{
  233. createCampusWindow(true);
  234. }
  235. });
  236. ipc.on('window-all-closed', function () {//退出
  237. mainWindow.close();
  238. });
  239. ipc.on('hide-window', function () {//小化
  240. mainWindow.minimize();
  241. });
  242. ipc.on('show-window', function () {//最大化
  243. mainWindow.maximize();
  244. });
  245. ipc.on('orignal-window', function () {//还原
  246. mainWindow.restore();
  247. });
  248. ipc.on('elm',function (e,arg){
  249. mainWindow.webContents.send('elm', arg);
  250. });
  251. /*反馈窗口ipc*/
  252. ipc.on('BrowserFeedback-show', function () {
  253. createFeedBack();
  254. });
  255. ipc.on('BrowserFeedback-close', function () {//隐藏
  256. BrowserFeedback.hide();
  257. BrowserFeedback.close();
  258. BrowserFeedback=null;
  259. });
  260. /*历史记录ipc*/
  261. ipc.on('BrowserHistory-show', function () {//显示
  262. CreateHistory();
  263. });
  264. ipc.on('BrowserHistory-close', function () {//隐藏
  265. BrowserHistory.hide();
  266. BrowserHistory.close();
  267. BrowserHistory = null
  268. });
  269. /*关于我们IPC*/
  270. ipc.on('AboutUsW-show', function () {//显示
  271. AboutUsW.show();
  272. });
  273. ipc.on('AboutUsW-close', function () {//隐藏
  274. AboutUsW.hide();
  275. });
  276. /*关于我们获取版本信息*/
  277. ipcMain.on('get-version',function (event,arg) {
  278. event.sender.send('version',version);
  279. });
  280. /*接收打开历史记录*/
  281. ipc.on('open-history', function (event,arg) {//隐藏
  282. mainWindow.webContents.send('open-history', arg)
  283. });
  284. /*检查更新*/
  285. ipc.on('check-for-update', function(event, arg) {
  286. CheckUpdate(event);
  287. autoUpdater.checkForUpdates();
  288. });
  289. ipc.on('update', function(event, arg) {
  290. autoUpdater.quitAndInstall();
  291. });
  292. }
  293. const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  294. if (CampusWindow) {
  295. if (CampusWindow.isMinimized()){
  296. CampusWindow.restore()
  297. }
  298. CampusWindow.focus();
  299. CampusWindow.show();
  300. }
  301. });
  302. app.on('ready',function () {
  303. BindIpc();//ipc通信绑定
  304. if(process.argv[1]==='start'){
  305. createCampusWindow();
  306. }else{
  307. if(process.argv[1]&&process.argv[1].indexOf('--')<0) {
  308. createWindow(process.argv[1]);
  309. }
  310. createWindow();
  311. CreateAboutUs();
  312. if(!CampusWindow&&!shouldQuit){//让校园资讯在多个浏览器窗口开启的情况下也只有一个实例存在
  313. createCampusWindow();
  314. }
  315. }
  316. } );
  317. app.on('window-all-closed', function () {
  318. if (process.platform !== 'darwin') {
  319. app.quit()
  320. }
  321. });
  322. app.on('activate', function () {
  323. if (CampusWindow === null&&process.argv[1]==='browers') {
  324. createIndexWindow(true)
  325. }
  326. });