diff --git a/ChangeLog.md b/ChangeLog.md index 4e76c0a..c2dd68e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,8 @@ ## 1.0.5 * 粗暴地加入了“关注问题”的 记录/可视化的功能 * 略微优化了时间图的显示 + * 活动时间记录逻辑修改(虽然没有暴露给你们使用) + * 加入了恼人的升级提示 ## 1.0.4 * 点赞列表界面增加自动检测功能。 diff --git a/README.md b/README.md index 768a2d4..95ece3d 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,6 @@ * 本地数据库过大时,导入导出页面的`快速导入`/`快速导出`功能将不可靠(容易导致插件崩溃),需要用[自带工具](./DBExportor/)进行导出。 工具基于Asp.net core 2.0,请自行准备编译环境。 - 大数据库的导入未经测试,风险请自行承担。 ## Dependency * [Dexie.js](http://dexie.org/) A Minimalistic Wrapper for IndexedDB [Apache License 2.0](./License/Dexie.license) diff --git a/ZhiHuExt/CoreDB.js b/ZhiHuExt/CoreDB.js index 8051c75..d6f58cf 100644 --- a/ZhiHuExt/CoreDB.js +++ b/ZhiHuExt/CoreDB.js @@ -164,6 +164,27 @@ class ZhiHuDB this[table] = this.db[table]; } + /** + * @param {Zan[]} data + * @param {Map} acttime + */ + static formActTime(data, acttime) + { + if (!data) + return; + for (let idx = 0; idx < data.length; ++idx) + { + const act = data[idx]; + const oldtime = acttime.get(act.from); + if (!oldtime) + acttime.set(act.from, [act.time, act.time]); + else if (act.time > oldtime[0]) + acttime.set(act.from, [act.time, oldtime[1]]); + else if (act.time < oldtime[1]) + acttime.set(act.from, [oldtime[0], act.time]); + } + } + /** * @param {string} target * @param {object[] | object | StandardDB} data @@ -177,22 +198,18 @@ class ZhiHuDB Object.entries(data).forEach(([key, val]) => sum += this.insert(key, val)); if (notify) notify(sum); - if (data.zans && data.zanarts && data.followqsts) + /**@type {Map} */ + const acttime = new Map(); + ZhiHuDB.formActTime(data.zans, acttime); + ZhiHuDB.formActTime(data.zanarts, acttime); + ZhiHuDB.formActTime(data.followqsts, acttime); + if (acttime.size > 0) { - /**@type {Map} */ - const acttime = new Map(); - const curtime = new Date().toUTCSeconds(); - data.zans.concat(data.zanarts).concat(data.followqsts).forEach(act => + const recs = []; + for (const entry of acttime) { - const oldtime = acttime.get(act.from); - if (!oldtime) - acttime.set(act.from, [act.time, act.time]); - else if (act.time > oldtime[0]) - acttime.set(act.from, [act.time, oldtime[1]]); - else if (act.time < oldtime[1]) - acttime.set(act.from, [oldtime[0], act.time]); - }); - const recs = Array.from(acttime.entries()).map(x => ({ id: x[0], new: x[1][0], old: x[1][1] })); + recs.push({ id: entry[0], new: entry[1][0], old: entry[1][1]}); + }; this.db.rectime.bulkPut(recs); } return sum; diff --git a/ZhiHuExt/background.js b/ZhiHuExt/background.js index 3fd6b9e..0bef977 100644 --- a/ZhiHuExt/background.js +++ b/ZhiHuExt/background.js @@ -14,7 +14,32 @@ function clearBadge() { chrome.browserAction.setBadgeText({ text: "" }); } - +/**@param {string} str */ +function strToVer(str) +{ + const verstr = str.replace(/[^0-9.]/g, "").split(".").map(Number); + return verstr[0] * 10000 + verstr[1] * 100 + verstr[2]; +} +async function getVersion() +{ + const curver = strToVer(chrome.runtime.getManifest().version); + try + { + const resp = await fetch("https://api.github.com/repos/XZiar/ZhiHuExt/releases"); + const releases = await resp.json() + console.log("releases", releases); + releases.forEach(release => release.pubTime = new Date(release.published_at)); + releases.sort((a, b) => a.pubTime < b.pubTime); + const newver = strToVer(releases[0].tag_name); + console.log("latest release version", newver); + return { curver: curver, newver: newver }; + } + catch (e) + { + console.warn(e); + return { curver: curver, newver: 0 }; + } +} const db = new ZhiHuDB("ZhihuDB", [ { @@ -228,6 +253,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => sendResponse(result); }); return true; + case "chkver": + getVersion().then(result => sendResponse(result)); + return true; case "chkspam": { if (request.target === "users") @@ -487,9 +515,29 @@ chrome.runtime.onMessageExternal.addListener( } }); -$(document).ready(function () +(async function () { - new Clipboard('#copyBtn'); -}); + $(document).ready(() => + { + new Clipboard('#copyBtn'); + }); + const verinfo = await getVersion(); + if (verinfo.curver < verinfo.newver) + { + chrome.notifications.create("UpdInfo", + { + type: "basic", + title: "銆愮煡涔庣柉鐗涚梾銆戞洿鏂版彁绀", + message: `鎻掍欢浼间箮鍙戝竷鏂扮増鏈簡锛屽揩鍘荤湅鐪嬩綔鑰呭張鍔犱簡浠涔堝崕鑰屼笉瀹炵殑鍔熻兘鍚э紵锛乣, + iconUrl: "icon.png" + }); + } +})(); + +chrome.notifications.onButtonClicked.addListener(notificationId => +{ + if (notificationId === "UpdInfo") + chrome.tabs.create({ active: true, url: "https://www.github.com/XZiar/ZhiHuExt/releases" }); +}); diff --git a/ZhiHuExt/manifest.json b/ZhiHuExt/manifest.json index 2c74cd0..30f8dd8 100644 --- a/ZhiHuExt/manifest.json +++ b/ZhiHuExt/manifest.json @@ -25,7 +25,8 @@ "*://127.0.0.1:*/", "tabs", "storage", - "downloads" + "downloads", + "notifications" ], "externally_connectable": { diff --git a/ZhiHuExt/popup.js b/ZhiHuExt/popup.js index 428f05c..6e80962 100644 --- a/ZhiHuExt/popup.js +++ b/ZhiHuExt/popup.js @@ -37,30 +37,14 @@ function verToStr(ver) { return `v${Math.floor(ver / 10000)}.${Math.floor((ver % 10000) / 10)}.${ver % 10}`; } - -(async function() +chrome.runtime.sendMessage({ action: "chkver" }, resp => { - const curver = 10005; - $("#curver").text(verToStr(curver)); - try + $("#curver").text(verToStr(resp.curver)); + $("#newver").text(verToStr(resp.newver)); + if (curver < newver) { - const resp = await fetch("https://api.github.com/repos/XZiar/ZhiHuExt/releases"); - const releases = await resp.json() - console.log(releases); - releases.forEach(release => release.pubTime = new Date(release.published_at)); - releases.sort((a, b) => a.time < b.time); - const verstr = releases[0].tag_name.substring(1).split(".").map(Number); - const newver = verstr[0] * 10000 + verstr[1] * 100 + verstr[2]; - console.log(newver); - $("#newver").text(verToStr(newver)); - if (curver < newver) - { - $("#newver")[0].style.color = "red"; - $("#upd").show(); - } + $("#newver")[0].style.color = "red"; + $("#upd").show(); } - catch (e) - { - console.warn(e); - } -})(); \ No newline at end of file +}); +