chrome警报的功能在扩展background.js中执行两次

问题描述

注意:这是我第一次编写扩展程序,并使用警报/通知。

我想自己学习如何进行扩展以及如何使用警报/通知,然后到了要点,即我的扩展在后台运行,定期在测试本地服务器上检查测试通知,并显示所有当下。问题在于,在最初的检查之后,每隔一个将被调用两次,这意味着每隔一个瞬间就会出现另一个并被覆盖。 这是“代码”(content.js目前为空):

manifest.json

{
    "manifest_version": 2,"name": "Bruh","version": "0.1","content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'","permissions": [
        "http://127.0.0.1/","alarms","notifications"
    ],"content_scripts": [
        {
            "matches": [
                "http://127.0.0.1/taikoThingy/users.php"
            ],"js": ["content.js"]
        }
    ],"browser_action": {
        "default_icon": "icon.png"
    },"background": {
        "scripts": ["background.js"],"persistent": false
    }
}

background.js

var audio = new Audio('./audio.mp3');

function checkForNotifications() {

    var xhttp = new XMLHttpRequest(); // an xhr request to a test server to look for notifications
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var result = this.responseText; // json containing notification data

            if (result != " ") {
                result = JSON.parse(result);
                chrome.notifications.create("whatever",{
                    "type": "basic","iconUrl": "./icon.png","title": `${result.notif_type}`,"message": `${result.notif_text}`
                },function notifId() {});
                audio.play();
            }
        }
    }
    xhttp.open("GET",'http://127.0.0.1/taikoThingy/test.php');
    xhttp.send();

}

chrome.alarms.create("notifAlarm",{"delayInMinutes":0.01,"periodInMinutes": 1});

chrome.alarms.onAlarm.addListener(function alarm() {
    checkForNotifications();
});

test.php

<?php

    require 'server.php';

    $stmt = $link->prepare("SELECT * FROM tab_notifications ORDER BY id ASC"); // get the oldest notification
    $stmt->execute();
    $result = $stmt->get_result();
    $stmt->close();

    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        echo '{ "notif_type":"' . $row['notif_type'] . '",'; // set the received data as JSON
        echo '"notif_text":"' . $row['notif_text'] . '"}';
        $stmt = $link->prepare("DELETE FROM tab_notifications ORDER BY id ASC LIMIT 1");
        $stmt->execute(); // delete that entry
        $stmt->close();
    } else {
        echo " ";
    }

?>

是什么原因导致重复执行?我应该解决什么?我找不到可行的解决方案:c

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...