Browse Source

Integrate interval triggering alarms and update permissions

In this change, interval triggering alarms have been integrated in `App.js` enriching its continuity aspects. The code now retrieves relevant data at each alarm triggering for enhanced monitoring. Additionally, the required permissions have been updated in `manifest.json` to effectively integrate this new feature.
Wang Tai 7 months ago
parent
commit
ff4c4842e4
3 changed files with 34 additions and 3 deletions
  1. 23 1
      public/background.js
  2. 7 1
      public/manifest.json
  3. 4 1
      src/App.js

+ 23 - 1
public/background.js

@@ -1,7 +1,29 @@
 // background.js
 
-const utils = {
 
+// MARK: - 强制激活插件
+setInterval(() => {
+  // 执行一些后台任务
+  console.log('后台任务');
+}, 10000);
+
+chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
+  // 检查标签的状态是否为'complete',并且URL符合特定条件
+  if (changeInfo.status === 'complete') {
+    console.log("特定网页已打开:", tab.url);
+  }
+  // 创建一个名为"triggerAlarm"的闹钟,5秒后触发,之后每10秒触发一次
+  chrome.alarms.create("triggerAlarmFromTab", {delayInMinutes: 0.083, periodInMinutes: 0.166});
+});
+
+chrome.alarms.create("triggerAlarmBgStart", {delayInMinutes: 0.083, periodInMinutes: 0.166});
+chrome.alarms.onAlarm.addListener((alarm) => {
+  console.log(`Alarm ${alarm.name} triggered`);
+});
+
+// MARK: - END 强制激活插件
+
+const utils = {
   /**
    * Retrieves the current time in HH:mm:ss format.
    *

+ 7 - 1
public/manifest.json

@@ -14,7 +14,13 @@
   "permissions": [
     "tts",
     "background",
-    "storage"
+    "storage",
+    "alarms",
+    "tabs",
+    "webNavigation"
+  ],
+  "host_permissions": [
+    "<all_urls>"
   ],
   "background": {
     "service_worker": "background.js"

+ 4 - 1
src/App.js

@@ -106,6 +106,8 @@ const App = () => {
        */
       () => {
 
+        // 创建一个名为"triggerAlarm"的闹钟,5秒后触发,之后每10秒触发一次
+        chrome.alarms.create("triggerAlarmFromApp", {delayInMinutes: 0.083, periodInMinutes: 0.166});
         //
         chrome.storage?.local.get(['log'], (obj) => {
           if (obj.log) {
@@ -123,6 +125,7 @@ const App = () => {
           }
         });
         chrome.storage?.local.get(['voiceNames', 'voiceName', 'voiceVolume', 'wsUrl', 'startAlert', 'wifi'], (obj) => {
+          console.log("get", obj);
           const voiceNames = obj.voiceNames;
           // appendLog(voiceNames);
           setOptions(voiceNames.map((voiceName) => {
@@ -248,7 +251,7 @@ const App = () => {
                     appendLog("停止报警");
                     setAlert(false);
                   });
-                }}>停止</Button>
+                }}>静音</Button>
                 {alert ?
                     <SoundOutlined/>
                     :