diff --git a/src/assets/img/128.png b/src/assets/img/main.png similarity index 100% rename from src/assets/img/128.png rename to src/assets/img/main.png diff --git a/src/components/OptionsPage/index.tsx b/src/components/OptionsPage/index.tsx index 7519386d..773b6f0c 100644 --- a/src/components/OptionsPage/index.tsx +++ b/src/components/OptionsPage/index.tsx @@ -4,7 +4,7 @@ import { } from 'office-ui-fabric-react'; import { initializeIcons } from '@uifabric/icons'; import { getMessageI18n, chromeGet, chromeSet, isNull } from '../../utils/utils' -import Settings from "../../utils/settings" +import Settings,{ loadSettings } from "../../utils/settings" import './index.css'; initializeIcons(); @@ -13,21 +13,25 @@ const OptionsPage: React.FC = () => { const [settings, setSettings] = useState(new Settings()); const [inited, setInited] = useState(false); + const [version, setVersion] = useState("0.0.0"); useEffect(() => { const initSettings = async () => { - let obj = await chromeGet("settings"); - if (isNull(obj)) { - obj = {}; - } - settings.loadFromJson(obj); - setSettings(settings); + const temp=await loadSettings(); + setSettings(temp); setInited(true); } initSettings(); }, [settings]); - const saveSettings = async () => { + useEffect(() => { + // @ts-ignore + const details=chrome.app.getDetails(); + setVersion(details["version"]); + + }, [version]); + + const saveSettings = async (settings:Settings) => { setSettings(settings); const obj = settings.toJson(); await chromeSet("settings", obj); @@ -41,7 +45,7 @@ const OptionsPage: React.FC = () => {

HYPERTRONS

- version 0.1.2 + {`version ${version}`}
@@ -58,12 +62,22 @@ const OptionsPage: React.FC = () => { childrenGap: 10 }} > + { + settings.checkForUpdates = checked; + await saveSettings(settings); + }} + /> { settings.developerNetwork = checked; - await saveSettings(); + await saveSettings(settings); }} /> { defaultChecked={settings.projectNetwork} onChange={async (e, checked) => { settings.projectNetwork = checked; - await saveSettings(); - }} - /> - - { - }} - > - {getMessageI18n("global_btn_ok")} - - - { - settings.checkForUpdates = checked; - await saveSettings(); + await saveSettings(settings); }} /> + diff --git a/src/components/PopupPage/index.tsx b/src/components/PopupPage/index.tsx index e53bd25b..73a456ff 100644 --- a/src/components/PopupPage/index.tsx +++ b/src/components/PopupPage/index.tsx @@ -4,8 +4,7 @@ import { } from 'office-ui-fabric-react'; import { initializeIcons } from '@uifabric/icons'; import './index.css'; -import Settings from '../../utils/settings'; -import { chromeGet,isNull} from '../../utils/utils'; +import Settings,{ loadSettings } from '../../utils/settings'; initializeIcons(); @@ -16,12 +15,8 @@ const PopupPage: React.FC = () => { useEffect(() => { const initSettings=async ()=> { - let obj=await chromeGet("settings"); - if(isNull(obj)){ - obj={}; - } - settings.loadFromJson(obj); - setSettings(settings); + const temp=await loadSettings(); + setSettings(temp); setInited(true); } initSettings(); diff --git a/src/manifest.json b/src/manifest.json index 95edf318..7448bd00 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -3,7 +3,7 @@ "default_locale": "zh_CN", "options_page": "options.html", "icons": { - "128": "128.png" + "128": "main.png" }, "background": { "scripts": ["background.bundle.js"], @@ -11,7 +11,7 @@ }, "browser_action": { "default_popup": "popup.html", - "default_icon": "128.png" + "default_icon": "main.png" }, "content_scripts": [{ "matches": ["*://github.com/*"], @@ -20,7 +20,7 @@ }], "web_accessible_resources": [ "content.styles.css", - "128.png" + "main.png" ], "permissions": [ "*://github.com/*", diff --git a/src/mock/background.data.ts b/src/mock/background.data.ts new file mode 100644 index 00000000..decf0935 --- /dev/null +++ b/src/mock/background.data.ts @@ -0,0 +1,17 @@ +export const updateInformation = { + "chrome": { + "latest_version":"0.1.2", + "date":"2021-4-1", + "url":"https://github.com/hypertrons/hypertrons-crx/releases" + }, + "edge": { + "latest_version":"0.1.2", + "date":"2021-3-1", + "url":"https://github.com/hypertrons/hypertrons-crx/releases" + }, + "develop": { + "latest_version":"0.1.2", + "date":"2021-3-27", + "url":"https://github.com/hypertrons/hypertrons-crx/releases" + } +} \ No newline at end of file diff --git a/src/pages/Background/index.ts b/src/pages/Background/index.ts index 43895b21..2dbf84c3 100644 --- a/src/pages/Background/index.ts +++ b/src/pages/Background/index.ts @@ -1,12 +1,43 @@ +import { compareVersion, getBrowserType} from '../../utils/utils'; +import { getUpdateInfor } from "../../services/background" +import { loadSettings } from '../../utils/settings'; + export enum BackgroundTasks { update = 'check_for_updates' } -chrome.alarms.create(BackgroundTasks.update,{periodInMinutes:10}); +chrome.alarms.create(BackgroundTasks.update,{periodInMinutes:0.1}); -chrome.alarms.onAlarm.addListener((alarm) => { +chrome.alarms.onAlarm.addListener(async (alarm) => { const name=alarm.name; - if(name===BackgroundTasks.update){ - console.log("check for updates"); + + const settings=await loadSettings(); + + if(name===BackgroundTasks.update ){ + if(settings.checkForUpdates){ + // @ts-ignore :we must ignore here + const details=chrome.app.getDetails(); + const currentVersion=details["version"]; + const browserType=getBrowserType(); + const updateInformation=await getUpdateInfor(); + let latestVersion; + + if("key" in details){ + // the store-version installation + if(browserType==="Edge"){ + latestVersion=updateInformation["edge"]["latest_version"]; + } + else{ + latestVersion=updateInformation["chrome"]["latest_version"]; + } + } + else{ + latestVersion=updateInformation["develop"]["latest_version"]; + } + + if(compareVersion(currentVersion,latestVersion)===-1){ + console.log(`new ${browserType} update available, version:${latestVersion}`); + } + } } }); \ No newline at end of file diff --git a/src/services/background.ts b/src/services/background.ts new file mode 100644 index 00000000..b1b5325b --- /dev/null +++ b/src/services/background.ts @@ -0,0 +1,8 @@ +import { updateInformation } from '../mock/background.data'; +const url_update=""; + +export const getUpdateInfor = async () => { + // const response = await fetch(url_update); + // return await response.json(); + return updateInformation; +} \ No newline at end of file diff --git a/src/utils/settings.ts b/src/utils/settings.ts index 6a4438f6..6dc134d0 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -1,3 +1,5 @@ +import { chromeGet, isNull } from './utils'; + class Settings { checkForUpdates: boolean | undefined; developerNetwork: boolean | undefined; @@ -31,4 +33,14 @@ class Settings { } } +export const loadSettings=async ()=>{ + const settings=new Settings() + let obj = await chromeGet("settings"); + if (isNull(obj)) { + obj = {}; + } + settings.loadFromJson(obj); + return settings +} + export default Settings; \ No newline at end of file diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 5a0545f7..1c1b80e1 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -111,3 +111,61 @@ export const minMaxRange = (data: any, key: string, MIN: number, MAX: number) => } return data; } + +export const compareVersion=(version_1:string,version_2:string)=>{ + const v1 = version_1.split('.'); + const v2 = version_2.split('.'); + const len = Math.max(v1.length, v2.length); + + while (v1.length < len) { + v1.push('0'); + } + while (v2.length < len) { + v2.push('0'); + } + + for (let i = 0; i < len; i++) { + const num_1 = parseInt(v1[i]); + const num_2 = parseInt(v2[i]); + + if (num_1 > num_2) { + return 1; + } + else if (num_1 < num_2) { + return -1; + } + } + + return 0; +} + +export const getBrowserType=()=> { + var userAgent = navigator.userAgent; + var isOpera = userAgent.indexOf("Opera") > -1; + var isEdge = userAgent.indexOf("Edge") > -1; + var isFF = userAgent.indexOf("Firefox") > -1; + var isSafari = userAgent.indexOf("Safari") > -1 + && userAgent.indexOf("Chrome") === -1; + var isChrome = userAgent.indexOf("Chrome") > -1 + && userAgent.indexOf("Safari") > -1; + + if (isOpera) { + return "Opera"; + } + else if (isEdge) { + return "Edge"; + } + else if (isFF) { + return "FireFox"; + } + else if (isSafari) { + return "Safari"; + } + else if (isChrome) { + return "Chrome"; + } + else{ + return "Unknown"; + } + +} \ No newline at end of file