main.js 10 KB

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