Skip to content

Commit

Permalink
new update check&notify
Browse files Browse the repository at this point in the history
new rectime collecting
final 1.0.5
  • Loading branch information
XZiar committed Mar 11, 2018
1 parent fcc0a1f commit 699079a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 44 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 1.0.5
* 粗暴地加入了“关注问题”的 记录/可视化的功能
* 略微优化了时间图的显示
* 活动时间记录逻辑修改(虽然没有暴露给你们使用)
* 加入了恼人的升级提示

## 1.0.4
* 点赞列表界面增加自动检测功能。
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 31 additions & 14 deletions ZhiHuExt/CoreDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ class ZhiHuDB
this[table] = this.db[table];
}

/**
* @param {Zan[]} data
* @param {Map<string,number[]>} 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
Expand All @@ -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<string,number[]>} */
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<string,number[]>} */
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;
Expand Down
56 changes: 52 additions & 4 deletions ZhiHuExt/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", [
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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" });
});


3 changes: 2 additions & 1 deletion ZhiHuExt/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"*://127.0.0.1:*/",
"tabs",
"storage",
"downloads"
"downloads",
"notifications"
],
"externally_connectable":
{
Expand Down
32 changes: 8 additions & 24 deletions ZhiHuExt/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();
});

0 comments on commit 699079a

Please sign in to comment.