main.js 12 KB

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