main.js 10.0 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.on('closed', function () {
  81. mainWindow = null
  82. });
  83. mainWindow.webContents.on('did-finish-load', function(){
  84. if(request_url) {
  85. mainWindow.webContents.send('flag', '1');
  86. mainWindow.webContents.send('url', request_url)
  87. }else {
  88. mainWindow.webContents.send('flag', '-1');
  89. }
  90. });
  91. mainWindow.focus();
  92. mainWindow.on('maximize',function () {
  93. mainWindow.webContents.send('size', 1);
  94. });
  95. mainWindow.on('unmaximize',function () {
  96. mainWindow.webContents.send('size', -1);
  97. });
  98. }
  99. function CreateHistory() {
  100. if(BrowserHistory){
  101. BrowserHistory.show();
  102. return false;
  103. }
  104. BrowserHistory = new BrowserWindow({//历史记录
  105. width: 800,
  106. height:600,
  107. resizable:false,
  108. frame: false,
  109. parent:mainWindow
  110. });
  111. BrowserHistory.loadURL(url.format({
  112. pathname: path.join(__dirname, 'history.html'),
  113. protocol: 'file:',
  114. slashes: true
  115. }));
  116. BrowserHistory.on('closed', function () {
  117. BrowserHistory = null
  118. });
  119. }
  120. function createFeedBack(){
  121. if(BrowserFeedback){
  122. BrowserFeedback.show();
  123. return false;
  124. }
  125. BrowserFeedback = new BrowserWindow({
  126. width: 500,
  127. height:320,
  128. frame:false,
  129. modal :true,
  130. resizable: false,
  131. maximizable:false,
  132. minimizable:false,
  133. parent:mainWindow
  134. });
  135. BrowserFeedback.loadURL(url.format({
  136. pathname: path.join(__dirname, 'feedback.html'),
  137. protocol: 'file:',
  138. slashes: true
  139. }));
  140. BrowserFeedback.on('closed', function () {
  141. BrowserFeedback = null
  142. });
  143. }
  144. function CreateAboutUs() {
  145. AboutUsW = new BrowserWindow({
  146. width: 500,
  147. height:280,
  148. frame:false,
  149. modal :true,
  150. resizable: false,
  151. maximizable:false,
  152. minimizable:false,
  153. show:false,
  154. parent:mainWindow
  155. });
  156. AboutUsW.loadURL(url.format({
  157. pathname: path.join(__dirname, 'about.html'),
  158. protocol: 'file:',
  159. slashes: true
  160. }));
  161. AboutUsW.on('closed', function () {
  162. AboutUsW = null
  163. });
  164. }
  165. function CheckUpdate(event) {
  166. //当开始检查更新的时候触发
  167. autoUpdater.on('checking-for-update', function() {
  168. event.sender.send('check-for-update',message.checking);//返回一条信息
  169. });
  170. //当发现一个可用更新的时候触发,更新包下载会自动开始
  171. autoUpdater.on('update-available', function(info) {
  172. event.sender.send('update-down-success', info);
  173. event.sender.send('check-for-update',message.updateAva);//返回一条信息
  174. });
  175. //当没有可用更新的时候触发
  176. autoUpdater.on('update-not-available', function(info) {
  177. event.sender.send('check-for-update',message.updateNotAva);//返回一条信息
  178. });
  179. autoUpdater.on('error', function(error){
  180. event.sender.send('check-for-update',message.error);//返回一条信息
  181. });
  182. // 更新下载进度事件
  183. autoUpdater.on('download-progress', function(progressObj) {
  184. //这个事件无法使用
  185. mainWindow.webContents.send('download-progress',progressObj)
  186. });
  187. autoUpdater.on('update-downloaded', function () {
  188. event.sender.send('check-for-update',message.downloaded);//返回一条信息
  189. //通过main进程发送事件给renderer进程,提示更新信息
  190. });
  191. //执行自动更新检查
  192. }
  193. function BindIpc(){
  194. ipc.on('window-min', function () {
  195. CampusWindow.minimize();
  196. });
  197. ipc.on('window-max', function () {
  198. if (CampusWindow.isMaximized()) {
  199. CampusWindow.restore();
  200. } else {
  201. CampusWindow.maximize();
  202. }
  203. });
  204. ipc.on('window-close', function () {
  205. CampusWindow.close();
  206. });
  207. /*校园资讯写注册表*/
  208. console.log(process)
  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. });