Skip to content

Commit d4eac51

Browse files
authored
Merge pull request #1 from JackyHe398/beta
Beta
2 parents 21ae25e + cd47c73 commit d4eac51

File tree

4 files changed

+193
-63
lines changed

4 files changed

+193
-63
lines changed

public/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "原神官方論壇自動簽到",
3-
"version": "3.9",
3+
"version": "3.9.2",
44
"manifest_version": 3,
55
"description": "自動簽到,省去每天還要簽到的麻煩\n每日00:05自動開啟簽到網頁並簽到(可自由調整簽到時間)",
66
"background": {
@@ -30,7 +30,7 @@
3030
"browser_specific_settings": {
3131
"gecko": {
3232
"id": "genshin-auto-sign@JackyHe398",
33-
"strict_min_version": "58.0"
33+
"strict_min_version": "109.0"
3434
}
3535
}
3636
}

src/background.ts

Lines changed: 186 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,202 @@
11
import { IConfigType } from "./interface/IConfigType";
22

3-
function checkSign() {
4-
chrome.storage.sync.get(["lastDate", "signTime", "open"], (data) => {
5-
console.log("start check sign...");
6-
let { lastDate, open, signTime } = data as IConfigType;
7-
if (!data.urls) {
8-
chrome.storage.sync.set({
9-
urls: [],
10-
});
11-
}
12-
if (!lastDate) {
13-
chrome.storage.sync.set({
14-
lastDate: new Date().getDate(), //上次簽到日期
15-
});
16-
}
17-
18-
if (!signTime) {
19-
chrome.storage.sync.set({
20-
signTime: {
21-
hours: 0,
22-
minutes: 5,
23-
},
24-
});
25-
return;
26-
}
3+
function initializeDefaults(): Promise<void> {
4+
return new Promise((resolve) => {
5+
chrome.storage.sync.get(["lastDate", "signTime", "open"], (data) => {
6+
const updates: Record<string, any> = {};
7+
const { lastDate, signTime, open } = data as IConfigType;
278

28-
if (typeof open === "undefined") {
29-
chrome.storage.sync.set({
30-
open: true,
31-
});
32-
return;
33-
}
9+
/* firefox 編者注: 這段沒看到拿來幹嘛的*/
10+
// if (typeof data.urls === "undefined") {
11+
// chrome.storage.sync.set({
12+
// urls: [],
13+
// });
14+
// }
3415

35-
let h = Number(signTime.hours);
36-
let m = Number(signTime.minutes);
16+
if (!lastDate) {
17+
console.log("Initialize lastDate");
18+
updates.lastDate = new Date().getDate()
19+
}
20+
21+
if (!signTime ||
22+
typeof signTime === "undefined" ||
23+
typeof signTime.hours === "undefined" ||
24+
typeof signTime.minutes === "undefined") {
25+
console.log("Initialize signTime");
26+
updates.signTime = { hours: 0, minutes: 5 };
27+
// continue to check sign for not missing a day
28+
}
29+
30+
if (typeof open === "undefined") {
31+
console.log("Initialize open");
32+
updates.open = true;
33+
}
34+
35+
if (Object.keys(updates).length === 0) {
36+
resolve(); // nothing to update
37+
} else {
38+
chrome.storage.sync.set(updates, () => resolve());
39+
}
40+
});
41+
});
42+
}
3743

44+
async function checkSign() {
45+
await initializeDefaults();
46+
chrome.storage.sync.get(["lastDate", "signTime", "open"], (data) => {
3847
let now = new Date(); //目前時間
39-
let year = now.getFullYear();
40-
let month = now.getMonth();
41-
let day = now.getDate();
42-
let old = new Date(year, month, day, h, m);
48+
console.log(`${now.toLocaleTimeString()} start check sign`);
49+
50+
// construct time now and target time for comparison
51+
let { lastDate, open, signTime } = data as IConfigType;
52+
const h = +signTime.hours,
53+
m = +signTime.minutes;
54+
const todayAtHM = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m);
55+
56+
// condition check
57+
if (!open){
58+
console.log("Sign-in failed: disabled");
59+
return;
60+
};
61+
if (now < todayAtHM) {
62+
console.log("Sign-in failed: Not yet time to sign in");
63+
return;
64+
};; //
65+
if (now.getDate() === lastDate) {
66+
console.log("Sign-in failed: Signed today");
67+
return;
68+
};; // already signed today
4369

44-
/* debug */
70+
// region - debug
4571
// console.clear();
4672
// console.log(data);
4773
// console.log("currentDate:", now);
4874
// console.log(now.getDate(), lastDate);
4975
// console.log(now.getDate() !== lastDate);
50-
// console.log(now > old);
51-
52-
//如果日期不同且大於設定時間的話就自動開網頁簽到
53-
if (open && now.getDate() !== lastDate && now > old) {
54-
//簽到後用目前時間覆蓋掉上次時間,防止重複開啟網頁
55-
chrome.storage.sync.set({
56-
lastDate: new Date().getDate(),
57-
});
58-
59-
//開啟米哈遊的簽到頁面
60-
//這邊不需要做任何簽到動作,因為content.ts裡面已經設定只要開啟米哈遊網頁就會自動簽到了
61-
chrome.tabs.create({
62-
url: "https://act.hoyolab.com/ys/event/signin-sea-v3/index.html?act_id=e202102251931481",
63-
active: false, //開啟分頁時不會focus
64-
});
65-
console.log(
66-
`sign-in triggered at ${now.toLocaleTimeString()}`
67-
);
68-
}
69-
70-
console.log("check done..");
76+
// endregion
77+
78+
79+
/* firefox 編者注:
80+
已改成每日chrome.alarms,
81+
因此理論上不會重複開啟網頁,不過暫時還是保留這段程式碼,
82+
瀏覽器有limit,有需要請移除或注釋掉*/
83+
//簽到後用目前時間覆蓋掉上次時間,防止重複開啟網頁
84+
chrome.storage.sync.set({lastDate: new Date().getDate(),});
85+
86+
//開啟米哈遊的簽到頁面
87+
//這邊不需要做任何簽到動作,因為content.ts裡面已經設定只要開啟米哈遊網頁就會自動簽到了
88+
chrome.tabs.create({
89+
url: "https://act.hoyolab.com/ys/event/signin-sea-v3/index.html?act_id=e202102251931481",
90+
active: false, //開啟分頁時不會focus
91+
});
92+
93+
console.log(`Sign-in triggered`);
7194
});
7295
}
7396

74-
// checkSign();
97+
// region - functions scheduling
98+
function getNextRun(h: number, m: number) {
99+
// construct time now and target time for comparison
100+
const now = new Date();
101+
const todayAtHM = new Date(
102+
now.getFullYear(), now.getMonth(), now.getDate(),
103+
h, m, 0, 0
104+
);
105+
106+
// if the time is already past, set the date to tomorrow
107+
let date = now.getDate();
108+
if (now.getTime() > todayAtHM.getTime()) {
109+
date = now.getDate() + 1;
110+
};
111+
112+
// construct the target alarm time
113+
const nextRun = new Date(
114+
now.getFullYear(),
115+
now.getMonth(),
116+
date,
117+
h, m, 0, 0
118+
);
119+
120+
return nextRun;
121+
}
122+
123+
124+
async function scheduleNext() {
125+
await initializeDefaults();
126+
// create alarm once a day
127+
chrome.storage.sync.get(["signTime"], (data) => {
128+
129+
// get signTime
130+
let {signTime} = data as IConfigType;
131+
let h = Number(signTime.hours);
132+
let m = Number(signTime.minutes);
133+
134+
const nextrun = getNextRun(h, m);
135+
chrome.alarms.clear('dailySignIn', () => {
136+
chrome.alarms.create("dailySignIn", {when: nextrun.getTime()});
137+
});
138+
console.log(`Alarm set for ${nextrun.toLocaleDateString() + ' ' +nextrun.toLocaleTimeString()}`);
139+
});
140+
}
141+
// endregion
142+
143+
// region - main and event listeners
144+
async function performScheduledSign() {
145+
try {
146+
await checkSign();
147+
scheduleNext();
148+
} catch (err) {
149+
console.error("Sign or schedule failed:", err);
150+
}
151+
}
152+
153+
154+
chrome.runtime.onInstalled.addListener(({reason}) => {
155+
if (reason === "install") { // On a fresh install, run a catch-up
156+
console.log("==Fresh Install==");
157+
scheduleNext();
158+
}
159+
});
160+
161+
chrome.runtime.onStartup.addListener(() => {
162+
console.log("==Browser startup==");
163+
performScheduledSign();
164+
});
165+
166+
chrome.runtime.onMessage.addListener((msg, sender, _sendResponse) => {
167+
if (msg.action === "performScheduledSign") {
168+
console.log("==SignTime changed==");
169+
performScheduledSign(); // No response needed
170+
// No `return true`
171+
}
172+
});
173+
174+
chrome.alarms.onAlarm.addListener(alarm => {
175+
if (alarm.name !== "dailySignIn") {return; }
176+
console.log("==Alarm triggered==");
177+
performScheduledSign();
178+
});
179+
// endregion
180+
181+
/* region - debugging
182+
// check the time of the alarm
183+
// Since log in chrome.runtime will not show up in the console
184+
chrome.alarms.get("dailySignIn", alarm => {
185+
if (!alarm) {
186+
console.log("No dailySignIn alarm found");
187+
} else {
188+
console.log(
189+
"dailySignIn will fire next at",
190+
new Date(alarm.scheduledTime).toLocaleString()
191+
);
192+
}
193+
});
194+
195+
// reset the lastDate
196+
const yesterday = new Date();
197+
yesterday.setDate(yesterday.getDate() - 1);
198+
chrome.storage.sync.set({ lastDate: yesterday.getDate() }, () => {
199+
console.log(`✅ Debug: lastDate set to yesterday (${yesterday.getDate()})`);
200+
});
75201
76-
chrome.alarms.create({ periodInMinutes: 1 });
77-
chrome.alarms.onAlarm.addListener(() => checkSign());
202+
endregion*/

src/popup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ document.addEventListener("DOMContentLoaded", async () => {
113113
console.log("Selected time:", dateStr);
114114
chrome.storage.sync.set({ signTime: { hours, minutes } });
115115
updateSignTime(hours, minutes);
116+
chrome.runtime.sendMessage({ action: "performScheduledSign" });
116117
},
117118
});
118119
}

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const path = require("path")
44

55
const config = (_, options) => {
66
let config = {
7+
optimization: {
8+
minimize: false
9+
},
10+
devtool: "source-map",
711
entry: {
812
background: "./src/background.ts",
913
content: "./src/content.ts",

0 commit comments

Comments
 (0)