main.js 11 KB

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