Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 变量=true,即使if语句为false_Javascript_Jquery_Userscripts - Fatal编程技术网

Javascript 变量=true,即使if语句为false

Javascript 变量=true,即使if语句为false,javascript,jquery,userscripts,Javascript,Jquery,Userscripts,我正在制作一个通知系统用户脚本。。基本上,它所做的就是向一个带有.txt文件的站点发送GET请求。文件输出如下: 12345|User notification all|Everyone will see this 12345是用户的ID,all只是设置为向所有人显示消息。当我写入用户ID(如12345)时,它将向ID为12345的特定用户发送通知,如果我写入“全部”,它将向所有人发送通知 问题:我对每个通知都有一个“取消”按钮(因为两个通知可以同时显示),但当我取消两个通知,然后发送新通知时

我正在制作一个通知系统用户脚本。。基本上,它所做的就是向一个带有.txt文件的站点发送GET请求。文件输出如下:

12345|User notification
all|Everyone will see this
12345
是用户的ID,
all
只是设置为向所有人显示消息。当我写入用户ID(如12345)时,它将向ID为12345的特定用户发送通知,如果我写入“全部”,它将向所有人发送通知

问题:我对每个通知都有一个“取消”按钮(因为两个通知可以同时显示),但当我取消两个通知,然后发送新通知时,两个通知都会显示,即使我取消了其中一个通知。这是我的密码:

GM_xmlhttpRequest({
    method: "GET",
    url: "http://website.com/notification.txt",
    ignoreCache: false,
    onload: function(response){
        res = trim(response.responseText);
        array = res.split("\n");

        for(i = 0; i < array.length; i++) {
            array2 = array[i].split("|");

            if(array2[0] == uid) {
                userMessage = array2[1];
            }
            if(array2[0] == "all") {
                allMessage = array2[1];
            }
        }

        if(typeof userMessage !== 'undefined'){
            if(GM_getValue("userMessage") != userMessage) {  
                GM_setValue("userMessage", userMessage);
                GM_setValue("userDismiss", false);
            }
        }

        if(typeof allMessage !== 'undefined'){
            if(GM_getValue("allMessage") != allMessage) {
                GM_setValue("allMessage", allMessage);
                GM_setValue("allDismiss", false);
            }
        }
    }
});
检查用户ID是否等于用户的uid(用户ID),或者是否显示
all
。然后看看这些:

if(GM_getValue("userMessage") != userMessage) { 

if(GM_getValue("allMessage") != allMessage) {
这些检查来自先前通知的存储消息(在GM_getValue内)是否与它刚收到的新数据不相等。如果是相同的,则不应继续,但是,如果只有一个通知不同,则会显示两个If语句都为true,然后继续。即使
GM_getValue(“userMessage”)等于userMessage
,它仍然会继续执行if语句,即使它说的是
=

另外,我在userMessage、allMessage和GM_getValue()上尝试了console.log(),如果我尝试为所有人添加通知,它会说userMessage与GM_getValue(“userMessage”)相同。因此这些if语句有问题,但我不知道是什么


任何帮助都将不胜感激。我被困在这里,请帮助我。如果您需要更多信息,请告诉我。

我认为您需要在这里正确地定义一些变量,例如(
allMessage
userMessage
,以及其他一些变量).我想一旦你正确地审视
allMessage
userMessage
你就会很好

GM_xmlhttpRequest({
  method: "GET",
  url: "http://website.com/notification.txt",
  ignoreCache: false,
  onload: function(response){
    var res = trim(response.responseText);
    var array = res.split("\n");
    var array2 = [];
    var allMessage, userMessage;

    for(var i = 0; i < array.length; i++) {
        array2 = array[i].split("|");

        if(array2[0] == uid) {
            userMessage = array2[1];
        }
        if(array2[0] == "all") {
            allMessage = array2[1];
        }
    }

    if(typeof userMessage !== 'undefined'){
        if(GM_getValue("userMessage") != userMessage) {  
            GM_setValue("userMessage", userMessage);
            GM_setValue("userDismiss", false);
        }
    }

    if(typeof allMessage !== 'undefined'){
        if(GM_getValue("allMessage") != allMessage) {
            GM_setValue("allMessage", allMessage);
            GM_setValue("allDismiss", false);
        }
    }
  }
});
GM\u xmlhttpRequest({
方法:“获取”,
url:“http://website.com/notification.txt",
ignoreCache:false,
onload:函数(响应){
var res=微调(response.responseText);
var数组=res.split(“\n”);
var array2=[];
var-allMessage,userMessage;
对于(var i=0;i
这样,旧结果不会影响新结果

GM_xmlhttpRequest({
  method: "GET",
  url: "http://website.com/notification.txt",
  ignoreCache: false,
  onload: function(response){
    var res = trim(response.responseText);
    var array = res.split("\n");
    var array2 = [];
    var allMessage, userMessage;

    for(var i = 0; i < array.length; i++) {
        array2 = array[i].split("|");

        if(array2[0] == uid) {
            userMessage = array2[1];
        }
        if(array2[0] == "all") {
            allMessage = array2[1];
        }
    }

    if(typeof userMessage !== 'undefined'){
        if(GM_getValue("userMessage") != userMessage) {  
            GM_setValue("userMessage", userMessage);
            GM_setValue("userDismiss", false);
        }
    }

    if(typeof allMessage !== 'undefined'){
        if(GM_getValue("allMessage") != allMessage) {
            GM_setValue("allMessage", allMessage);
            GM_setValue("allDismiss", false);
        }
    }
  }
});