Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jump to certain url when user clicks update notification #94

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
14 changes: 9 additions & 5 deletions src/services/background.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/services/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
6 changes: 6 additions & 0 deletions src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down