|
| 1 | +const LS = chrome.storage.local |
| 2 | + |
1 | 3 | function show() {
|
2 | 4 | var time = /(..)(:..)/.exec(new Date()); // 当前时间.
|
3 | 5 | var hour = time[1] % 12 || 12; // 小时
|
4 | 6 | var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // 上午、下午
|
5 |
| - new Notification(hour + time[2] + ' ' + period, { |
6 |
| - icon: '48.png', |
7 |
| - body: '是时候起来溜达一下了~~' |
| 7 | + chrome.notifications.create({ |
| 8 | + type: 'basic', |
| 9 | + iconUrl: 'img/icon_48.png', |
| 10 | + title: '是时候起来溜达一下了~~', |
| 11 | + message: hour + time[2] + ' ' + period, |
| 12 | + priority: 0 |
8 | 13 | });
|
9 | 14 | }
|
10 | 15 |
|
11 | 16 | // 判断是否已经初始化
|
12 |
| -if (!localStorage.isInitialized) { |
13 |
| - localStorage.isActivated = true; // 是否激活 |
14 |
| - localStorage.frequency = 1; // 显示间隔,分钟 |
15 |
| - localStorage.isInitialized = true; // 初始化状态 |
16 |
| -} |
| 17 | +async function init() { |
| 18 | + let isInitializedInitObj = await LS.get(['isInitialized']) |
| 19 | + if (!isInitializedInitObj.isInitializedInit) { |
| 20 | + LS.set({"isActivated": true}) // 是否激活 |
| 21 | + LS.set({"frequency": 1}) // 显示间隔,分钟 |
| 22 | + LS.set({"isInitialized": true}) // 初始化状态 |
| 23 | + } |
| 24 | + // 浏览器是否支持通知 |
| 25 | + var window = window ?? self; |
| 26 | + if (window.Notification) { |
| 27 | + // 加载时就先显示一下 |
| 28 | + let tmp = await LS.get(['isActivated']) |
| 29 | + if (tmp.isActivated) { show(); } |
17 | 30 |
|
18 |
| -// 浏览器是否支持通知 |
19 |
| -if (window.Notification) { |
20 |
| - // 加载时就先显示一下 |
21 |
| - if (JSON.parse(localStorage.isActivated)) { show(); } |
| 31 | + var interval = 0; // 间隔分钟数 |
22 | 32 |
|
23 |
| - var interval = 0; // 间隔分钟数 |
| 33 | + setInterval(async () => { |
| 34 | + interval++; |
| 35 | + let isActivatedObj = await LS.get(['isActivated']) |
| 36 | + let frequencyObj = await LS.get(['frequency']) |
| 37 | + if ( |
| 38 | + isActivatedObj.isActivated && |
| 39 | + (frequencyObj.frequency <= interval) |
| 40 | + ) { |
| 41 | + show(); |
| 42 | + interval = 0; |
| 43 | + } |
| 44 | + }, 60000); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +init() |
24 | 49 |
|
25 |
| - setInterval(function() { |
26 |
| - interval++; |
27 | 50 |
|
28 |
| - if ( |
29 |
| - JSON.parse(localStorage.isActivated) && |
30 |
| - localStorage.frequency <= interval |
31 |
| - ) { |
32 |
| - show(); |
33 |
| - interval = 0; |
34 |
| - } |
35 |
| - }, 60000); |
36 |
| -} |
|
0 commit comments