main.js 12 KB

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