From 8fd3a95e4a61134e483c1253bf8f61cfd04bb3fc Mon Sep 17 00:00:00 2001 From: LiuChangFreeman Date: Tue, 13 Apr 2021 13:15:01 +0800 Subject: [PATCH] feat: jump to certain url when user clicks update notification (#94) * distinguish prod and dev * open update url when click notification --- src/pages/Background/index.ts | 36 +++++++++++++++++++++++++++-------- src/services/background.ts | 14 +++++++++----- src/services/common.ts | 6 +++++- src/utils/metadata.ts | 6 ++++++ 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/pages/Background/index.ts b/src/pages/Background/index.ts index e2e51e51..71411a80 100644 --- a/src/pages/Background/index.ts +++ b/src/pages/Background/index.ts @@ -9,7 +9,12 @@ export enum BackgroundTasks { } // in develop-version, we can do this every 0.1 min, but not in store-version -chrome.alarms.create(BackgroundTasks.update,{periodInMinutes:0.1}); +if (process.env.NODE_ENV !== 'production') { + chrome.alarms.create(BackgroundTasks.update, { periodInMinutes: 0.1 }); +} +else{ + chrome.alarms.create(BackgroundTasks.update, { periodInMinutes: 30 }); +} chrome.alarms.onAlarm.addListener(async (alarm) => { const name=alarm.name; @@ -21,23 +26,38 @@ chrome.alarms.onAlarm.addListener(async (alarm) => { if(name===BackgroundTasks.update){ if(settings.checkForUpdates){ console.log("check for updates"); - const [currentVersion,latestVersion]=await checkUpdate(); + const [currentVersion,latestVersion,updateUrl]=await checkUpdate(); if(compareVersion(currentVersion,latestVersion)===-1){ const timeNow=new Date().valueOf(); const duration=timeNow-metaData.timeLastNoticeNewUpdate; console.log(duration); if(duration>=24*60*60*1000){ // only notice people once within 24 hours - createNotification('check_for_updates', { - type: 'basic', - iconUrl: 'main.png', - title: getMessageI18n('notification_title_newUpdate'), - message: getMessageI18n('notification_message_newUpdate').replace('%v', latestVersion), - }) + metaData.updateUrl=updateUrl; metaData.timeLastNoticeNewUpdate=timeNow; await chromeSet("meta_data", metaData.toJson()); + createNotification('check_for_updates', + { + type: 'basic', + iconUrl: 'main.png', + title: getMessageI18n('notification_title_newUpdate'), + message: getMessageI18n('notification_message_newUpdate').replace('%v', latestVersion), + } + ) } } } } +}); + +chrome.notifications.onClicked.addListener(async function(notificationId){ + switch (notificationId){ + case "check_for_updates": + const metaData=await loadMetaData(); + chrome.tabs.create({url:metaData.updateUrl}); + break; + default: + break; + } + chrome.notifications.clear(notificationId); }); \ No newline at end of file diff --git a/src/services/background.ts b/src/services/background.ts index 0f2984d3..89fa9b77 100644 --- a/src/services/background.ts +++ b/src/services/background.ts @@ -1,13 +1,17 @@ import { updateInformation } from '../mock/background.data'; import { sleep } from "../utils/utils" -const url_update=""; +const url_update="http://static.liuchangfreeman.xyz/files/static/update_information.json"; export const getUpdateInfor = async () => { let result=null; - // const response = await fetch(url_update); - // return await response.json(); - await sleep(1000); - result=updateInformation; + if (process.env.NODE_ENV !== 'production') { + await sleep(1000); + result=updateInformation; + } + else{ + const response = await fetch(url_update); + result= await response.json(); + } return result; } diff --git a/src/services/common.ts b/src/services/common.ts index 44978589..80025e09 100644 --- a/src/services/common.ts +++ b/src/services/common.ts @@ -8,18 +8,22 @@ export const checkUpdate= async ()=>{ const browserType=getBrowserType(); const updateInformation=await getUpdateInfor(); let latestVersion; + let updateUrl; if("key" in details){ // the store-version installation if(browserType==="Edge"){ latestVersion=updateInformation["edge"]["latest_version"]; + updateUrl=updateInformation["edge"]["url"]; } else{ latestVersion=updateInformation["chrome"]["latest_version"]; + updateUrl=updateInformation["chrome"]["url"]; } } else{ latestVersion=updateInformation["develop"]["latest_version"]; + updateUrl=updateInformation["develop"]["url"]; } - return [currentVersion,latestVersion]; + return [currentVersion,latestVersion,updateUrl]; } \ No newline at end of file diff --git a/src/utils/metadata.ts b/src/utils/metadata.ts index eedb5bad..7017f340 100644 --- a/src/utils/metadata.ts +++ b/src/utils/metadata.ts @@ -2,20 +2,26 @@ import { chromeGet, isNull } from './utils'; class MetaData { timeLastNoticeNewUpdate: number; + updateUrl:string; constructor() { this.timeLastNoticeNewUpdate=new Date().valueOf()-24*60*60*1000; + this.updateUrl="https://github.com/hypertrons/hypertrons-crx"; } loadFromJson(data: { [key: string]: any; }): void { if ("timeLastNoticeNewUpdate" in data) { this.timeLastNoticeNewUpdate = data["timeLastNoticeNewUpdate"]; } + if ("updateUrl" in data) { + this.updateUrl = data["updateUrl"]; + } } toJson(): { [key: string]: any; } { const result: { [key: string]: any; } = {}; result["timeLastNoticeNewUpdate"] = this.timeLastNoticeNewUpdate; + result["updateUrl"] = this.updateUrl; return result; } }