main.js 10 KB

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